sim*_*ity 7 php wordpress cart variations woocommerce
我正在尝试在购物车中显示我的产品变体说明.我试过在cart.php模板中插入此代码:
if ( $_product->is_type( 'variation' ) ) {echo $_product->get_variation_description();}
Run Code Online (Sandbox Code Playgroud)
按照此文档https://docs.woocommerce.com/document/template-structure/
但它仍然没有出现.
不知道我在这里做错了什么.
任何人都可以帮忙吗?
谢谢
更新WooCommerce版本3+的兼容性
自WooCommerce 3以来,get_variation_description()现已弃用并被该WC_Product方法取代get_description().
要在购物车中 获取产品项目变体描述(过滤变化产品类型条件),您有两种可能性(可能更多......):
woocommerce_cart_item_namehook 显示变体描述,无需编辑任何模板.在这两种情况下,您都不需要在代码中使用
foreach循环,如前所述,因为它已经存在.所以代码会更紧凑.
案例1 - 使用woocommerce_cart_item_name钩子:
add_filter( 'woocommerce_cart_item_name', 'cart_variation_description', 20, 3);
function cart_variation_description( $name, $cart_item, $cart_item_key ) {
// Get the corresponding WC_Product
$product_item = $cart_item['data'];
if(!empty($product_item) && $product_item->is_type( 'variation' ) ) {
// WC 3+ compatibility
$descrition = version_compare( WC_VERSION, '3.0', '<' ) ? $product_item->get_variation_description() : $product_item->get_description();
$result = __( 'Description: ', 'woocommerce' ) . $descrition;
return $name . '<br>' . $result;
} else
return $name;
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,描述仅显示在标题和变体属性值之间.
此代码位于活动子主题(或主题)的function.php文件中,或者也可以放在任何插件文件中.
案例2 - 使用cart/cart.php模板(根据您的评论更新).
您可以选择要显示此说明的位置(2个选项):
因此,您将根据您的选择在第86行或第90行的cart.php模板上插入此代码:
// Get the WC_Product
$product_item = $cart_item['data'];
if( ! empty( $product_item ) && $product_item->is_type( 'variation' ) ) {
// WC 3+ compatibility
$description = version_compare( WC_VERSION, '3.0', '<' ) ? $product_item->get_variation_description() : $product_item->get_description();
if( ! empty( $description ) ) {
// '<br>'. could be added optionally if needed
echo __( 'Description: ', 'woocommerce' ) . $description;;
}
}
Run Code Online (Sandbox Code Playgroud)
所有代码都经过测试,功能齐全
| 归档时间: |
|
| 查看次数: |
13449 次 |
| 最近记录: |