Asked  7 Months ago    Answers:  5   Viewed   52 times

Ever since we upgraded to Woocommerce version 3 our order confirmations are showing huge titles that include the variation detail. I don't like how it looks and it breaks some important functionalities in some custom-made plugins.

Reference: Order Name Showing Variations since update to WC version 3

There is a filter that can be used to disable this data displaying in the title called woocommerce_product_variation_title_include_attribute_name from what I understand. But I have no idea where to apply the filter.

Is there a quick way to apply the filter to change it back to display as it did before?

 Answers

27

This filter should work returning a false value for $should_include_attributes first argument in woocommerce_product_variation_title_include_attributes filter hook this way:

add_filter( 'woocommerce_product_variation_title_include_attributes', 'custom_product_variation_title', 10, 2 );
function custom_product_variation_title($should_include_attributes, $product){
    $should_include_attributes = false;
    return $should_include_attributes;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

It should just work as you expect.


Update: The shorter way is:

add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_false' );

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

just works too.

Wednesday, March 31, 2021
 
nighter
answered 7 Months ago
11

The missing part to get it work for for cart and checkout pages (and also Orders and email notifications too) is a very simple trick:

add_action( 'woocommerce_before_calculate_totals', 'set_cart_item_sale_price', 20, 1 );
function set_cart_item_sale_price( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Iterate through each cart item
    foreach( $cart->get_cart() as $cart_item ) {
        $price = $cart_item['data']->get_sale_price(); // get sale price
        $cart_item['data']->set_price( $price ); // Set the sale price

    }
}

Code goes in function.php file of your active child theme (active theme).

Tested and works.

So the code just set the product sale price as the product price in cart items and it works.

Saturday, May 29, 2021
 
Zach
answered 5 Months ago
97

On shop page you can't use add-to-cart button for variable products, because you need first to go on single product page to chose the options for this variable product before adding it to cart.

On variable product pages, normally you have some displayed options to chose for a variable product, before using "add to cart" button. If you don't do it, you get the error messageā€¦
So at this point:

  1. The options are not displayed in the product page (Bad settings in your backend product page, a bug with your theme or some additional plugin):
    • Check your product backend settings
    • Try to switch to default wordpress theme (to see if this issue is still there)
    • Try to disable most of all plugins.
  2. the options are displayed: So chose your options first for this product, then add to cart

If this issue is related to your theme, contact the author of your theme and open a support thread or ticketā€¦


OUPUTTING PRODUCT VARIATIONS FOR A PRODUCT ID:

To get product variations programmatically for a variable product ID:

$product = wc_get_product( $product_id );
$product_variations = $product->get_available_variations();

echo var_dump($product_variations); // Displaying the array

Then to get the first variation ID:

$product = wc_get_product( $product_id );
$product_variations = $product->get_available_variations();

$variation_product_id = $product_variations [0]['variation_id'];
echo $variation_product_id; // Displaying the variation ID

Or to get an array of all variations ID of this product ID:

$product = wc_get_product( $product_id );
$product_variations = $product->get_available_variations();

$arr_variations_id = array();
foreach ($product_variations as $variation) {
    $product_variation_id = $variation['variation_id'];
    array_push( $arr_variations_id, $product_variation_id );
}

echo var_dump($arr_variations_id); // Displaying the array of variations ID

A reference : Change "Add to Cart" button to "Go to Product" in the Shop Page

Saturday, May 29, 2021
 
mgraph
answered 5 Months ago
88

To get post meta data you can use WordPress get_post_meta() function instead (It's much shorter and affordable).

So your code should be now:

if ( ! function_exists( 'print_attribute_radio' ) ) {
    function print_attribute_radio( $checked_value, $value, $label, $name, $product_id ) {

        // This handles < 2.4.0 bw compatibility where text attributes were not sanitized.
        $checked = sanitize_title( $checked_value ) === $checked_value ? checked( $checked_value, sanitize_title( $value ), false ) : checked( $checked_value, $value, false );

        $input_name = 'attribute_' . esc_attr( $name ) ;
        $esc_value = esc_attr( $value );
        $id = esc_attr( $name . '_v_' . $value );
        $filtered_label = apply_filters( 'woocommerce_variation_option_name', $label );

        // HERE the product variation description
        $variation_id = $product_id + 3;
        $variable_description = get_post_meta( $variation_id, '_variation_description', true );

        printf( '<div>
        <input type="radio" name="%1$s" value="%2$s" id="%3$s" %4$s>
        <label for="%3$s">%5$s</label>
        <label>'.$variable_description.'</label>
        </div>', $input_name, $esc_value, $id, $checked, $filtered_label );
    }
}
Saturday, May 29, 2021
 
sohum
answered 5 Months ago
30

Updated for WooCommerce version 3 and above

Since WooCommerce 3, get_variation_description() is now deprecated and replaced by get_description() WC_Product method.

To get your product item variation description (filtering variation product type condition), you can use the following hooked function instead:

// Cart page (and mini cart)
add_filter( 'woocommerce_cart_item_name', 'cart_item_product_description', 20, 3);
function cart_item_product_description( $item_name, $cart_item, $cart_item_key ) {
    if ( ! is_checkout() ) {
        if( $cart_item['variation_id'] > 0 ) {
            $description = $cart_item['data']->get_description(); // variation description
        } else {
            $description = $cart_item['data']->get_short_description(); // product short description (for others)
        }

        if ( ! empty($description) ) {
            return $item_name . '<br><div class="description">
                <strong>' . __( 'Description', 'woocommerce' ) . '</strong>: '. $description . '
            </div>';
        }
    }
    return $item_name;
}

// Checkout page
add_filter( 'woocommerce_checkout_cart_item_quantity', 'cart_item_checkout_product_description', 20, 3);
function cart_item_checkout_product_description( $item_quantity, $cart_item, $cart_item_key ) {
    if( $cart_item['variation_id'] > 0 ) {
        $description = $cart_item['data']->get_description(); // variation description
    } else {
        $description = $cart_item['data']->get_short_description(); // product short description (for others)
    }

    if ( ! empty($description) ) {
        return $item_quantity . '<br><div class="description">
            <strong>' . __( 'Description', 'woocommerce' ) . '</strong>: '. $description . '
        </div>';
    }

    return $item_quantity;
}

Now the description is just displayed between the title and the variation attributes values (if there is any).

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

Saturday, July 3, 2021
 
eliotlencelot
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :