Php 显示特定产品类别的购物车项目简短说明
在WooCommerce中,我试图为购物车项目中的特定类别添加产品简短描述 我发现这会将产品简短描述添加到购物车中的所有产品中,但我不知道如何将其缩小到仅显示在特定产品上:Php 显示特定产品类别的购物车项目简短说明,php,wordpress,woocommerce,checkout,categories,Php,Wordpress,Woocommerce,Checkout,Categories,在WooCommerce中,我试图为购物车项目中的特定类别添加产品简短描述 我发现这会将产品简短描述添加到购物车中的所有产品中,但我不知道如何将其缩小到仅显示在特定产品上: add_filter('woocommerce_get_item_data', 'wc_checkout_description_so_27900033', 10, 2); function wc_checkout_description_so_27900033($other_data, $cart_item) {
add_filter('woocommerce_get_item_data', 'wc_checkout_description_so_27900033', 10, 2);
function wc_checkout_description_so_27900033($other_data, $cart_item) {
$post_data = get_post($cart_item['product_id']);
echo $post_data - > post_excerpt;
return $other_data;
}
如何使此代码仅显示已定义产品类别的简短说明
感谢更新woocommerce版本3及以上版本 我已经改变并实现了一点你的代码。然后,要针对一个产品类别,您应该使用条件WordPress函数 您必须在函数中定义类别ID、段塞或名称 下面是定义产品类别术语的代码:
add_filter('woocommerce_get_item_data', 'filter_woocommerce_get_item_data', 10, 2);
function filter_woocommerce_get_item_data( $item_data, $cart_item ) {
// Define HERE your Category term IDs, Slugs or Names in the array
$categories = array('clothing', 'music');
// Product Category condition Below
if( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
if( ! ( $cart_item['variationt_id'] > 0 ) ) {
$description = $cart_item['data']->get_short_description();
} else {
$description = $cart_item['data']->get_description();
if ( ! empty( $description ) ) {
$parent_product = wc_get_product( $cart_item['product_id'] );
$description = $parent_product->get_short_description();
}
}
if ( ! empty( $description ) ) {
$item_data[] = array(
'key' => __( 'Product description', 'woocommerce' ),
'value' => $description,
'display' => $description,
);
}
}
return $item_data;
}
代码位于活动子主题(或主题)的functions.php文件或任何插件文件中
代码经过测试并正常工作。谢谢Loic,我感谢您在这方面的帮助!