Ad
Display Product Variation Description Or Product Short Description Based On Product Type In WooCommerce Checkout
This code snippet displays product short description at WooCommerce checkout:
// Display on cart & checkout pages
function filter_woocommerce_get_item_data( $item_data, $cart_item ) {
// Product excerpt
$post_excerpt = get_the_excerpt( $cart_item['product_id'] );
// NOT empty
if ( ! empty( $post_excerpt ) ) {
$item_data[] = array(
'key' => __( 'Product description', 'woocommerce' ),
'value' => $post_excerpt,
'display' => $post_excerpt,
);
}
return $item_data;
}
add_filter( 'woocommerce_get_item_data', 'filter_woocommerce_get_item_data', 10, 2 );
The issue is that a variable product can only have 1 product short description, so all of the product variations have the same exact description.
Is it possible to modify this code snippet to display product variation description instead of product short description for variable products?
Ad
Answer
To display product variation description instead of product short description for variable products, you can use:
// Display on cart & checkout pages
function filter_woocommerce_get_item_data( $item_data, $cart_item ) {
// Compare
if ( $cart_item['data']->get_type() == 'variation' ) {
// Get the variable product description
$description = $cart_item['data']->get_description();
} else {
// Get product excerpt
$description = get_the_excerpt( $cart_item['product_id'] );
}
// Isset & NOT empty
if ( isset ( $description ) && ! empty( $description ) ) {
$item_data[] = array(
'key' => __( 'Description', 'woocommerce' ),
'value' => $description,
'display' => $description,
);
}
return $item_data;
}
add_filter( 'woocommerce_get_item_data', 'filter_woocommerce_get_item_data', 10, 2 );
Ad
source: stackoverflow.com
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?
Ad