【问题标题】:Change WooCommerce product quantity settings for specific product category更改特定产品类别的 WooCommerce 产品数量设置
【发布时间】:2021-05-08 12:09:37
【问题描述】:

我有这段代码可以在存档(商店)页面上添加输入数量值。

 // For implementation instructions see: https://aceplugins.com/how-to-add-a-code-snippet/

/**
 * Add quantity field on the shop page.
 */
function ace_shop_page_add_quantity_field() {

    /** @var WC_Product $product */
    $product = wc_get_product( get_the_ID() );

    if ( ! $product->is_sold_individually() && 'variable' != $product->get_type() && $product->is_purchasable() ) {
        woocommerce_quantity_input( array( 'min_value' => 1, 'max_value' => $product->backorders_allowed() ? '' : $product->get_stock_quantity() ) );
    }
}
add_action( 'woocommerce_after_shop_loop_item', 'ace_shop_page_add_quantity_field', 8 );

/**
 * Add required JavaScript.
 */
function ace_shop_page_quantity_add_to_cart_handler() {

    wc_enqueue_js( '
        $(".woocommerce .products").on("click", ".quantity input", function() {
            return false;
        });
        $(".woocommerce .products").on("change input", ".quantity .qty", function() {
            var add_to_cart_button = $(this).parents( ".product" ).find(".add_to_cart_button");

            // For AJAX add-to-cart actions
            add_to_cart_button.data("quantity", $(this).val());

            // For non-AJAX add-to-cart actions
            add_to_cart_button.attr("href", "?add-to-cart=" + add_to_cart_button.attr("data-product_id") + "&quantity=" + $(this).val());
        });

        // Trigger on Enter press
        $(".woocommerce .products").on("keypress", ".quantity .qty", function(e) {
            if ((e.which||e.keyCode) === 13) {
                $( this ).parents(".product").find(".add_to_cart_button").trigger("click");
            }
        });
    ' );

}
add_action( 'init', 'ace_shop_page_quantity_add_to_cart_handler' );

如果类别为 250 且步长值为 250 而不是 1,我还有此代码可将最小值从 1 更改为 250。

// Funktion som kollar om produkterna har en aktiv kategori vid namn 250, om produkten/produkterna har det så sätt minsta order-antal till 250, varje steg (antal produkter du vill öka att lägga till) till 250. 

add_filter("woocommerce_quantity_input_args", function($args, $product){

    if(has_term("250", "product_cat", $product->get_id())) {
        $args['min_value'] = 250;
        $args['step'] = 250; 
    } 

    return $args;

}, 10, 2);

当我将多个产品添加到购物车时,值是正确的 250x2 = 500 但是当我将 250x1 添加到购物车时,值会返回 1 而不是 250。

在单页产品页面上,无论您添加一个还是 1000 个,它都可以正常工作..

【问题讨论】:

    标签: php wordpress woocommerce product product-quantity


    【解决方案1】:

    您的代码中缺少一些部分,请改用以下内容:

    // General quantity settings
    add_filter( 'woocommerce_quantity_input_args', 'custom_quantity_input_args', 10, 2 );
    function custom_quantity_input_args( $args, $product ){
        $terms      = array(250); // (can be terms Ids, slugs or names)
        $taxonomy   = 'product_cat'; // Product category taxonomy | For product tags use 'product_tag'
        $qty_value  = 250;
        
        if( has_term( $terms, $taxonomy, $product->get_id() ) {
            if( ! is_cart() ) {
                $args['input_value'] = $qty_value; // Starting value
            }
        
            $args['min_value'] = $qty_value; // Minimum value
            $args['step']      = $qty_value; // Step value
        } 
        return $args;
    }
    
    // For Ajax add to cart button (define the min and max value)
    add_filter( 'woocommerce_loop_add_to_cart_args', 'custom_loop_add_to_cart_quantity_arg', 10, 2 );
    function custom_loop_add_to_cart_quantity_arg( $args, $product ) {
        $terms      = array(250); // (can be terms Ids, slugs or names)
        $taxonomy   = 'product_cat'; // Product category taxonomy | For product tags use 'product_tag'
        $qty_value  = 250;
        
        if( has_term( $terms, $taxonomy, $product->get_id() ) {
            $args['quantity'] = $qty_value; // Min value
        }
        return $args;
    }
    
    // For product variations (define the min value)
    add_filter( 'woocommerce_available_variation', 'custom_available_variation_min_qty', 10, 3);
    function custom_available_variation_min_qty( $data, $product, $variation ) {
        $terms      = array(250); // (can be terms Ids, slugs or names)
        $taxonomy   = 'product_cat'; // Product category taxonomy | For product tags use 'product_tag'
        $qty_value  = 250;
        
        if( has_term( $terms, $taxonomy, $product->get_id() ) {
            $args['min_qty'] = $qty_value; // Min value
        }
        return $data;
    }
    

    或者这也可以通过这种方式在一个函数中集中设置(同样的):

    // Custom general settings to be loaded
    function get_my_taxonomy_terms_qty_settings(){
        return array(
            'terms'        => array(250), // (can be terms Ids, slugs or names)
            'taxonomy'     => 'product_cat', // Product category taxonomy | For product tags use 'product_tag'
            'input_value'  => 250, // Default 1
            'min_value'    => 250, // Default 0
            'max_value'    => -1, // Deafult '-1'
            'step'         => 250, // Default 1
        );
    }
    
    
    // General quantity settings
    add_filter( 'woocommerce_quantity_input_args', 'custom_quantity_input_args', 10, 2 );
    function custom_quantity_input_args( $args, $product ){
        extract( get_my_taxonomy_terms_qty_settings() );
        
        if( has_term( $terms, $taxonomy, $product->get_id() ) {
            if( ! is_cart() ) {
                $args['input_value'] = $input_value; // Starting value
            }
        
            $args['min_value'] = $min_value; // Minimum value
            // $args['max_value'] = $max_value; // Maximum value
            $args['step']      = $step; // Step value
        }
        return $args;
    }
    
    // For Ajax add to cart button (define the min value)
    add_filter( 'woocommerce_loop_add_to_cart_args', 'custom_loop_add_to_cart_quantity_arg', 10, 2 );
    function custom_loop_add_to_cart_quantity_arg( $args, $product ) {
        extract( get_my_taxonomy_terms_qty_settings() );
        
        if( has_term( $terms, $taxonomy, $product->get_id() ) {
            $args['quantity'] = $min_value; // Min value
        }
        return $args;
    }
    
    // For product variations (define the min and max value)
    add_filter( 'woocommerce_available_variation', 'custom_available_variation_min_qty', 10, 3);
    function custom_available_variation_min_qty( $data, $product, $variation ) {
        extract( get_my_taxonomy_terms_qty_settings() );
        
        if( has_term( $terms, $taxonomy, $product->get_id() ) {
            $args['min_qty'] = $min_value; // Min value
            // $args['max_qty'] = $max_value; // Max value
        }
        return $data;
    }
    

    代码位于活动子主题(或活动主题)的functions.php 文件中。它应该可以工作。

    【讨论】:

    • 谢谢,我已经用整个 sn-p 更新了我的代码,以便为商店页面添加数量,我认为这会导致一些错误,因为您的代码似乎不起作用
    • @emmbla 我的代码在普通的 WooCommerce 网站上完美运行......请参阅 those related threads... 问题来自您在存档(商店)页面上的自定义
    • 是的,这是真的,谢谢。我刚刚从数组中的数字更改为字符串,代码就可以工作了!非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2018-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多