我正在努力寻找一个简单的解决方案来解决这个问题,我正在使用Woocommerce的一页结账插件。将产品描述添加到Woocommerce的购物车项目
我的客户希望在购物车商品的商品标题旁添加商品说明。
有关如何操作代码以显示说明的任何想法?
这是我反倒:
我认为这个插件也只是躲在某处的描述,但我不能在代码的任何地方找到它。
我正在努力寻找一个简单的解决方案来解决这个问题,我正在使用Woocommerce的一页结账插件。将产品描述添加到Woocommerce的购物车项目
我的客户希望在购物车商品的商品标题旁添加商品说明。
有关如何操作代码以显示说明的任何想法?
这是我反倒:
我认为这个插件也只是躲在某处的描述,但我不能在代码的任何地方找到它。
你可以在你的主题文件夹的根目录下的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;
}
}
,似乎没有工作..谢谢尝试,虽然! –
更新的答案可能适用。 –
有2种方式来做到这一点(做工作产品和产品变体):
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) {
$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;
}
代码放在您的活动子主题的function.php文件(或主题)或者在任何插件文件中。
代码在Woocommerce 3+上测试并正常工作。
您正在使用什么版本的WooCommerce? –
我使用的是版本4.8.2 –
这是最新的WordPress版本,但如果您更新了插件,则可能是WooCommerce版本3.2。 –