我正在使用 wordpress 中的 woocommerce 插件开发购物车网站。在产品中,我有两个属性,例如尺寸和颜色。
当前产品名称和价格显示在产品列表页面 dafault 上。现在我想在产品列表页面中的价格下方添加 SIZE 属性。
我知道要添加钩子(Hook),但我不知道如何使用钩子(Hook)添加SIZE?
最佳答案
编辑:
我误会你了。这应该可以做到(在我的 woocommerce 测试页上检查过):
if (!function_exists('shop_attributes_in_loop')) {
function shop_attributes_in_loop(){
global $product;
$attributes = $product->get_attributes();
if(!empty($attributes)){
$attribute_single = array_keys($attributes);
$myArray = array();
echo '<div class="product_attributes">';
foreach ($attribute_single as $attribute => $value) {
$myArray[] = ucfirst($value);
}
echo implode(', ', $myArray).'</div>';
}
}
}
add_action('woocommerce_after_shop_loop_item', 'shop_attributes_in_loop');
这会将您的产品属性(如果存在)放入 div .product_attrributes
中。您可以轻松添加可翻译的字符串,说明这些是产品属性,例如
echo esc_html('Product attributes', 'my-theme-name') .'<div class="product_attributes">'
在第一个echo
.
关于php - 如何在 WordPress 的 woocommerce 产品列表页面添加产品属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31448709/