本文介绍了如果是乘积,则执行条件;否则,如果执行简单,则执行其他条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
function wooprice(){
global $product;
// 1 Get product varations
$product_variations = $product->get_available_variations();
// 2 Get one variation id of a product
$variation_product_id = $product_variations [0]['variation_id'];
// 3 Create the product object
$variation_product = new WC_Product_Variation( $variation_product_id );
if ($product->is_type( 'variable' )) :
if( $variation_product ->sale_price !=0 ) :
echo $variation_product ->sale_price;
else :
echo $variation_product ->regular_price;
endif;
else :
echo wc_price($product->get_price());
endif;
}
我正在我的主题中的price.php中使用此功能.
I am using this function in price.php in my theme.
如果产品是可变产品,则效果很好,但当产品简单时,效果会失败.
This works well if the products are variable products, but fails when the product is simple.
如果产品简单,则会引发致命错误:
It throws a fatal error if the product is simple:
Fatal error: Uncaught Error: Call to undefined method WC_Product_Simple::get_available_variations() in /home3/
我认为这需要纠正:
else :
echo wc_price($product->get_price());
推荐答案
function wooprice() {
global $product;
if ($product->is_type('variable')) :
// 1 Get product varations
$product_variations = $product->get_available_variations();
// 2 Get one variation id of a product
$variation_product_id = $product_variations [0]['variation_id'];
// 3 Create the product object
$variation_product = new WC_Product_Variation($variation_product_id);
if ($variation_product->sale_price != 0) :
echo $variation_product->sale_price;
else :
echo $variation_product->regular_price;
endif;
else :
echo $product->get_price();
endif;
}
尝试一下,我已经对您的代码进行了一些修改
Try this, I have made some modifications to your code
这篇关于如果是乘积,则执行条件;否则,如果执行简单,则执行其他条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!