Zakaria Ghazali
Zakaria Ghazali

Reputation: 57

Custom quantities in single product pages on Woocommerce

In Woocommerce, I need fixed quantity for my single products.

For example quantities :

100,300,500,1000,1200 & 1500

How to customize?

Upvotes: 1

Views: 5054

Answers (2)

magicsun
magicsun

Reputation: 11

This does not change the "add to cart" button in the shop page. Therefor when users click "add to cart" it still adds product to cart with an amount of 1, instead of defined 100, 200 or such.

Add this to your functions.php file to overwrite the add to cart button for specific product id's, so that the button links to the product page instead of adding to cart.

add_filter( 'woocommerce_loop_add_to_cart_link', 'replacing_add_to_cart_button', 10, 2 );
function replacing_add_to_cart_button( $button, $product  ) {
 $specific_ids = array(2917, 2918, 2919, 2920);
if( in_array($product->get_id(), $specific_ids) ) {
$button_text = __("View product", "woocommerce");
$button = '<a class="button" href="' . $product->get_permalink() . '">' . 
$button_text . '</a>';
}
return $button;
}

This code changes add to cart to specific button, for the product id's defined.

Oh and thanks for deleting my response. I'm not able to comment yet, because of not having enough points so I had to do it this way.

Regards, Mirna

Upvotes: 0

LoicTheAztec
LoicTheAztec

Reputation: 253784

To customize the quantities you can use the filter hook woocommerce_quantity_input_args on your single product pages.

There is 2 arguments for this hook:

  1. The returned $args argument that handle:
    • The "imput default value" with $args['input_value'] (default value is 1)
    • The "Maximun allowed value" with $args['max_value'] (default value is -1)
    • The "Minimum allowed value" with $args['min_value'] (default value is 0)
    • The "Step default value" with $args['step'] (default value is 1)
    • The "Pattern default value" with $args['pattern'] (default value is '[0-9]*')
    • The "Mode default value" with $args['inputmode'] (default value is 'numeric*')
  2. The WC_Product Object $product allowing you to target specific products.

But you should need to handle also the quantity fields in your cart page where customer can edit the product quantities of the cart items.

Note: You can't have a variable step as it is a constant numeric value.


Defined specific quantities by product (for simple products):

You can add a custom field to your products that will handle a fixed quantity (for simple products):

// Adding and displaying additional product quantity custom fields
add_action( 'woocommerce_product_options_pricing', 'additional_product_pricing_option_fields', 50 );
function additional_product_pricing_option_fields() {
    $domain = "woocommerce";
    global $post;

    echo '</div><div class="options_group pricing">';

    woocommerce_wp_text_input( array(
        'id'            => '_input_qty',
        'label'         => __("Input quantity", $domain ),
        'placeholder'   => '',
        'description'   => __("Input quantity explanation goes here…", $domain ),
        'desc_tip'      => true,
    ) );


    woocommerce_wp_text_input( array(
        'id'            => '_step_qty',
        'label'         => __("Step quantity", $domain ),
        'placeholder'   => '',
        'description'   => __("Step quantity explanation goes here…", $domain ),
        'desc_tip'      => true,
    ) );

}

// Saving product custom quantity values
add_action( 'woocommerce_admin_process_product_object', 'save_product_custom_meta_data', 100, 1 );
function save_product_custom_meta_data( $product ){
    if ( isset( $_POST['_input_qty'] ) )
        $product->update_meta_data( '_input_qty', sanitize_text_field($_POST['_input_qty']) );

    if ( isset( $_POST['_step_qty'] ) )
        $product->update_meta_data( '_step_qty', sanitize_text_field($_POST['_step_qty']) );
}

// Set product quantity field by product
add_filter( 'woocommerce_quantity_input_args', 'custom_quantity_input_args', 10, 2 );
function custom_quantity_input_args( $args, $product ) {
    if( $product->get_meta('_input_qty') ){
        $args['input_value'] = is_cart() ? $args['input_value'] : $product->get_meta('_input_qty');
        $args['min_value']   = $product->get_meta('_input_qty');
    }

    if( $product->get_meta('_step_qty') ){
        $args['step'] = $product->get_meta('_step_qty');
    }

    return $args;
}

Code goes in function.php file of your active child theme (active theme). Tested and works.

additional product custom fields


Handling Steps

The following example will start at 100 and with steps of 100:

add_filter( 'woocommerce_quantity_input_args', 'custom_quantity_input_args', 10, 2 );
function custom_quantity_input_args( $args, $product ) {
    $args['input_value'] = is_cart() ? $args['input_value'] : 100;
    $args['min_value']   = 100;
    $args['step']        = 100;

    return $args;
}

For product variations (of a variable product) you will need to use additionally:

add_filter( 'woocommerce_available_variation', 'custom_qty_available_variation_args', 10, 3 );
function custom_qty_available_variation_args( $data, $product, $variation ) {
    $data['min_qty'] = 100;

    return $data;
}

Code goes in function.php file of your active child theme (active theme). Tested and works.


Upvotes: 3

Related Questions