Custom quantities in single product pages on Woocommerce












1














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



For example quantities :



100,300,500,1000,1200 & 1500


How to customize?










share|improve this question
























  • you might get a faster answer on wordpress.stackexchange.com
    – pathfinder
    Nov 20 at 4:26










  • @pathfinder Sorry But Wordpress StackExchange is only for pure WordPress questions… All Plugin related questions are off topic on it. So this is the right place for Woocommerce related questions.
    – LoicTheAztec
    Nov 20 at 5:32
















1














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



For example quantities :



100,300,500,1000,1200 & 1500


How to customize?










share|improve this question
























  • you might get a faster answer on wordpress.stackexchange.com
    – pathfinder
    Nov 20 at 4:26










  • @pathfinder Sorry But Wordpress StackExchange is only for pure WordPress questions… All Plugin related questions are off topic on it. So this is the right place for Woocommerce related questions.
    – LoicTheAztec
    Nov 20 at 5:32














1












1








1







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



For example quantities :



100,300,500,1000,1200 & 1500


How to customize?










share|improve this question















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



For example quantities :



100,300,500,1000,1200 & 1500


How to customize?







php wordpress woocommerce product product-quantity






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 7:01









LoicTheAztec

84.2k136095




84.2k136095










asked Nov 20 at 3:07









Zakaria Ghazali

19126




19126












  • you might get a faster answer on wordpress.stackexchange.com
    – pathfinder
    Nov 20 at 4:26










  • @pathfinder Sorry But Wordpress StackExchange is only for pure WordPress questions… All Plugin related questions are off topic on it. So this is the right place for Woocommerce related questions.
    – LoicTheAztec
    Nov 20 at 5:32


















  • you might get a faster answer on wordpress.stackexchange.com
    – pathfinder
    Nov 20 at 4:26










  • @pathfinder Sorry But Wordpress StackExchange is only for pure WordPress questions… All Plugin related questions are off topic on it. So this is the right place for Woocommerce related questions.
    – LoicTheAztec
    Nov 20 at 5:32
















you might get a faster answer on wordpress.stackexchange.com
– pathfinder
Nov 20 at 4:26




you might get a faster answer on wordpress.stackexchange.com
– pathfinder
Nov 20 at 4:26












@pathfinder Sorry But Wordpress StackExchange is only for pure WordPress questions… All Plugin related questions are off topic on it. So this is the right place for Woocommerce related questions.
– LoicTheAztec
Nov 20 at 5:32




@pathfinder Sorry But Wordpress StackExchange is only for pure WordPress questions… All Plugin related questions are off topic on it. So this is the right place for Woocommerce related questions.
– LoicTheAztec
Nov 20 at 5:32












1 Answer
1






active

oldest

votes


















1














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.









share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385639%2fcustom-quantities-in-single-product-pages-on-woocommerce%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    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.









    share|improve this answer




























      1














      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.









      share|improve this answer


























        1












        1








        1






        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.









        share|improve this answer














        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.










        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 20 at 6:58

























        answered Nov 20 at 6:28









        LoicTheAztec

        84.2k136095




        84.2k136095






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385639%2fcustom-quantities-in-single-product-pages-on-woocommerce%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            RAC Tourist Trophy