WordPress.org

Support

Support » Plugins and Hacks » Adding Quantity to archive page add to cart

Adding Quantity to archive page add to cart

  • Hi!
    I’m trying to put the Quantity form in the Products page as in the single product page.

    If I add this

    <?php
    	 		if ( ! $product->is_sold_individually() )
    	 			woocommerce_quantity_input( array( 'min_value' => 1, 'max_value' => $product->backorders_allowed() ? '' : $product->get_stock_quantity() ) );
    	 	?>

    in the loop-shop.php template just before
    <?php do_action('woocommerce_after_shop_loop_item'); ?>

    It appears but it doesn’t work

    any tip?

    http://wordpress.org/extend/plugins/woocommerce/

Viewing 15 replies - 1 through 15 (of 30 total)
  • Roy Ho
    Member

    @splashingpixelscom

    Why do you need to manually put that field there? Does it now automatically show it for you?

    It shows up and I can add quantities but when I click the add to cart button it only adds 1 product

    Roy Ho
    Member

    @splashingpixelscom

    No, what I mean is without you having to manually add the code above, the quantity field should be there.

    From your post, it seemed to me you’re saying there is no quantity field at all until you manually added that code.

    yeah, it only appears in the single product page, but I want it to appear in the products / archive page. It doesn’t appear there in the default template.

    Roy Ho
    Member

    @splashingpixelscom

    Can you send me the url to the exact page you want quantity field to appear?

    Roy Ho
    Member

    @splashingpixelscom

    Ok try this leave the code you added and then turn off AJAX submit in your woocommerce settings and let me know if that works.

    it doesn’t work, and if I disable the AJAX thingy it refreshes everytime you add to cart

    Roy Ho
    Member

    @splashingpixelscom

    No no..I only ask you to do that to check if it is related to ajax add to cart…this is to help me analyze the issue..I am looking into this.

    ah ok, disabled again

    ah ok, disabled again

    ah ok, disabled again

    Roy Ho
    Member

    @splashingpixelscom

    Ok in order to do this via ajax, it gets quite complicated so I don’t know if you want to go down this road.

    If you look in the add to cart js file for simple products, ONLY the product ID is passed when you click on add to cart. That in itself is not enough. It would need to be modified to pass in quantity as well.

    And on top of that, you would need to modify the code that handles the ajax request.

    How familiar are you with PHP and JS? I won’t write the code for you but I can definitely guide you in the right direction as in which file needs to be edited..etc…

    oh thank you so much, I’ll take a look on those files you talk about, I’m not a PHP and JS literate but I can write some code…

    btw, I already hacked this thing in the shopo theme, maybe I can copy some function from there, will see.

    Thank you so much!

    Roy Ho
    Member

    @splashingpixelscom

    Here are some reference for you:

    woocommerce-ajax.php line 181 function woocommerce_ajax_add_to_cart()
    add-to-cart.js line 4 $(‘.add_to_cart_button’)

    So basically you’re wanting to add the quantity field and use the add to cart JS to pull the quantity value and pass that to the ajax action. In the code you can see ONLY the id is passed…you need to add the quantity and then in the woocommerce_ajax_add_to_cart function, get the quantity and pass it to the class method add_to_cart..you can even see it is default to “1” which means it is currently only adding 1 quantity…so instead, you pass in the value you got from the ajax…

    That is the bulk of it…but don’t forget to sanitize your values as you don’t want to accept any code passed in…use absint() function to restrict it to integer values.

    Perhaps I can write a plugin for this so everyone can use it…Hmmm..

Viewing 15 replies - 1 through 15 (of 30 total)
  • The topic ‘Adding Quantity to archive page add to cart’ is closed to new replies.