【问题标题】:Woocommerce cart notice showing multiple timesWoocommerce 购物车通知显示多次
【发布时间】:2017-07-03 21:31:56
【问题描述】:

我有一个正常工作的功能,只是它在购物车中显示了两次通知而不是一次。该功能应用折扣并显示通知。该函数查找特定类别的项目添加总数,如果满足折扣金额,则应用折扣并显示通知。现在,即使购物车中只有一件商品,它也会显示两次通知。

我已尝试添加wc_clear_notices(),但这会清除我需要的其他通知,例如针对类别最低订单金额的最小最大通知。如果我在函数开头或任何 foreach 语句中添加wc_clear_notices(),它将在显示之前清除其他最小/最大通知。

以下是我已经看过的一些问题,但只是说使用 wc_clear_notices() 不起作用,因为其他通知已清除:

WooCommerce Notice, run only once

Show wc_add_notice only one time

这是我目前拥有的代码,可以正确折扣商品并显示通知,但显示通知两次而不是一次:

//apply discounts to foil and seal product categories
add_action( 'woocommerce_before_calculate_totals', 'cart_count_foil_seal_items',10,1);

function cart_count_foil_seal_items( $cart_object ) {
$seal_prod_tally = 0;
$foil_prod_tally = 0;
// Iterating through each item in cart
foreach ( $cart_object->get_cart() as $item_values ) {
    //  Get cart item data
    $item_id = $item_values['data']->get_id(); // Product ID
    $item_qty = $item_values['quantity']; // Item quantity

    // Getting the object
    $product = new WC_Product( $item_id );
    $prod_cat = wp_get_post_terms($product->get_id(),'product_cat',array('fields'=>'slugs'));

    //tally total
    if (in_array('seal-stickers', $prod_cat)){
        $seal_prod_tally += $item_qty;
    }else if(in_array('foil-badges', $prod_cat)){
        $foil_prod_tally += $item_qty;
    }
}

 foreach ( $cart_object->get_cart() as $item_values ) {

    //Get cart item data
    $item_id = $item_values['data']->get_id(); // Product ID
    $item_qty = $item_values['quantity']; // Item quantity

    // Getting the object
    $product = new WC_Product( $item_id );
    $prod_cat2 = wp_get_post_terms($product->get_id(),'product_cat',array('fields'=>'slugs'));        

    //apply discount to each item within category

      if (in_array('seal-stickers',$prod_cat2)){

        switch ($seal_prod_tally){
            case 20000:
                $item_values['data']->set_price(1327.01/20000);
                if(!is_checkout()){
                    wc_add_notice( 
                            sprintf( 'Quantity discount has been applied to item %s: 20,000 at $1327.01.', $item_values['data']->get_title()
                            ), 'notice' 
                    );
                }
                break;
            case 30000:
                $item_values['data']->set_price(1578.65/30000);
                if(!is_checkout()){
                    wc_add_notice( 
                            sprintf( 'Quantity discount has been applied to item %s: 30,000 at $1578.65.', $item_values['data']->get_title()
                            ), 'notice' 
                    );
                }
                break;
            case 40000:
                $item_values['data']->set_price(1853.05/40000);
                if(!is_checkout()){
                    wc_add_notice( 
                            sprintf( 'Quantity discount has been applied to item %s: 40,000 at $1853.05.', $item_values['data']->get_title()
                            ), 'notice' 
                    );
                }
                break;
            case 50000:
                $item_values['data']->set_price(2126.76/50000);
                if(!is_checkout()){
                    wc_add_notice( 
                            sprintf( 'Quantity discount has been applied to item %s: 50,000 at $2126.76.', $item_values['data']->get_title()
                            ), 'notice' 
                    );
                }
                break;
            case 60000:
                $item_values['data']->set_price(2405.98/60000);
                if(!is_checkout()){
                    wc_add_notice( 
                            sprintf( 'Quantity discount has been applied to item %s: 60,000 at $2405.98.', $item_values['data']->get_title()
                            ), 'notice' 
                    );
                }
                break;
            default:
                break;
        }        
    }else if (in_array( 'foil-badges',$prod_cat2)){
        switch ($foil_prod_tally){
            case 25000:
                $item_values['data']->set_price(5872.63/25000);
                if(!is_checkout()){
                    wc_add_notice( 
                            sprintf( 'Quantity discount has been applied to item %s: 25,000 at $5872.63.', $item_values['data']->get_title()
                            ), 'notice' 
                    );
                }
                break;
            case 50000:
                $item_values['data']->set_price(10815.47/50000);
                if(!is_checkout()){
                    wc_add_notice( 
                            sprintf( 'Quantity discount has been applied to item %s: 50,000 at $10815.47.', $item_values['data']->get_title()
                            ), 'notice' 
                    );
                }
                break;
            default:
                break;
        }            
    }      
 }

}

【问题讨论】:

  • 感谢您澄清这一点。看起来它显示的添加费用通知实际上低于购物车中的运费,这将不起作用,特别是如果我有多个通知。 :/ Screenshot of calculate fees notice
  • 看来我需要检查这个函数运行后是否应用了折扣然后显示通知?你对此有什么想法吗?也许是绑定到购物车的会话变量?
  • 如果我不清楚,我很抱歉。我没有使用您的代码示例。 add_fee() 将折扣添加到运费下方,这是行不通的。我在问除了 add_fee() 示例之外,您是否还有其他选择。

标签: php wordpress woocommerce


【解决方案1】:

当客户使用更新购物车按钮更新购物车时会导致多个通知,因此快速修复只需在客户每次单击更新购物车按钮时清除通知。

最好从源头上解决问题,即找到一种不同的方式来显示不重复的通知,但在此之前这会达到预期的效果。

//clear notices on cart update
function clear_notices_on_cart_update() { 
    wc_clear_notices();
}; 

// add the filter 
add_filter( 'woocommerce_update_cart_action_cart_updated', 'clear_notices_on_cart_update', 10, 1 ); 

【讨论】:

    【解决方案2】:

    我更喜欢使用 wc_has_notice 来查看消息是否尚未添加。

    【讨论】:

    • 考虑在你的答案背后添加一些推理
    • ` if (!wc_has_notice('You can apply coupon only for 3 products.', 'error')) { wc_add_notice(__('You can apply coupon only for 3 products.', 'woocommerce '), '错误'); } ` 您需要复制粘贴消息才能正常工作。所以使用条件运算符我们可以解决这个问题。如果有人有另一个很好的解决方案,我会很高兴知道。
    猜你喜欢
    • 2019-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-21
    • 1970-01-01
    • 2019-09-18
    • 2016-03-30
    相关资源
    最近更新 更多