我有一个功能,可以检查两个不同项目类别的两个最小和最大购物车数量 - 一个最小最大数量取决于另一个项目类别的购物车中存在的数量项目 - 并且还会自动添加项目以便不登录顾客。如何禁止“删除项目,撤消”消息并确保显示“全部”警告?我已经给出了下面的代码,对此的任何改进也表示赞赏。
function check_total() {
// Only run in Cart or Checkout pages
if( is_cart() || is_checkout() ) {
global $woocommerce, $product;
$total_quantity = 0;
$total_squantity = 0; //snacks
$display_notice = 1;
$display_snotice = 1;
$i = 0;
//loop through all cart products
foreach ( $woocommerce->cart->cart_contents as $product ) {
// See if any product is from the breakfast or meals category or not
if ( has_term( 'meals', 'product_cat', $product['product_id'] ) || has_term( 'breakfast', 'product_cat', $product['product_id'] ) ) {
$total_quantity += $product['quantity'];
}
if ( has_term( 'snacks', 'product_cat', $product['product_id'] )) {
$total_squantity += $product['quantity'];
}
}
// Set up the acceptable meal totals.
$acceptable_totals = array(8, 9, 10, 11, 12, 20, 21, 22, 23, 24);
// Acceptable snacks totals
$acceptable_stotals = array(0, 1, 2, 3, 4);
foreach($acceptable_totals as $total_check) {
if ( $total_check == $total_quantity ) { $display_notice = 0; }
}
foreach($acceptable_stotals as $total_scheck) {
if ( $total_scheck == $total_squantity ) { $display_snotice = 0; }
}
foreach ( $woocommerce->cart->cart_contents as $product ) {
if ( has_term( 'meals', 'product_cat', $product['product_id'] ) || has_term( 'breakfast', 'product_cat', $product['product_id'] ) ) {
if( $display_notice == 1 && $i == 0 ) {
// Display error message
wc_print_notice( sprintf( 'Whoa, you need to order 8-12 meals (1 cooler) or 20-24 meals (2 coolers). Give it another shot!<br />', $total_quantity),'error' );
}
$i++;
}
}
//Adjust snacks to match the meal quantities
if ((($total_quantity == 8 ) || ($total_quantity == 20 ) ) && ($total_squantity > 4)){
wc_print_notice( sprintf( 'Sorry dude, your bag can hold only 4 snack items...<br />', $total_squantity),'error' );
}
else if ((($total_quantity == 9 ) || ($total_quantity == 21 ) ) && ($total_squantity > 3)){
wc_print_notice( sprintf( 'Sorry dude, your bag can hold only 3 snack items...<br />', $total_squantity),'error' );
}
else if ((($total_quantity == 10 ) || ($total_quantity == 22 ) ) && ($total_squantity > 2)){
wc_print_notice( sprintf( 'Sorry dude, your bag can hold only 2 snack items...<br />', $total_squantity),'error' );
}
else if ((($total_quantity == 11 ) || ($total_quantity == 23 ) ) && ($total_squantity > 1)){
wc_print_notice( sprintf( 'Sorry dude, your bag can hold only 1 snack item...<br />', $total_squantity),'error' );
}
else if ((($total_quantity == 12 ) || ($total_quantity == 24 ) ) && ($total_squantity > 0)){
wc_print_notice( sprintf( 'Sorry dude, your bag(s) do not have enough space for snacks...<br />', $total_squantity),'error' );
}
// set our flag to be false until we find a product in that category
$cat_check = false;
// check each cart item for cooler bags category
foreach ( $woocommerce->cart->cart_contents as $product ) {
if ( has_term( 'cooler-bags', 'product_cat', $product['product_id'] )) {
$cat_check = true;
break;
}
}
// if a product in the cart is in our category, do something
if ( $cat_check ) {
// we have the category, do what we want
}
else {
if ( is_user_logged_in() ) {
//do nothing
}
else {
// select ID
$product_id = 148;
WC()->cart->add_to_cart( $product_id );
// Display notification
wc_print_notice( __( 'We just added a cooler bag to your order as you seem to be new around here. Not new? <a href="/my-account">Click here to login</a>', 'woocommerce' ), 'notice' );
}
}
}
}
答案 0 :(得分:0)
使用CSS管理删除项目已删除邮件。 &#39; .woocommerce-cart .woocommerce-message {display:none!important;}&#39;。