获取WooCommerce
最小变动价格,我是一个初学者想学习Wordpress和具有可变产品价格:获取WooCommerce
- 鳕鱼250卢比
- 在线200卢比
我想在我的主题functions.php
中使用最低价格。
我将如何获取或获取最小值?
我想在下面的代码使用方法:
function filter_gateways($gateways){
$payment_NAME = 'cod'; //
$min_variation_price = ''; <--------------- minimum variation price
global $woocommerce;
foreach ($woocommerce->cart->cart_contents as $key => $values) {
// Get the terms, i.e. category list using the ID of the product
$terms = get_the_terms($values['product_id'], 'product_cat');
// Because a product can have multiple categories, we need to iterate through the list of the products category for a match
foreach ($terms as $term) {
// 20 is the ID of the category for which we want to remove the payment gateway
if($term->term_id == $min_variation_price){
unset($gateways[$payment_NAME]);
// If you want to remove another payment gateway, add it here i.e. unset($gateways['cod']);
break;
}
break;
}
}
return $gateways;
}
要从WC_Product_Variable
对象获得的最小变化活跃价格woocommerce :
$variation_min_price = $product->get_variation_price('min');
当购物车商品最小变化价格与其产品类别ID匹配时,您似乎试图在此处取消设置'鳕鱼'支付网关。
使用WordPress的条件函数has_term()
将真正简化代码(它接受长期的ID,长期蛞蝓或项名称的任何分类(如“product_cat”这里的产品种类)。
我猜想你的函数在woocommerce_available_payment_gateways
过滤钩子钩住...
我必须让你的代码的一些变化,因为它是一个有点过时,并与一些错误:
add_filter('woocommerce_available_payment_gateways', 'conditional_payment_gateways', 10, 1);
function conditional_payment_gateways($available_gateways) {
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
// Get the WC_Product object
$product = wc_get_product($cart_item['product_id']);
// Only for variable products
if($product->is_type('variable')){
// Get the Variation "min" price
$variation_min_price = $product->get_variation_price('min');
// If the product category match with the variation 'min" price
if(has_term($variation_min_price, 'product_cat', $cart_item['product_id'])){
// Cash on delivery (cod) payment gateway
unset($available_gateways['cod']); // unset 'cod'
break; // stop the loop
}
}
}
return $available_gateways;
}
代码会出现在您的活动子主题(或主题)的function.php文件中,或者也存在于任何插件文件中。
这应该工作(但我真的不能测试它,因为它是非常具体的关于产品类别ID和最小变动价格) ...
相关答案:
Disable WooCommerce Payment methods if cart item quantity limit is reached
我认为你正在寻找:
add_filter('woocommerce_variable_sale_price_html', 'get_min_variation_price_format', 10, 2);
add_filter('woocommerce_variable_price_html', 'get_min_variation_price_format', 10, 2);
function get_min_variation_price_format($price, $product) {
$min_variation_price = $product->get_variation_regular_price('min');
return wc_price($min_variation_price);
}
如果我想添加另一个函数的最低价格,有没有办法? – Peace
谢谢先生的回答很好的解释,但我不想与产品类别匹配,我只是想如果选择最低价格只是取消鳕鱼网关 – Peace