Product Quantity - by Jon Filin - on WordPress WooCommerce support

This topic has 2 replies, 2 voices, and was last updated 4 years, 3 months ago ago by Olga Barlow

  • Avatar: Jon_Filin
    Jon Filin
    Participant
    November 8, 2020 at 19:19

    Hey.
    I need to write a function that would be able to set the minimum quantity of an item to add to the cart, and the step of increasing this quantity.
    I wrote it:

    // 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 ‘

    ‘;

    woocommerce_wp_text_input( array(
    ‘id’ => ‘_input_qty’,
    ‘label’ => __(“quantity”, $domain ),
    ‘placeholder’ => ”,
    ‘description’ => __(“Quantityof items in a package”, $domain ),
    ‘desc_tip’ => true,
    ) );

    woocommerce_wp_text_input( array(
    ‘id’ => ‘_step_qty’,
    ‘label’ => __(“Step”, $domain ),
    ‘placeholder’ => ”,
    ‘description’ => __(“Step of packing units when ordering”, $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;
    }

    But it doesn’t work right.
    The item card says 25, but when you click on “add” only 1 item appears in the cart.
    However, if I first press “+” or “-” on the quantity selection buttons, and only then add the product to the cart, then 25 will be added.

    1 Answer
    Avatar: Olga Barlow
    Olga Barlow
    Participant
    November 9, 2020 at 13:34

    Hello,

    Take our apologies but additional customization is not included in theme support https://themeforest.net/page/item_support_policy
    If you need help with additional customization you can submit request to WPKraken team here.

    Also, you may try third-party plugins to implement things you need, for example, check if this one suits your needs https://wordpress.org/plugins/woo-min-max-quantity-step-control-single/ , https://wordpress.org/plugins/product-quantity-for-woocommerce/

    Regards

  • Viewing 2 results - 1 through 2 (of 2 total)

The issue related to '‘Product Quantity’' has been successfully resolved, and the topic is now closed for further responses

We're using our own and third-party cookies to improve your experience and our website. Keep on browsing to accept our cookie policy.