代码之家  ›  专栏  ›  技术社区  ›  Rose Thorn

wc\u打印\u通知在购物车和迷你购物车刷新时重复?(Woocommerce | Wordpress)

  •  0
  • Rose Thorn  · 技术社区  · 5 年前

    1076

    我的代码清除了购物车中的任何其他产品,并在将包添加到购物车时只保留包。然后,当包装在购物车中时,它将禁用所有产品的“添加到购物车”按钮。最后,当人们点击灰色的addtocart按钮时,它会改变弹出的错误消息。

    我如何确保 wc_print_notice() 在购物车页面只输出一次通知?

    // Remove conditionally cart items based on a specific product (item)
        function remove_cart_items_conditionally( $cart ) {
        // HERE define your specific product ID
        $specific_product_id = 1076; 
    
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        $cart_items  = $cart->get_cart(); // Cart items array
        $items_count = count($cart_items); // Different cart items count
    
        // Continue if cart has at least 2 different cart items
        if ( $items_count < 2 )
            return;
    
        $last_item    = end($cart_items); // Last cart item data array
        $is_last_item = false; // Initializing
    
        // Check if the specific product is the last added item
        if ( in_array($specific_product_id, array( $last_item['product_id'], $last_item['variation_id'] ) ) ) {
            $is_last_item = true;
        }
    
        // Loop through cart items
        foreach ( $cart_items as $cart_item_key => $cart_item ) {
            // Remove all others cart items when specific product ID is the last added to cart
            if ( ! in_array($specific_product_id, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) && $is_last_item ) {
                $cart->remove_cart_item( $cart_item_key );
                wc_print_notice( 'Individual resources have been removed as the full pack already contains all the resources', 'notice' );
            }
        }
    }
    add_action( 'woocommerce_before_calculate_totals', 'remove_cart_items_conditionally', 10, 1 );
    
    // Disable the add to cart button if specified product is in the cart
    function disable_add_to_cart_if_product_is_in_cart ( $is_purchasable, $product ){
    
        $specific_product_id = 1076;
    
        // Loop through cart items checking if the product is already in cart
        foreach ( WC()->cart->get_cart() as $cart_item ){
            if ( in_array($specific_product_id, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) {
                return false;
    
            }
        }
        return $is_purchasable;
    }
    add_filter( 'woocommerce_is_purchasable', 'disable_add_to_cart_if_product_is_in_cart', 10, 2 );
    
    // Change disabled add to cart button popup message to something relevant
    function customizing_product_variation_message( $translated_text, $untranslated_text, $domain )
    {
        if ( in_array($specific_product_id, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) {
            if ($untranslated_text == 'Sorry, this product is unavailable. Please choose a different combination.') {
            $translated_text = __( 'The full pack in the cart already contains this resource', $domain );
            }
        }
        return $translated_text;
    }
    add_filter( 'gettext', 'customizing_product_variation_message', 10, 3 );
    

    我的代码由以下答案组成:

    Remove conditionally Woocommerce cart items based on a specific product

    Disable Woocommerce add to cart button if the product is already in cart

    Customizing product variations message on single product pages

    0 回复  |  直到 5 年前
        1
  •  0
  •   Momin IqbalAhmed    5 年前

    您正在使用 wc_print_notice 在循环中,它将显示与循环计数相同的时间。为了克服这个问题,请尝试使用以下代码。

    替换这段代码。此代码将默认禁用通知调用。只有在执行循环并删除购物车项目时,它才会启用通知调用。

    // Loop through cart items
        foreach ( $cart_items as $cart_item_key => $cart_item ) {
            // Remove all others cart items when specific product ID is the last added to cart
            if ( ! in_array($specific_product_id, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) && $is_last_item ) {
                $cart->remove_cart_item( $cart_item_key );
                wc_print_notice( 'Individual resources have been removed as the full pack already contains all the resources', 'notice' );
            }
        }
    

    用这个密码告诉我结果。

    // Loop through cart items
        $enable_notice = false;
        foreach ( $cart_items as $cart_item_key => $cart_item ) {
            // Remove all others cart items when specific product ID is the last added to cart
            if ( ! in_array($specific_product_id, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) && $is_last_item ) {
                $cart->remove_cart_item( $cart_item_key );
                $enable_notice = true;
            }
        }
        if($enable_notice){
            wc_print_notice( 'Individual resources have been removed as the full pack already contains all the resources', 'notice' );    
        }