Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 向Woocommerce中的购物车项目添加产品说明_Php_Wordpress_Woocommerce_Checkout_Cart - Fatal编程技术网

Php 向Woocommerce中的购物车项目添加产品说明

Php 向Woocommerce中的购物车项目添加产品说明,php,wordpress,woocommerce,checkout,cart,Php,Wordpress,Woocommerce,Checkout,Cart,我正在努力找到一个简单的方法来解决这个问题,我正在使用Woocommerce的一页签出插件 我的客户希望在购物车项目的产品标题旁边添加产品说明 关于如何操作代码来显示描述,有什么想法吗 这就是我实际拥有的: 我认为这个插件只是把描述隐藏在某个地方,但我在代码中的任何地方都找不到它。您可以在主题文件夹根目录下的functions.php中尝试这段代码。不确定它是否仍然有效,因为我不再积极参与Wordpress开发。[未经测试] 更新:这可能适用于WooCommerce 3+ 使用woocomme

我正在努力找到一个简单的方法来解决这个问题,我正在使用Woocommerce的一页签出插件

我的客户希望在购物车项目的产品标题旁边添加产品说明
关于如何操作代码来显示描述,有什么想法吗

这就是我实际拥有的:


我认为这个插件只是把描述隐藏在某个地方,但我在代码中的任何地方都找不到它。

您可以在主题文件夹根目录下的functions.php中尝试这段代码。不确定它是否仍然有效,因为我不再积极参与Wordpress开发。[未经测试]

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

使用woocommerce\u cart\u item\u名称挂钩:

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;
    }
}
添加过滤器('woocommerce\u cart\u item\u name','cart\u description',20,3);
功能购物车描述($name、$cart\u item、$cart\u item\u key){
//获取相应的WC_产品
$product_item=$cart_item['data'];
如果(!空($product_item)){
//wc3+兼容性
$description=$product\u item->get\u description();
$result=_uu('Description:','woocommerce')。$Description;
返回$name.“
”.$result; }否则 返回$name; } }
有两种方法(为产品和产品变体工作):

1) 。将自定义函数挂接到
woocommerce\u get\u item\u data
操作挂接(最佳方式)

代码位于活动子主题(或主题)的function.php文件或任何插件文件中

……或者

2) 。将自定义函数挂接到
woocommerce\u cart\u item\u name
过滤器挂接:


您现在使用的是什么版本的WooCommerce?我使用的是版本4.8.2这是Wordpress的最新版本,但如果您更新了插件,您可能使用的是WooCommerce版本3.2。这似乎不起作用。。谢谢你的尝试!我使用了echo$cart_item['data']->get_description();它成功了。

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