php - 删除 WooCommerce 3 中特定产品类别的“添加到购物车”按钮

标签 php wordpress woocommerce cart product

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

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

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

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

最佳答案

Updated: (Added compatibility for WooCommerce Product Add-ons plugin in simple products).

以下方法(针对简单和可变产品类型的已定义产品类别):

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

代码:

// 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' ) ){

关于php - 删除 WooCommerce 3 中特定产品类别的“添加到购物车”按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47614054/

相关文章:

php - Controller 之间的 codeigniter $this->load->vars

php - 带有媒体查询的 WordPress wp_enqueue 样式表

css - 如何将 css 类添加到现有的 Woocommerce/Wordpress 小部件

php - 用自定义图像替换 WooCommerce 属性标签

javascript - 从数据库填充数据时,Jquery token 输入不起作用

PHP Setters/Getters 和构造函数

php - 使用 PHP/MySQL 显示一周内发生的生日

php - 在 Contact Form 7 上使用图像作为单选按钮

php - WooCommerce 存档页面产品查询上的多个 orderby 参数

php - WooCommerce的product_type属性在产品可变时返回简单