【问题标题】:Remove add to cart button for specific product categories in WooCommerce 3删除 WooCommerce 3 中特定产品类别的添加到购物车按钮
【发布时间】:2019-04-22 15:52:31
【问题描述】:

我需要为已定义产品类别的产品隐藏“添加到购物车”按钮。

我希望所填写的数量仍然可见,因为我正在使用 Yith 请求报价插件,该插件将数量用于报价系统。

目标:隐藏某个产品类别的“添加到购物车”按钮,保留数量字段。

我正在寻找一小段代码放在我的functions.php 文件中。

【问题讨论】:

    标签: php wordpress woocommerce cart product


    【解决方案1】:

    更新: (在简单产品中添加了对 WooCommerce 产品插件插件的兼容性)

    这是(针对简单和可变产品类型的已定义产品类别)的方式:

    • 可选,在存档页面上:将添加到购物车按钮替换为产品的链接按钮。
    • 在单个产品页面上:删除添加到购物车按钮(保留数量字段)

    代码:

    // function add back quantities without button (variable product)
    function add_back_quantities_variable_products(){
        global $product;
    
        ?>
        <div class="woocommerce-variation-add-to-cart variations_button">
        <?php
    
        do_action( 'woocommerce_before_add_to_cart_quantity' );
    
        woocommerce_quantity_input( array(
            'min_value'   => apply_filters( 'woocommerce_quantity_input_min', $product->get_min_purchase_quantity(), $product ),
            'max_value'   => apply_filters( 'woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product ),
            'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( $_POST['quantity'] ) : $product->get_min_purchase_quantity(),
        ) );
    
        do_action( 'woocommerce_after_add_to_cart_quantity' );
        ?>
            <input type="hidden" name="add-to-cart" value="<?php echo absint( $product->get_id() ); ?>" />
            <input type="hidden" name="product_id" value="<?php echo absint( $product->get_id() ); ?>" />
            <input type="hidden" name="variation_id" class="variation_id" value="0" />
        </div>
        <?php
    }
    
    
    // function add back quantities without button (simple product)
    function add_back_quantities_simple_products(){
        global $product;
    
        if ( ! $product->is_purchasable() ) return;
    
        echo wc_get_stock_html( $product );
    
        if ( $product->is_in_stock() ) : ?>
    
            <?php do_action( 'woocommerce_before_add_to_cart_form' ); ?>
    
            <form class="cart" method="post" enctype='multipart/form-data'>
                <?php
                    // For WooCommerce Product add-ons (Update)
                    do_action( 'woocommerce_before_add_to_cart_button' ); 
    
                    do_action( 'woocommerce_before_add_to_cart_quantity' );
    
                    woocommerce_quantity_input( array(
                        'min_value'   => apply_filters( 'woocommerce_quantity_input_min', $product->get_min_purchase_quantity(), $product ),
                        'max_value'   => apply_filters( 'woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product ),
                        'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( $_POST['quantity'] ) : $product->get_min_purchase_quantity(),
                    ) );
    
                    do_action( 'woocommerce_after_add_to_cart_quantity' );
                ?>
            </form>
    
        <?php
            do_action( 'woocommerce_after_add_to_cart_form' );
        endif;
    }
    
    
    // Replacing add to cart button and quantities by your custom button in Single product pages
    add_action( 'woocommerce_single_product_summary', 'conditionally_replacing_template_single_add_to_cart', 1, 0 );
    function conditionally_replacing_template_single_add_to_cart() {
        global $product, $post;
    
        // Set HERE your product categories in the array
        $terms = array( 't-shirts', 'gloves' );
    
        if( has_term( $terms, 'product_cat' ) ){
    
            // For variable product types
            if( $product->is_type( 'variable' ) ){
                // Removing add to cart button and quantities
                remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
    
                // Add back quantities without button
                add_action( 'woocommerce_single_variation', 'add_back_quantities_variable_products', 20 );
            }
            // For simple product types
            else if( $product->is_type( 'simple' ) )
            {
                // Removing add to cart button and quantities
                remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
    
                // Add back quantities without button
                add_action( 'woocommerce_single_product_summary', 'add_back_quantities_simple_products', 30 );
            }
        }
    }
    

    并且可选(对于档案页面)

    // Replacing the button add to cart by a link to the product in Shop and archives pages
    // For variable and simple products
    add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
    function replace_loop_add_to_cart_button( $button, $product  ) {
        // Set HERE your product categories in the array
        $terms = array( 't-shirts', 'gloves' );
    
        // Only for simple products
        if( ! $product->is_type( 'variable' ) ) return;
    
        if( has_term( $terms, 'product_cat', $product->get_id() ) ){
            $button_text = __( "View product", "woocommerce" );
            $button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
        }
        return $button;
    }
    

    代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

    此代码已在 WooCommerce 3+ 下测试并且可以工作。


    对于产品标签 - 如果您希望它与产品标签一起使用,您将替换:

    if( has_term( $terms, 'product_cat' ) ){
    

    通过

    if( has_term( $terms, 'product_tag' ) ){
    

    【讨论】:

    • 感谢您的回复。我不需要产品按钮的链接,因为存档页面上没有可见的添加到购物车按钮。客户只能在单个产品页面中看到添加到购物车。您可以简化代码以不包含它吗?谢谢!
    • 感谢您的回答和信息。它对我有用,我刚刚删除了最后一个字符串,因为我没有在商店/存档页面上看到这些按钮。我一定会在我的问题中发布我已经尝试过的代码,这样人们就不会认为我只是要求免费代码。谢谢!
    • 嘿 LoicTheAztec。您的代码完美无缺。我还有一个问题,我相信这不仅会帮助我,还会帮助其他有同样问题的人。我正在尝试使用“产品附加组件”,但是这串代码也会使产品附加组件消失。我一直在研究查看代码,但无法弄清楚为什么这会导致附加组件被删除。 “添加到购物车”按钮和产品附加组件之间必须存在某种关系。如果您愿意,我们将不胜感激任何进一步的帮助。谢谢
    • @LukeBaumann 我已经回答了你的新问题,并在这里更新了代码,以便与 WooCommerce 产品插件插件兼容。
    【解决方案2】:

    它也适用于标签。

    只需将$categories 替换为$tags 并将product_cat 替换为product_tag

    【讨论】:

      猜你喜欢
      • 2017-05-30
      • 2019-12-13
      • 2018-02-10
      • 1970-01-01
      • 2019-08-04
      • 1970-01-01
      • 2018-08-07
      • 2020-09-01
      • 1970-01-01
      相关资源
      最近更新 更多