Viewing 12 replies - 1 through 12 (of 12 total)
  • You could make a form, something like:

    
    <form action="whatever-cart-url" method ="get">
    <input type="hidden" name="add-to-cart" value="123">
    other parameters similar
    <input type="number" name="quantity" value="1" min="1" max="10">
    <input type="submit" value="Add to cart">
    </form>
    

    Some polish needed to that.

    rahimbeyran

    (@rahimbeyran)

    you can use this snippet `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;
    }`

    Thread Starter Jonesy

    (@snapspotscouk)

    @lorro

    I think i might be able to get that to work, but how can I get it to work with a variable product.

    Should i include variation_id= in, will that work? or does it need an attribute_size=

    Also, is it possible to get it to stay on the product page rather than redirecting to the cart.

    Many thanks for your help,

    It depends how you have setup your variable products:
    Method 1: If they all have the same cost, image, variation description and SKU, and you don’t need stock control at variation level, then you can create only one variation with attributes = “Any” & “Any”, if so, you will need to set the attributes in the hidden inputs in the form to specify the variation.
    Method 2: Otherwise, you will have made a separate variation for each variation, if so, each variation will have specific attributes, so the variation id should be enough to specify the product.

    If you leave out the action=”” parameter from the form tag, any form will submit to the current page. I think the ?add-to-cart=etc will still work. Not sure, but try it.

    Ullban

    (@ullban)

    I can’t get the snippet to work (I’m a absolute beginner when it comes to computer programming). So this question refer to the <FORM>-solution above.

    What is suppose to go into the “whatever-cart-url”? Is it an actual URL (http…) or a short code like “[add_to_cart_url id=”2893″]”?

    No shortcodes here! If you want to stay on the same page, leave out the action parameter. If you want to end up on the cart page, the url could be:
    https://yoursite.com/cart/
    I can’t tell you exactly because the url depends on whether you have ssl, your domain name and your permalink settings. Navigate to your cart page and capture its url.

    If it doesn’t work, please post the url of the page which has your non-working code on it.

    Ullban

    (@ullban)

    Thank you for the quick response! I still don’t get it to work,though. When I click the button, I get a blank white page and the “url” in my browser says “about:blank?add_to_cart=123&quantity=1”

    Ullban

    (@ullban)

    Thank you for the quick response! I still don’t get it to work,though. When I click the button, I get a blank white page and the “url” in my browser says “about:blank?add_to_cart=123&quantity=1”

    When I leave out the action part, nothing happens at all. No product is added to the cart.

    The site is under construction, so it still looks like shit. But here’s the link to the page: https://temp.uniline.se/landningssida-01/

    What I try to achieve in the end is a simple landing page, showing two different products. The product info may come from the product pages, but I can do a layout “manually” if that is hard to do. What I do need is an “add to cart” button for each one of the products (so it can be paid for in my ordinary checkout), as well as the opportunity to select the quantity for each one. I have figured out how to add a button for each product, but not the quantity-part.

    In my first post, the 123 is an example number and it has to be replaced with the id of the product you are trying to add. You can get the product id by going to the products list in the dashboard – hover over a product and it will come up with #123 or whatever number it is. You will also see the product id in the url when you go to edit the product.

    The line “other parameters similar” is my shorthand for some more input type=”hidden” statements. I don’t know what these will be because it depends on the method which you have used to setup your variations. Say if you have used method 2 as described in my second post, each variation will have an id number. You can get this number by going to the product editing page > Product Data section > Variations tab, then look at the variation you’re trying to add. Say its #567, then instead of “other parameters similar” you would put:

    
    <input type="hidden" name="variation_id" value="567">
    

    When you’ve written your html snippet, make sure it is valid html by using this service:
    https://validator.w3.org/#validate_by_input
    It will flag up any syntax errors.

    Ullban

    (@ullban)

    Thank you so much for your kind help! I can now add a product to the cart, but the action part still don’t work. However, I don’t think I need it. I do want the customer to stay on the page (otherwise he want be able to add the other product, without having to go back again).

    I can see you’ve added an actual product id. You should remove the “other parameters similar” line as it is invalid html.

    Pricing with quantity

    Hi,
    I need when i click on add to cart button then price * Quantity should be added into add to cart.
    When you will scroll down you will find the Pricing section, when you click on Add to cart then per price should be multiply with the quantity and the output should be added into add to cart.
    for example: per price= $1.73 and quanity= 150, when some one click on add to cart then per price * quantity i.e $1.73*150 = 259.5

    if you want pls refer the screenshot

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘WordPress woocommerce add to cart link with quantity option’ is closed to new replies.