2017-03-28 32 views
1

在WooCommerce,我想輸出的產品HTML的價格使用該代碼的增加20%:輸出產品的html價格提高了20%

echo $product->get_price_html(); // +20% 

我已經嘗試了很長的時間來得到它的工作但不幸的是我不能這樣做。
我該如何做到這一點?

感謝

回答

1

首先你要知道,變量$產品WC_Product類的實例,這將讓你適用於它any methods from this WC_Product class

您可以使用WC_Product method get_price()WC_Product method adjust_price()這樣:

// Get the numerical (float) price value 
$product_price = (float) $product->get_price(); 

// calculate 20% of product price 
$add_to_price = $product_price * 0.2; 

// Adjust the new displayed price 
$product->adjust_price($add_to_price); 

// Displaying the new adjusted price html 
echo $product->get_price_htm(); 

所以,現在你終於得到了HTML產品價格產量增長的20%只需要重用WC_Product get_price_html() method ...

+0

對不起跳在這裏。我想修改'get_price_html()'輸出總和:產品價格+(輸入自定義字段的字符數* 2)。上面的代碼是否也是我需要實現這一目標的方向,儘管進行了相應的修改。我已經嘗試了代碼,但它導致網站無法加載,所以我想問,在花費更多時間嘗試修改代碼以達到我自己的目的之前。提前致謝。 – Craig

+0

@Craig由於WooCommerce 3.0+'get_price_html()'現在已被棄用,您需要使用'wc_price()'或者過濾器鉤子'wc_price'。但是如果您使用的是WooCommerce 2.6.x,最好的方法是使用專用過濾器鉤子「woocommerce_get_price_html」。在StackOverFlow中有很多相關的答案。 – LoicTheAztec