我正在努力找到一个简单的解决方案,我正在使用Woocommerce的One-page-checkout插件 .
我的客户希望在购物车项目的产品 Headers 旁边添加产品说明 .有关如何操作代码以显示描述的任何想法?
这就是我的实际情况:
我认为这个插件只是隐藏在某处的描述,但我无法在代码中的任何位置找到它 .
有两种方法可以做到这一点(为产品和产品变化做准备):
1)将自定义函数挂钩在 woocommerce_get_item_data 动作钩 (The best way) :
add_filter( 'woocommerce_get_item_data', 'customizing_cart_item_data', 10, 2 ); function customizing_cart_item_data( $cart_data, $cart_item ) { $custom_items = array(); $label = __( 'Description', 'woocommerce' ); // Get the product description $description = $cart_item['data']->get_description(); // For product variations when description is empty if( $cart_item['data']->is_type('variation') && empty( $description ) ){ // Get the parent variable product object $product = wc_get_product( $cart_item['data']->get_parent_id() ); // Get the variable product description $description = $product->get_description(); } // If product or variation description exists we display it if( ! empty( $description ) ){ $custom_items[] = array( 'key' => $label, 'display' => $description, ); } // Merging description and product variation attributes + values if( ! empty( $cart_data ) ) $custom_items = array_merge( $custom_items, $cart_data ); return $custom_items; }
代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中 .
… 要么 …
2)将自定义函数挂钩在 woocommerce_cart_item_name 过滤器钩子中:
add_filter( 'woocommerce_cart_item_name', 'customizing_cart_item_data', 10, 3); function customizing_cart_item_data( $item_name, $cart_item, $cart_item_key ) { // The label $label = __( 'Description', 'woocommerce' ); // Get the product description $description = $cart_item['data']->get_description(); // For product variations when description is empty if( $cart_item['data']->is_type('variation') && empty( $description ) ){ // Get the parent variable product object $product = wc_get_product( $cart_item['data']->get_parent_id() ); // Get the variable product description $description = $product->get_description(); } if( ! empty( $description ) ){ $item_name .= '<p class="item-description" style="margin:12px 0 0;"> <strong>'.$label.'</strong>: <br>'.$description.' </p>'; } return $item_name; }
代码在Woocommerce 3上进行测试并正常运行 .
您可以在位于主题文件夹根目录中的functions.php中尝试此代码 . 不确定它是否仍然有效,因为我不再活跃在Wordpress开发中 . [未测试]
Updated: this should probably work for WooCommerce 3+
Using woocommerce_cart_item_name hook:
add_filter( 'woocommerce_cart_item_name', 'cart_description', 20, 3); function cart_description( $name, $cart_item, $cart_item_key ) { // Get the corresponding WC_Product $product_item = $cart_item['data']; if(!empty($product_item)) { // WC 3+ compatibility $description = $product_item->get_description(); $result = __( 'Description: ', 'woocommerce' ) . $description; return $name . '<br>' . $result; } else return $name; } }
2 回答
有两种方法可以做到这一点(为产品和产品变化做准备):
1)将自定义函数挂钩在 woocommerce_get_item_data 动作钩 (The best way) :
代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中 .
… 要么 …
2)将自定义函数挂钩在 woocommerce_cart_item_name 过滤器钩子中:
代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中 .
代码在Woocommerce 3上进行测试并正常运行 .
您可以在位于主题文件夹根目录中的functions.php中尝试此代码 . 不确定它是否仍然有效,因为我不再活跃在Wordpress开发中 . [未测试]
Updated: this should probably work for WooCommerce 3+
Using woocommerce_cart_item_name hook: