Pea*_*ace 2 php wordpress product categories woocommerce
我是一个初学者,正在尝试学习Wordpress并且有一个价格可变的产品:
我想在我的主题中使用最低价格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;
}
Run Code Online (Sandbox Code Playgroud)
我想你正在寻找那个:
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);
}
Run Code Online (Sandbox Code Playgroud)
要从对象获得woocommerce中的最小变化有效价格WC_Product_Variable:
$variation_min_price = $product->get_variation_price('min');
Run Code Online (Sandbox Code Playgroud)
当购物车项目最小变化价格与他的产品类别ID匹配时,您似乎在尝试取消设置'cod'支付网关.
使用WordPress条件函数has_term()将真正简化您的代码(它接受任何分类的术语ID,术语slu或术语名称(对于产品类别,这里为'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;
}
Run Code Online (Sandbox Code Playgroud)
代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中.
这应该工作(但我不能真正测试它,因为它对产品类别ID和最小变化价格非常具体) ...
相关答案:
如果达到购物车商品数量限制,请禁用WooCommerce付款方式
| 归档时间: |
|
| 查看次数: |
5794 次 |
| 最近记录: |