Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

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

woocommerce - how to display the short description in my cart and the checkout

I would like to display the short description of my product rather than its name in my cart and the checkout I went to the cart.php file and I found a code I think I must change it someone would have a solution for me ?

    <td class="product-name" data-title="<?php esc_attr_e( 'Product', 'woocommerce' ); ?>">
    <?php
    if ( ! $product_permalink ) {
    echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), 
    $cart_item, $cart_item_key ) . '&nbsp;' );
    } else {
    echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', sprintf( '<a href="%s">%s</a>', 
    esc_url( $product_permalink ), $_product->get_name() ), $cart_item, $cart_item_key ) );
                    }
question from:https://stackoverflow.com/questions/65852326/how-to-display-the-short-description-in-my-cart-and-the-checkout

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

1 Answer

0 votes
by (71.8m points)

If you want to display the short description in the cart and in the checkout you can do it using the woocommerce_cart_item_name hook.

If you want to show the short description of all product types you can use this code:

// returns the short description as the cart item name
add_filter( 'woocommerce_cart_item_name', 'change_cart_item_name', 99, 3 );
function change_cart_item_name( $item_name, $cart_item, $cart_item_key  ) {

    $product = $cart_item['data'];

    // get short description
    $short_description = $product->get_short_description();

    // if it exists, it returns the short description as the cart item name
    if ( ! empty( $short_description ) ) {
        return $short_description;
    } else {
        return $item_name;
    }

    return $item_name;

}

If, in the case of product variations, you want to show the short description of the parent product (variable product) you can use the following code:

// returns the short description as the cart item name
add_filter( 'woocommerce_cart_item_name', 'change_cart_item_name', 99, 3 );
function change_cart_item_name( $item_name, $cart_item, $cart_item_key  ) {

    $product = $cart_item['data'];

    // if it is a variation it gets the description of the variable product
    if ( $product->is_type( 'variation' ) ) {
        $parent_id = $product->get_parent_id();
        $variable = wc_get_product( $parent_id );
        $short_description = $variable->get_short_description();
    } else {
        // get short description
        $short_description = $product->get_short_description();
    }

    // if it exists, it returns the short description as the cart item name
    if ( ! empty( $short_description ) ) {
        return $short_description;
    } else {
        return $item_name;
    }

    return $item_name;

}

It works both on the cart page and in the checkout.

The code must be added in your theme's functions.php file.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...