I would like to add a function that fires when the customer selects a product variant. Just as the price appears (after choosing variants).
It is a simple function (from functions.php) that reads additional information about productions from the database.
However, I do not know if there is such a hook?
To add a text on selecting a variation you can use the following:
add_filter('woocommerce_available_variation', 'variation_custom_text', 10, 3 );
function variation_custom_text( $variation_data, $product, $variation ) {
// Here your custom text
$custom_text = __("This is my custom text…", "woocommerce");
$variation_data['availability_html'] .= '<p>' . $custom_text . '</p>';
return $variation_data;
}
Code goes in function.php file of the active child theme (or active theme). Tested and works.
Related
During the checkout process, under Company, I have another field for Company Registration Number. Now, everything works fine, the field is correctly saved under each order but I was wondering how can I display the company registration number under company name when I open the order in the admin panel.
PS: field name is billing_cif
From this WooCommerce snippet found in Customizing checkout fields using actions and filters (where there is a little mistake), try the following:
/**
* Display field value on the order edit page
*/
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('My Field').':</strong><br>' . get_post_meta( $order->get_id(), 'billing_cif', true ) . '</p>';
}
You need to be sure that the correct meta_key under wp_postmeta table is billing_cif as it could be saved also like _billing_cif instead.
Code goes in functions.php file of the active child theme (or active theme). It should works.
Or the same thing in a better way since WooCommerce 3:
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta( $order ){
$billing_cif = $order->get_meta('billing_cif'); // Get custom field value
if( ! empty( $billing_cif ) ) {
echo '<p><strong>'.__('CIF reference').':</strong><br>' . $billing_cif . '</p>';
}
}
Code goes in functions.php file of the active child theme (or active theme). It should works too.
I have lots of variable products and the description field is left blank by default, but I have other plugins which are set to display the Variation Description field on the front end. How can I get my site to copy the variation name (Pink Set No Box, Pink Set With Box, Green Set No Box etc.) into the corresponding variation description field?
As well as doing it for all existing products, it would need to do it for all new products being added as well.
So how can I copy automatically for product variations, the title into description field in WooCommerce?
Instead of copying the variation product name in the variation description, you can assign it to the description dynamically using this very simple hooked function:
add_filter( 'woocommerce_product_variation_get_description', 'wc_product_variation_get_description_filter', 10, 2 );
function wc_product_variation_get_description_filter( $description, $product ){
return $product->get_name();
}
Or you can also append it to the description using:
add_filter( 'woocommerce_product_variation_get_description', 'wc_product_variation_get_description_filter', 10, 2 );
function wc_product_variation_get_description_filter( $description, $product ){
return $description . $product->get_name();
}
Or prepend it with:
add_filter( 'woocommerce_product_variation_get_description', 'wc_product_variation_get_description_filter', 10, 2 );
function wc_product_variation_get_description_filter( $description, $product ){
return $product->get_name() . $description;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
On some cases when products have been created since a long time, the attributes are not displayed in the product variation name… You will need to add the following line:
add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_true' );
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Related:
How to display the variation name in Woocommerce Items
Remove attribute values from product variation title and show them on separate rows
I thought this would have been easy, but I am stuck. All I am trying to do is add the word each after the variation price on a product page. The solution I have found adds it on the category page and in two places on the product page.
The code is:
/* Adds a text Each - after price */
function change_product_price( $price ) {
$price .= ' each';
return $price;
}
add_filter( 'woocommerce_get_price_html', 'change_product_price' );
From the picture above, I only need the each added to the price above the add to cart button, but not the other pacles like the crossed out section in the photo above.
Thank you for any guidance you can provide.
The following code will add a suffix to the product variations price:
add_filter('woocommerce_available_variation', 'variation_price_custom_suffix', 10, 3 );
function variation_price_custom_suffix( $variation_data, $product, $variation ) {
$variation_data['price_html'] .= ' <span class="price-suffix">' . __("each", "woocommerce") . '</span>';
return $variation_data;
}
Code goes in function.php file of your active child theme (active theme). Tested and works.
I am using this function so I get the default variation price but I want to show my price suffix after the price:
add_filter('woocommerce_variable_price_html', 'custom_variation_price', 10, 2);
function custom_variation_price( $price, $product ) {
foreach($product->get_available_variations() as $pav){
$def=true;
foreach($product->get_variation_default_attributes() as $defkey=>$defval){
if($pav['attributes']['attribute_'.$defkey]!=$defval){
$def=false;
}
}
if($def){
$price = $pav['display_price'];
}
}
return woocommerce_price($price);
}
What I am getting out now is for example '€15,00' but what I want it to show is '€15,00 per kilo' and 'per kilo' is the price suffix
Your code is outdated since Woocommerce 3 (get_variation_default_attributes() and woocommerce_price() are deprecated).
The will display the selected variation price suffixed for Woocommerce 3:
add_filter('woocommerce_available_variation', 'display_variation_price_suffixed', 10, 3 );
function display_variation_price_suffixed( $variation_data, $product, $variation ) {
$variation_data['price_html'] .= ' <span class="price-suffix">' . __("per kilo", "woocommerce") . '</span>';
return $variation_data;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
If needed you can use $variation_data['attributes'] array to target specific product attributes term values in a foreach loop.
For information, the filter hook woocommerce_available_variation is located inside WC_Product_Variable get_available_variation() method.
I would like to change the way Woocommerce is displaying the product quantity in the order review table. I would like the quantity to be underneath the product name instead of after it.
I found this post which helped, but the code only changes the quantity layout for variable products.
How can I change it for EVERY product, even simple ones?
This can be done in multiple ways:
1) Overriding template checkout/review-order.php via your child theme.
2) Customizing the product item name:
add_filter( 'woocommerce_cart_item_name', 'customizing_checkout_item_name', 10, 3);
function customizing_checkout_item_name( $item_name, $cart_item, $cart_item_key ) {
if( is_checkout() )
$item_name .= '<br>';
return $item_name;
}
Code goes in function.php file of the active child theme (or active theme).
3) Customizing the product item quantity (the best way):
add_filter( 'woocommerce_checkout_cart_item_quantity', 'customizing_checkout_item_quantity', 10, 3);
function customizing_checkout_item_quantity( $quantity_html, $cart_item, $cart_item_key ) {
$quantity_html = ' <br>
<span class="product-quantity">' . __('Quantity:') . ' <strong>' . $cart_item['quantity'] . '</strong></span>';
return $quantity_html;
}
Code goes in function.php file of the active child theme (or active theme).
All code is tested and works.