【问题标题】:Disable Woocommerce add to cart button if the product is already in cart如果产品已经在购物车中,则禁用 Woocommerce 添加到购物车按钮
【发布时间】:2018-09-30 01:27:26
【问题描述】:

我正在尝试在 Woocommerce 中的“添加到购物车”按钮上设置功能,以仅允许将特定产品添加到购物车一次。

首次将特定产品添加到购物车后,需要隐藏“添加到购物车”。

在购物车中我可以有任意数量的产品 - 每个产品最多只能有 1 个。

我在做研究,发现我可以使用woocommerce_add_to_cart_validation 钩子。但不知道如何开始。

如何允许将特定产品添加到购物车一次?

【问题讨论】:

    标签: php wordpress woocommerce product hook-woocommerce


    【解决方案1】:

    如果产品在购物车中,则使用 woocommerce_is_purchasable 挂钩禁用添加到购物车按钮:

    add_filter( 'woocommerce_is_purchasable', 'disable_add_to_cart_if_product_is_in_cart', 10, 2 );
    function disable_add_to_cart_if_product_is_in_cart ( $is_purchasable, $product ){
        // Loop through cart items checking if the product is already in cart
        foreach ( WC()->cart->get_cart() as $cart_item ){
            if( $cart_item['data']->get_id() == $product->get_id() ) {
                return false;
            }
        }
        return $is_purchasable;
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经测试且有效(即使对于可变产品中的产品变体)


    原答案: 这是一个使用 woocommerce_add_to_cart_validation 钩子的示例,它可以解决问题(防止添加到购物车操作并在需要时显示自定义通知),并使用自定义实用程序函数将删除您特定定义的产品 ID 的数量字段:

    add_filter( 'woocommerce_add_to_cart_validation', 'limit_cart_items_from_category', 10, 3 );
    function limit_cart_items_from_category ( $passed, $product_id, $quantity ){
        // HERE define your product ID
        $targeted_product_id = 37;
    
        // Check quantity and display notice
        if( $quantity > 1 && $targeted_product_id == $product_id ){
            wc_add_notice( __('Only one item quantity allowed for this product', 'woocommerce' ), 'error' );
            return false;
        }
    
        // Loop through cart items checking if the product is already in cart
        foreach ( WC()->cart->get_cart() as $cart_item ){
            if( $targeted_product_id == $product_id && $cart_item['data']->get_id() == $targeted_product_id ) {
                wc_add_notice( __('This product is already in cart (only one item is allowed).', 'woocommerce' ), 'error' );
                return false;
            }
        }
        return $passed;
    }
    
    // Checking and removing quantity field for a specific product 
    add_filter( 'woocommerce_quantity_input_args', 'custom_quantity_input_args', 10, 2 );
    function custom_quantity_input_args( $args, $product ) {
        // HERE define your product ID
        $targeted_product_id = 37;
    
        if( $targeted_product_id == $product->get_id() )
            $args['min_value'] = $args['max_value'] = $args['input_value'] = 1;
    
        return $args;
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

    【讨论】:

    • 嗨,刚刚试了一下,但它不起作用。我继续添加,不受限制。
    • @maltdev 抱歉,但这是经过测试并且完美运行的……您需要在这两个函数中定义您的目标产品 ID。
    • 如何对所有产品而不是单个产品做到这一点?
    • @John 我添加了一个新的改进的更好的方法
    • @LoicTheAztec 如果您启用了“启用存档上的 AJAX 添加到购物车按钮”,有没有办法让它工作?
    猜你喜欢
    • 2021-01-31
    • 2018-11-07
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    • 1970-01-01
    • 2019-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多