【问题标题】:Add product description to cart items in Woocommerce在 Woocommerce 中将产品描述添加到购物车项目
【发布时间】:2018-03-25 17:27:27
【问题描述】:

我正在努力寻找一个简单的解决方案来解决我在 Woocommerce 的 One-page-checkout 插件中遇到的问题。

我的客户想在购物车项目中的产品标题旁边添加产品描述。
关于如何操作代码以显示描述的任何想法?

这是我实际拥有的:

我认为这个插件只是将描述隐藏在某处,但我无法在代码中的任何位置找到它。

【问题讨论】:

  • 您现在使用的是哪个版本的 WooCommerce?
  • 我使用的是 4.8.2 版
  • 这是最新的 Wordpress 版本,但如果您更新了插件,您可能使用的是 WooCommerce 版本 3.2。

标签: php wordpress woocommerce checkout cart


【解决方案1】:

有两种方法可以做到(为产品和产品变体工作):

1)。将自定义函数挂在 woocommerce_get_item_data 动作钩子中(最好的方法)


add_filter( 'woocommerce_get_item_data', 'customizing_cart_item_data', 10, 2 );
function customizing_cart_item_data( $cart_data, $cart_item ) {
    $description = $cart_item['data']->get_description(); // Get the product description

    // For product variations when description is empty
    if( $cart_item['variation_id'] > 0 && empty( $description ) ){
        // Get the parent variable product object
        $parent_product = wc_get_product( $cart_item['product_id'] );
        // Get the variable product description
        $description = $parent_product->get_description();
    }

    // If product or variation description exists we display it
    if( ! empty( $description ) ){
        $cart_data[] = array(
            'key'      => __( 'Description', 'woocommerce' ),
            'value'    => $description,
            'display'  => $description,
        );
    }
    return $cart_data;
}

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

……或……

2)。在 woocommerce_cart_item_name 过滤器挂钩中挂钩的自定义函数:

WooCommerce: Display also product variation description on cart items

【讨论】:

    【解决方案2】:

    您可以在位于主题文件夹根目录的 functions.php 中尝试此代码。不确定它是否仍然有效,因为我不再活跃于 Wordpress 开发中。 [未经测试]

    更新:这可能适用于 WooCommerce 3+

    使用 woocommerce_cart_item_name 挂钩:

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

    【讨论】:

    • 这似乎不起作用.. 感谢您的尝试!
    • 我使用了 echo $cart_item['data']->get_description();它奏效了。
    猜你喜欢
    • 1970-01-01
    • 2014-11-21
    • 2016-12-26
    • 2021-08-22
    • 2018-03-03
    • 2013-03-10
    • 1970-01-01
    • 2018-06-04
    • 1970-01-01
    相关资源
    最近更新 更多