Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #1026858

    How to add a quantity selecter on WooCommerce shop page, this is my functions.php.

    I’ve tried the folowing code in my functions.php:

    /**
     * Override loop template and show quantities next to add to cart buttons
     */
    add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2 );
    function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
    	if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) {
    		$html = '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
    		$html .= woocommerce_quantity_input( array(), $product, false );
    		$html .= '<button type="submit" class="button alt">' . esc_html( $product->add_to_cart_text() ) . '</button>';
    		$html .= '</form>';
    	}
    	return $html;
    }

    Functions.php:

    /**
     * Replace add to cart button in the loop.
     */
    function iconic_change_loop_add_to_cart() {
    	remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
    	add_action( 'woocommerce_after_shop_loop_item', 'iconic_template_loop_add_to_cart', 10 );
    }
    
    add_action( 'init', 'iconic_change_loop_add_to_cart', 10 );
    
    /**
     * Use single add to cart button for variable products.
     */
    function iconic_template_loop_add_to_cart() {
    	global $product;
    
    	if ( ! $product->is_type( 'variable' ) ) {
    		woocommerce_template_loop_add_to_cart();
    		return;
    	}
    
    	remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
    	add_action( 'woocommerce_single_variation', 'iconic_loop_variation_add_to_cart_button', 20 );
    
    	woocommerce_template_single_add_to_cart();
    }
    
    /**
     * Customise variable add to cart button for loop.
     *
     * Remove qty selector and simplify.
     */
    function iconic_loop_variation_add_to_cart_button() {
    	global $product;
    
    	?>
    	<div class="woocommerce-variation-add-to-cart variations_button">
    		<button type="submit" class="single_add_to_cart_button button"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>
    		<input type="hidden" name="add-to-cart" value="<?php echo absint( $product->get_id() ); ?>" />
    		<input type="hidden" name="product_id" value="<?php echo absint( $product->get_id() ); ?>" />
    		<input type="hidden" name="variation_id" class="variation_id" value="0" />
    	</div>
    	<?php
    }
    #1027330

    Hey big-webdesign,

    Unfortunately, it would require quite some time and customization of the theme to achieve this, so I am sorry to tell you that this is not covered by our support. However, if it’s really important for you to get this done, you can always hire a freelancer to do the job for you :)

    Best regards,
    Basilis

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.