如何在结帐时设置某个类别的最小重量要求的购买限额? [英] How to set a purchase limit at checkout with a minimum weight requirement for a certain category?
本文介绍了如何在结帐时设置某个类别的最小重量要求的购买限额?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试在结帐前设置购买限额,即:
- formaggi类别的最低重量要求'
这家商店有8个类别的产品,但目的是仅检查1个类别。
我正在使用此代码段,但不适用于该类别,
都不符合最低体重要求。
/ * PESO MINIMO CATEGORIA FORMAGGI 750GR-RAFFO 14mar2020 * /
add_action('woocommerce_check_cart_items','cldws_set_weight_requirements');
函数cldws_set_weight_requirements(){
//仅在购物车或结帐页面中运行
if(is_cart()|| is_checkout()|| is_product()&& has_term('formaggi ','product_cart')){
全球$ woocommerce;
//设置结帐前的最小重量
$ minimum_weight = 0.750;
//获取购物车中每个类别的内容总重量
$ cart_contents_weight = WC()-> cart-> cart_contents_weight;
//比较值并添加错误是购物车的总重量
if($ cart_contents_weight< $ minimum_weight){
//显示错误消息
wc_add_notice( sprintf(' Per i Formaggièrichiesto unacquisto minimo di%s gr。< / strong>'
。'< br /> Peso dei Formaggi nel carrello:%s gr',
$ minimum_weight * 1000,
$ cart_contents_weight * 1000,
get_option('woocommerce_weight_unit'),
get_permalink(wc_get_page_id('shop'))
),'错误' );
}
}
}
有人知道我在做什么错或哪里出错了吗?
解决方案
您的代码中有一些错误。
以下代码检查 formaggi类别的最小重量要求,并在代码行之间添加了解释并进行了注释
function cldws_set_weight_requirements(){
//仅在购物车上并检出页面
if(!(is_cart()|| is_checkout()))返回;
/ *设置* /
//最小权重
$ minimum_weight = 20; // 20千克
//设置术语(类别)
$ term =‘formaggi’;
/ *结束设置* /
//设置变量
$ found = false;
//总重量
$ total_weight = 0;
//遍历购物车项目
foreach(WC()-> cart-> get_cart()as $ cart_item_key => $ cart_item){
//产品id
$ product_id = $ cart_item ['product_id'];
//数量
$ product_quantity = $ cart_item [‘quantity’];
//获得体重
$ product_weight = $ cart_item [’data’]-> get_weight();
//不为空&具有特定类别
if(!empty($ product_weight)&& has_term($ term,'product_cat',$ product_id)){
//购物车包含特定类别的产品
$ found = true;
//计算
$ total_weight + = $ product_quantity * $ product_weight;
}
}
//如果总重量小于最小重量,并且购物车中有特定类别的物品
if($ total_weight< ; $ minimum_weight&& $ found){
//显示动态错误警告
wc_add_notice(sprintf(
'%s'类别的最小权重为%s,您当前具有%s',
$ term,
wc_format_weight($ minimum_weight),
wc_format_weight($ total_weight)
),'error');
//删除继续按钮,直到满足条件为止
remove_action('woocommerce_proceed_to_checkout','woocommerce_button_proceed_to_checkout',20);
}
}
add_action(‘woocommerce_check_cart_items’,’cldws_set_weight_requirements’);
I try to set a purchase limit before check out, namely:
- A minimum weight requirement for the category 'formaggi'
The shop has 8 categories of products, however, the intention is to only check on 1 category.
I'm using this snippet but it doesn't work with the category, neither for the minimum weight requirement.
/*PESO MINIMO CATEGORIA FORMAGGI 750GR - RAFFO 14mar2020*/
add_action( 'woocommerce_check_cart_items', 'cldws_set_weight_requirements' );
function cldws_set_weight_requirements() {
// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() || is_product () && has_term( ‘formaggi’, ‘product_cart’ )) {
global $woocommerce;
// Set the minimum weight before checking out
$minimum_weight = 0.750;
// Get the Cart's content total weight per categoria
$cart_contents_weight = WC()->cart->cart_contents_weight;
// Compare values and add an error is Cart's total weight
if( $cart_contents_weight < $minimum_weight ) {
// Display our error message
wc_add_notice( sprintf('<strong>Per i Formaggi è richiesto un acquisto minimo di %s gr.</strong>'
. '<br />Peso dei Formaggi nel carrello: %s gr',
$minimum_weight*1000,
$cart_contents_weight*1000,
get_option( 'woocommerce_weight_unit' ),
get_permalink( wc_get_page_id( 'shop' ) )
), 'error' );
}
}
}
Someone who knows what I am doing wrong or where it is going wrong?
解决方案
There are some mistakes in your code.
The following code checks for A minimum weight requirement for the category 'formaggi', comment with explanation added between the code lines
function cldws_set_weight_requirements() {
// Only on cart and check out pages
if( ! ( is_cart() || is_checkout() ) ) return;
/* SETTINGS */
// The minimum weight
$minimum_weight = 20; // 20 kg
// Set term (category)
$term = 'formaggi';
/* END SETTINGS */
// Set variable
$found = false;
// Total weight
$total_weight = 0;
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
// Product id
$product_id = $cart_item['product_id'];
// Quantity
$product_quantity = $cart_item['quantity'];
// Get weight
$product_weight = $cart_item['data']->get_weight();
// NOT empty & has certain category
if ( ! empty( $product_weight ) && has_term( $term, 'product_cat', $product_id ) ) {
// The shopping cart contains a product of the specific category
$found = true;
// Calculate
$total_weight += $product_quantity * $product_weight;
}
}
// If the total weight is less than the minimum and there is a item in the cart from a specific category
if( $total_weight < $minimum_weight && $found ) {
// Displays a dynamic error warning
wc_add_notice( sprintf(
'The minimum weight for the "%s" category is %s, you currently have %s',
$term,
wc_format_weight($minimum_weight),
wc_format_weight($total_weight)
), 'error' );
// Removing the proceed button, until the condition is met
remove_action( 'woocommerce_proceed_to_checkout','woocommerce_button_proceed_to_checkout', 20);
}
}
add_action( 'woocommerce_check_cart_items', 'cldws_set_weight_requirements' );
这篇关于如何在结帐时设置某个类别的最小重量要求的购买限额?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文