Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
563 views
in Technique[技术] by (71.8m points)

php - WooCommerce: Display also product variation description on cart items

I'm trying to display my product variation description in my Cart. I have tried inserting this code in the cart.php template:

if ( $_product->is_type( 'variation' ) ) {echo $_product->get_variation_description();}

By following this documentation https://docs.woocommerce.com/document/template-structure/

But it's still not showing up.

Not sure what I'm doing wrong here.

Can anyone help on this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...