• Resolved jvdurme

    (@jvdurme)


    Hi,

    I’m using this code snippet from the woocommerce docs:

    https://docs.woocommerce.com/document/override-loop-template-and-show-quantities-next-to-add-to-cart-buttons/#

    /**
     * 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;
    }

    This works fine on the loop page, but a nasty bug is present when viewing the single product page.
    When on the single product page, I also show related products. When I increment the quantity by eg. 1 (pressing +), ALL quantities on the page change accordingly. Quantity of the product changes as well as of all the related products. It seems like they are connected.

    Would someone has a solution for this?
    Thanks!

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support con

    (@conschneider)

    Engineer

    Hi there,

    This works fine on the loop page, but a nasty bug is present when viewing the single product page.

    The code uses the woocommerce_loop_add_to_cart_link which is not present on the single product page. After all it’s purpose is to add a quantity button in the shop archive.

    When on the single product page, I also show related products. When I increment the quantity by eg. 1 (pressing +), ALL quantities on the page change accordingly. Quantity of the product changes as well as of all the related products. It seems like they are connected.

    That sounds like a Javascript conflict. Does the problem disappear when you disable the code snippet?

    If yes you could try to change if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) { to

    if ( $product && is_shop() && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) {

    Please note that we provide no warranty or support for custom code or 3rd party plugins (meaning we cannot not help you further with this code snippet or www.ads-software.com / GitHub plugins). Use them at your own risk.

    **Also: Before you do that, please do make a full backup!**

    Kind regards,

    Thread Starter jvdurme

    (@jvdurme)

    Good morning,

    Thanks for your reply!
    When is disable the snippet, I lose the quantity input altogether, so I cannot check this. Only the “add to cart” button remains visible. I did try the “qty increment plugin” at https://www.ads-software.com/plugins/qty-increment-buttons-for-woocommerce/ and that one does not have the ‘bug’, but it has awful styling in combination with my theme (oceanwp). ??

    The snippet adds the + and – buttons with nice styling (and a bit of css).

    The ‘&& is_shop()’ addition did not help.

    Perhaps I could try to put the snippet in an additional hook (for the product page). As you say, the used hook is only for the loop/archive. I didn’t think of that yet.

    Plugin Support con

    (@conschneider)

    Engineer

    Hi again,

    When is disable the snippet, I lose the quantity input altogether, so I cannot check this. Only the “add to cart” button remains visible.

    Odd ;). I can see OceanWP does declare support for WooCommerce. Maybe it is worth asking their support whether the missing quantity field is intended.

    I did try the “qty increment plugin” at https://www.ads-software.com/plugins/qty-increment-buttons-for-woocommerce/ and that one does not have the ‘bug’, but it has awful styling in combination with my theme (oceanwp). ??

    Yeah. I see. That plugin probably was not build with OceanWP in mind, too bad ^^.

    The snippet adds the + and – buttons with nice styling (and a bit of css).
    The ‘&& is_shop()’ addition did not help.
    Perhaps I could try to put the snippet in an additional hook (for the product page). As you say, the used hook is only for the loop/archive. I didn’t think of that yet.

    Yes, of course. I forgot to add that you will need to use a more common hook so that the is_shop() control statement unfolds it’s desired restriction.

    Kind regards,

    Plugin Support Gabriel – a11n

    (@gabrielfuentes)

    We haven’t heard back from you in a while, so I’m going to mark this as resolved – if you have any further questions, you can start a new thread.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Quantity buttons next to ad to cart’ is closed to new replies.