i rocks technology
i rocks technology

Reputation: 39

How can I change the position of quantity Field?

I have tried lots of hooks but did not able to change the position of quantity field in woo-commerce single product page. So please let me know how can I move quantity box above the 'color'. Please have a look at screenshot for more details:

screenshot:-

https://prnt.sc/t9ri39

Upvotes: 0

Views: 2728

Answers (1)

Terminator-Barbapapa
Terminator-Barbapapa

Reputation: 3126

You can create a child theme and copy the file

woocommerce/templates/single-product/add-to-cart/variation-add-to-cart-button.php

to:

themes/your-child-theme/woocommerce/single-product/add-to-cart/variation-add-to-cart-button.php.

So basically the same file structure but without the templates folder. More information about templates here: WooCommerce template structure

After you've copied the file you can remove the quantity field from it by cutting out line 17 until 29:

<?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( wp_unslash( $_POST['quantity'] ) ) : $product->get_min_purchase_quantity(), // WPCS: CSRF ok, input var ok.
    )
);

do_action( 'woocommerce_after_add_to_cart_quantity' );
?>

Then add the quantity field again before the variations form with a small code snippet:

add_action( 'woocommerce_before_variations_form', 'add_quantity_field_before_variations_form', 10 ); 
function add_quantity_field_before_variations_form() {
    global $product;

    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( wp_unslash( $_POST['quantity'] ) ) : $product->get_min_purchase_quantity(), // WPCS: CSRF ok, input var ok.
        )
    );
    do_action( 'woocommerce_after_add_to_cart_quantity' );
}

Add this code snippet to the functions.php of your child theme.

Upvotes: 1

Related Questions