• Hi,

    I would like to have multiple “Add to cart” buttons on my product page of one product category (only the products from that category should have this). The thing however is that I want them to all add a different quantity of the product into the shopping cart and also to display the price for that quantity before/after/inside the button. Would this be possible? And if so, what’s the best and/or simplest way to do this?

    Thanks in advance 🙂 .

    Tim

    https://wordpress.org/plugins/woocommerce/

Viewing 8 replies - 1 through 8 (of 8 total)
  • You could use html like this:

    <a href="http://mydomain.com/shop/cart/?add-to-cart=2439&quantity=5" class="mybutton">Add 5 items for $10</a>

    where 2439 is the id of the product or variation in this example. The class allows the link to be styled as a button.

    Thread Starter RoofTurkey

    (@roofturkey)

    Awesome, that’s exactly what I was looking for.
    But since it’s for about 400+ products, I was wondering if there is a way to use the product id instead of hardcoding it?

    You could write a shortcode to do it. The product id would probably be in-scope.

    Thread Starter RoofTurkey

    (@roofturkey)

    I’m not sure what you mean?
    Currently I’m editing the PHP file that generates the Add to Cart button, so I’m not doing this in the front end environment.
    Would you suggest switching to that or is it more effective to do this in the back end?

    I wouldn’t edit the php file for the Add to Cart button because it may be overwritten by WooCommerce updates. Instead you could make your own add_to_cart shortcode:

    <?php
      // goes in functions.php for child theme
      add_shortcode('my_add_to_cart', 'my_add_to_cart');
      function my_add_to_cart() {
        global $post;
        echo 'Product ID: '.get_the_id();
        // copy of add-to_cart code modified to suit
      }

    You would still need to add the shortcode to every relevant product.

    Thread Starter RoofTurkey

    (@roofturkey)

    For some reason, even when I manually type in the correct URL, it doesn’t put the product in my cart.
    Any idea what might be causing that?

    Edit: I have managed to generate the correct URL using:

    <?php $postid = get_the_ID();
            $url = get_bloginfo('url') ."/shop/cart/?add-to-cart=". $postid. "&quantity=50000"; ?>
            <a href=<?php echo "$url";?> class="single_add_to_cart_button button alt">Add 50000 items for €100</a>

    However it just leads me to the shopping cart saying it’s still empty.

    Does it bring in the post id? You may need to use global $post to bring it into scope.

    The exact url for your cart may vary depending on your permalink setting.

    Thread Starter RoofTurkey

    (@roofturkey)

    It got the correct post id yes.
    However now I get a 404 error when trying to add something to the cart, so I’m going to try and fix that first.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Multiple add to cart buttons for different quantities’ is closed to new replies.