2017-08-03 40 views
0

我正在尝试添加一个动作,以便将单位价格添加到我的woocommerce商店中的产品中。我已经添加了下面的代码添加到functions.php向woocommerce中的钩子添加动作

add_action('woocommerce_after_shop_loop_item_title', 'add_price_per_unit', 23); 

function add_price_per_unit(){ 
?> 
    <span class="pricePerGramArchive"> 
       <?php 
       if (get_field('display_price_per_unit')) { 
        $price = $product->get_price(); 
        $unit = get_field('unit'); 
        $unit_value = get_field('unit_value'); 
        $price_per_unit = $price/$unit_value; 
        $price_per_unit_round = round($price_per_unit, 2); 
        $currency_symbol = get_woocommerce_currency_symbol(); 
        echo $unit_value.$unit." (".$price_per_unit_round." ".$currency_symbol."/".$unit.")"; 
       } ?> 
    </span> 
<?php 

return $unit_value.$unit." (".$price_per_unit_round." ".$currency_symbol."/".$unit.")"; 

} 

在前端,我可以看到跨越“pricePerGramArchive”显示出来,但它是空的,而我的产品的格式被搞砸了 - 产品展示只有一个,而不是11个产品。

我在做什么错?

回答

0

你确定$product不是空的吗?如果不试试这个:

if (get_field('display_price_per_unit', $product->get_id())) { 
    // Change price here.. 
} 
+0

嗨,感谢您的建议。我试过了你的版本,但我得到了和以前一样的结果。这里有一个链接,所以你可以看到我的意思:http://46.101.171.246/shop-en/?lang=en – quickfox

+0

我认为你有一些错误,你是否启用了wp-config.php中的wp_debug ?因为在范围内你看到一个W3总缓存信息。它看起来像你有一些语法错误,并且页脚比通常更早关闭 – justkidding96