php – Woocommerce:在购物车页面上显示产品变化描述

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – Woocommerce:在购物车页面上显示产品变化描述脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试在购物车中显示我的产品变体说明.我尝试在cart.PHP模板中插入此代码
if ( $_PRoduct->is_tyPE( 'VARiation' ) ) {echo $_product->get_variation_description();}

按照此文档https://docs.woocommerce.com/document/template-structure/

但它仍然没有出现.

知道在这里做错了什么.

任何人都可以帮忙吗?

谢谢

由于WooCommerce 3,get_variation_description()现已弃用,并被WC_Product方法get_description()取代.

>使用woocommerce_cart_ITem_name钩子显示变体描述,无需编辑任何模板.
>使用cart.PHP模板显示变体描述.

案例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;
}

在这种情况下,描述仅显示标题和变体属性值之间.

代码位于活动主题(或主题)的function.PHP文件中,或者也可以放在任何插件文件中.

案例2 – 使用cart / cart.PHP模板(根据您的评论更新).

您可以选择要显示此说明的位置(2个选项):

>标题
>标题和变体属性值之后.

因此,您将根据您的选择在第86行或第90行的cart.PHP模板上插入此代码

// Get the WC_Product
$product_item = $cart_item['data'];

if( ! empty( $product_item ) &amp;& $product_item->is_type( 'variation' ) ) {
    // WC 3+ compatibility
    $description = version_compare( WC_VERSION,'<' ) ? $product_item->get_variation_description() : $product_item->get_description();
    if( ! empty( $description ) ) {
        // '<br>'. Could be added optionally if needed
        echo  __( 'Description: ','woocommerce' ) . $description;;
    }
}

所有代码都经过测试,功能齐全

脚本宝典总结

以上是脚本宝典为你收集整理的php – Woocommerce:在购物车页面上显示产品变化描述全部内容,希望文章能够帮你解决php – Woocommerce:在购物车页面上显示产品变化描述所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。