Translate custom labels fields in function.php file of my theme - php

With the help from the community, I have succeeded to create, save and print the labels and their values to the single product page.
I can also translate the input values into different languages using Polylang, but translating the custom labels (Conditions and Brands) is extremely hard.
Anyone out there can help me with these issues?
I tried to use Polylang, Saywhat...with no success!
Here is the code:
// Enabling and Displaying Fields in backend
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
woocommerce_wp_text_input( array( // Text Field type
'id' => '_conditions',
'label' => __( 'Conditions', 'woocommerce' ),
'placeholder' => 'i.e: brand-new; refurbished; defected...',
'desc_tip' => 'true',
'description' => __( 'Enter the conditions of the products here.', 'woocommerce' )
) );
woocommerce_wp_text_input( array( // Text Field type
'id' => '_brands',
'label' => __( 'Brands', 'woocommerce' ),
'placeholder' => 'i.e: Lacoste; Hugo Boss...etc',
'desc_tip' => 'true',
'description' => __( 'Enter names of the Brands of the products if any.', 'woocommerce' )
));
echo '</div>'; // Closing </div> tag HERE
}
// Save Fields values to database when submitted (Backend)
add_action( 'woocommerce_process_product_meta', 'woo_save_custom_general_fields' );
function woo_save_custom_general_fields( $post_id ){
// Saving "Conditions" field key/value
$conditions_field = $_POST['_conditions'];
if( !empty( $conditions_field ) )
update_post_meta( $post_id, '_conditions', esc_attr( $conditions_field ) );
// Saving "Brands" field key/value
$brands_field = $_POST['_brands'];
if( !empty( $brands_field ) )
update_post_meta( $post_id, '_brands', esc_attr( $brands_field ) );
}
add_action('woocommerce_single_product_summary', 'woo_display_custom_general_fields_values', 20);
function woo_display_custom_general_fields_values() {
global $product;
echo '<p class="custom-conditions">Conditions: ' . get_post_meta( $product->id, '_conditions', true ) . '</p>';
echo '<p class="custom-brands">Brands: ' . get_post_meta( $product->id, '_brands', true ) . '</p>';
}
And here a screenshot:
Thank you.

First, you should change the gettex domain name from 'woocommerce' to the domain name of your theme (or something more custom), as it doesn't make part of woocommerce code, and it's located in your active theme.
1) the Free alternative:
Then as it's not really content that you are trying to translate, but some peaces of code located in the function.php file of your active child theme (or active theme), you should use a specialized free plugin as Loco Translate that will provides in-browser editing of WordPress translation files.
Loco Translate, also provides localization tools for developers, such as extracting strings and generating templates.
Loco Translate features include:
Built-in translation editor within WordPress admin
Create and update language files directly in your theme or plugin
Extraction of translatable strings from your source code
Native MO file compilation without the need for Gettext on your system
Support for PO features including comments, references and plural forms
PO source view with clickable source code references
Protected language directory for saving custom translations
Configurable PO file backups
Built-in WordPress locale codes
2) The commercial complete way (completely compatible with WooCommerce):
The most complete commercial alternative is WPML plugin, that will also handle perfectly and more easily WooCommerce custom localisation and translations content for multilingual web sites. The other free optional plugins are incomplete for WooCommerce and much more difficult to get them work totally well.
So If you are planing to publish a multilingual website, think about it.

Related

Show or hide admin custom setting fields, based on WooCommerce product type

I made custom field backend, which i want to be showed for simple product types and hided for other types. I think little is not enought to me to achieve it. I will appreciate any help. Thanks!
Code I added is:
add_action('admin_head', 'show_this_for_simple_products_and_hide_for_others');
function show_this_for_simple_products_and_hide_for_others() {
?>
<script>
jQuery( function($){
$( 'body' ).on( 'woocommerce-product-type-change', function( event, select_val, select ) {
if ( select_val === 'simple' ) {
// modify to an actual CSS class selector
$( '.p.form-field.final_product_sku_field' ).show();
}
} );
$( 'select#product-type' ).change();
} );
</script>
<?php
}
Updated
You don't need that as there is already some CSS class selector that allows that.
To show a field for "simple" product type, you need to add in between your custom setting fields (inside your function):
echo '<div class="show_if_simple hidden">';
// Your fields here (for "simple" products type only)
echo '</div >';
You can use the show_if_{$product_type} or hide_if_{$product_type} composite class selectors, to show or hide custom settings product fields based on the product type (where {$product_type} need to be replaced by the targeted product type).
It works for all custom product types.
The class selector hidden specify that the content is hidden for non specified product types to be shown.
You can also use those other class selectors like show_if_virtual, hide_if_virtual, show_if_downloadable and hide_if_downloadable.
The class selector options_group adds a separator horizontal line.
Here is an example (field is shown only for simple products):
add_action( 'woocommerce_product_options_sku', 'add_product_final_sku_custom_field' );
function add_product_final_sku_custom_field(){
global $post, $product_object;
echo '<div class="show_if_simple hidden">';
woocommerce_wp_text_input( array(
'label' => __( 'Final SKU', 'woocommerce' ),
'placeholder' => __( 'Enter final SKU here', 'woocommerce' ),
'id' => 'final_sku',
'desc_tip' => true,
'description' => __( 'This SKU is for final use only.', 'woocommerce' ),
) );
echo '</div>';
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
You can see that on the related core html files code source that displays WooCommerce Product setting fields on admin single product pages.

Updating Woo commerce checkout Page with Additional Form Data

I am collecting an additional User Meta Data for my Woo commerce check out page.
woocommerce_form_field('myName', array(
'type' =>'text',
'class'=>array('my-field-class form-row-wide'),
'label'=>__('First Name'),
'placeholder'=>__('Please enter your name'),
), $checkout->get_value('myName'));
And I am updating to the database with this code:
/*Update the info with the checkout*/
add_action('woocommerce_checkout_field_update_order_meta','my_custom_checkout_field_update_meta');
function my_custom_checkout_field_update_meta($order_id){
if($_POST['MyName'])
update_post_meta($order_id, 'First Name',esc_attr($POST['MyName']));
}
Each time I submit, I get an internal server Error, even though I am working on a local machine. I need to collect that data and persist it into the order Database. Anybody help?
Update: Normally the postmeta meta_key should not use white spaces and capitals…
Also your additional field should need to be inside the checkout form, if not nothing will be submitted and nothing can be saved.
To display an custom text field and save it in the database once submitted, the best way is to use the following:
// Add checkout custom text field
add_action( 'woocommerce_before_order_notes', 'add_checkout_custom_field', 20, 1 );
function add_checkout_custom_field( $checkout) {
// Text field
woocommerce_form_field('my_name', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('First Name'),
'placeholder' =>__('Please enter your name'),
), $checkout->get_value('my_name') );
}
// Save the data to the order
add_action('woocommerce_checkout_create_order','my_custom_checkout_field_update_meta');
function my_custom_checkout_field_update_meta( $order ){
if( isset($_POST['my_name']) && ! empty($_POST['my_name']) )
$order->update_meta_data( '_my_name', sanitize_text_field($POST['my_name']) );
}
Code goes in function.php file of your active child theme (or active theme). Tested and works…
To get this data once saved in the order, use get_post_meta( $order_id, '_my_name', true ); where $order_id is the dynamic order ID…
Your additional text field will be located just before "Order notes" field:
Now your additional field is confusing as Billing and Shipping first name fields already exist in checkout page.

How can I create a Custom Text Box on a WooCommerce Product Page?

I am working on a WordPress website, with WooCommerce functionality.
I have created 2 Custom Fields, for the Product Data box within the backend of the Product Page, using the following Code:
<?php
function theme_name_woocommerce_custom_fields() {
// Price Per Character
woocommerce_wp_text_input(
array(
'id' => '_character_price',
'label' => 'Price Per Character',
'description' => 'This is the amount a customer pays per letter entered.',
'desc_tip' => 'true',
'placeholder' => 'Enter Amount per Letter (Exclude the £)'
)
);
// Custom Text Box
woocommerce_wp_checkbox(
array(
'id' => '_custom_text_box',
'label' => 'Show Custom Text Box',
'description' => 'Select this box, if you would like a Custom Text Box to appear on the Product's page.',
'desc_tip' => 'true',
)
);
}
add_action( 'woocommerce_product_options_general_product_data', 'theme_name_woocommerce_custom_fields' );
function save_theme_name_woocommerce_custom_field( $post_id ) {
if ( ! empty( $_POST['_custom_text_field'] ) ) {
update_post_meta( $post_id, '_custom_text_field', esc_attr( $_POST['_custom_text_field'] ) );
}
}
add_action( 'woocommerce_process_product_meta', 'save_theme_name_woocommerce_custom_fields' );
?>
In reference to the woocommerce_wp_checkbox, I would like to create a function whereby when this checkbox is selected, it creates a Custom Text Box on the associated Product's page. This Custom Text Box can then be used by a potential customer to enter a piece of text, which they would like to have printed to the page's Product.
Is anyone aware of what additional piece of coding I would need to enter, in order to achieve said goal?
You can override simple/variable product's template file and add custom field there. Keep logic of show/hide textbox when checkbox is checked there only.
When add to cart is done, you will get all posted data in POST. Grab it from there and put into woocommerce' object.
=======================================================
Edited:
You can follow this steps:
Copy woocommerce/templates/single-product/add-to-cart/variable.php this template to your child theme and customize it. You will see a form tag there. In that you can put your custom created checkbox(which you added from admin). Also, add a custom textbox(in which user will enter text) - and hide it.
Add custom js from functions.php. - In this js you can write logic that if checkbox is checked then you will show the textbox, else not.
Now, when user does add to cart, add this custom textbox data to woocommerce object. How to enter custom data to woocommerce object - you can have step by step details here: https://wisdmlabs.com/blog/add-custom-data-woocommerce-order/

How to add wordpress image caption to woocommerce archive product (shop) page but not to the single product page

I am using Woocommerce 2.4.4 and Wordpress 4,3, I have read many suggestions for making changes to woocommerce shop page; however I cannot find anyone who addresses my specific request. I have limited understanding of Woocommerce; however I have been trying to solve my request, but to no avail.
I want to display the wordpress caption field for each image in the wordpress media library in my woocommerce shop page but not on the individual products.
I have made changes to my child theme, Deli (A woocommerce theme) functions.php . I added the following to the child functions.php.
function wp_get_attachment( $attachment_id ) {
$attachment = get_post( $attachment_id );
return array(
'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt',
true ),
**'caption' => $attachment->post_excerpt,**
'description' => $attachment->post_content,
'href' => get_permalink( $attachment->ID ),
'src' => $attachment->guid,
'title' => $attachment->post_title
);
}
I believe the field that has the image caption is 'caption' => $attachment->post_excerpt, as defined above.
I am trying to edit the Woocommerce Archive-product.php but do not know what to add or where to add it in the archive-product.php.
Hey as per your requirement have tried a code which might help you in your case or you can customize it further as per your request.
Add the following code to your Theme's functions.php.
Note: Its better to create a child theme first and do your customization in it so that the customization won't get overwritten when the theme updates.
add_filter( 'wp_get_attachment_image_attributes','wdm_shop_image_caption_func', 10,3 );
function wdm_shop_image_caption_func($attr, $attachment, $size){
if($size=='shop_single'){
unset($attr['title']);
}
elseif($size=='shop_catalog'){
$attr['title']=$attr['alt'];
}
return $attr;
}
Make sure that you set Caption for every Product's Images.

Wordpress Navigation Help

I am working on a site that calls the categories of a wordpress page and displays them in the right-side navigation using a php call. I am new to php and web programming in general. Is there a way I could split the categories into two sections using a particular php call or perhaps an if-loop.
Essentially, I want to display particular categories under custom sub-headings to better organize the site. Any help, I'm currently using the following script to display my categories:
<ul><?php wp_list_categories('show_count=1&title_li='); ?></ul>
Here is my site for reference: http://www.merrimentdesign.com
Try using your code above twice. Each time, you can use the other function arguments to limit the output to certain categories. See http://codex.wordpress.org/Template_Tags/wp_list_categories for the various ways to customize the output of the function.
For example you could use:
<ul><?php wp_list_categories('show_count=1&title_li=&child_of=100'); ?></ul>
// where 100 is the parent id of all of the categories you want to print.
<ul><?php wp_list_categories('show_count=1&title_li=&exclude_tree=100'); ?></ul>
// and then show everything, but children of 100
Or simply use the first string multiple times specifying different parent ids each time.
By far and away your best option is to use the new menu functionality within WordPress. It's dead straight forward to set up in your theme:
add_theme_support( 'menus' );
add_action( 'init', 'register_my_menus' );
function register_my_menus() {
register_nav_menus(
array(
'public-menu' => __( 'Public Menu' ),
'sidebar-public-menu' => __( 'Sidebar Public Menu' ),
'sidebar-members-menu' => __( 'Sidebar Members Menu' ),
'sidebar-staff-menu' => __( 'Sidebar Staff Menu' ),
'footer-menu' => __( 'Footer Menu' )
)
);
}
place that in your functions.php file (and obviously change it for your requirements).
Then in your template file - probably sidebar.php you'll want something like:
<?php wp_nav_menu( array( 'theme_location' => 'sidebar-staff-menu', 'container' => false ) ); ?>
And then go to the back end of WordPress (your wp-admin) and then go to Appearance > Menus and voila you're able to drag and drop your categories to your heart's content!
Helpful link: http://justintadlock.com/archives/2010/06/01/goodbye-headaches-hello-menus
Read that, Justin Tadlock is awesome.
Good luck.

Categories