1
私は、2つの異なる商品カテゴリの2つの最小および最大カート数量をチェックする機能を持っています.1つの最大数量は、他の商品カテゴリのカートにある数量アイテムに依存します。顧客にログインしていない場合の項目。 「削除したアイテム、元に戻す」メッセージを抑制し、「すべて」の警告が表示されるようにするにはどうすればよいですか?私は下のコードを与えており、それについての改良も高く評価されています。Woocommerceは複数のエラーメッセージ/通知を表示します
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');
}
}
}
}
CSSを使用して削除されたメッセージを削除することを管理しました。 '.woocommerce-cart .woocommerce-message {表示:なし!重要;}' – Pradeep