2017-08-03 78 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