how to add the dynamically quantity with buynow button in woocommerce - php

The buy now button on single product page as is presently, is redirecting straight to checkout after adding 1 product to the cart.
What is to be done is that the user selected quantity should ideally be fed to the buy now button dynamically and in real time (through ajax) so that the customer can select the quantity and then decide whether he wants to add the quantity to the cart and continue shopping or he wants to click buy now and add the selected quantity to the cart and being redirected straight to the checkout.
Please refer to the way system works now for better idea how this should work as currently the system works flawlessly apart from allowing to specify desired quantity.
here is the link of my website
http://www.merc-clinic.com/product/lockout-safety-valve-and-electrical-lockout-kit-large/
i am adding this code in themes/ls_new/woocommerce/single-product/add-to-cart/simple.php
<form id="qty" class="cart single-product" method="post" enctype='multipart/form-data' action="/checkout?set-cart-qty_<?php echo $product->id;?>=2">
<button id="buynow" type="submit" data-toggle="tooltip" data-placement="bottom" title="<?php echo get_option('tooltip_text');?>" class="single_product_buy_now single_add_to_cart_button button alt cart-buttton buy-now">Buy Now</button>
<input type="hidden" name="add-to-cart" value="<?php echo esc_attr( $product->id ); ?>" />

I do see you have some format errors and the form ending is missing. I don't use tooltip, but if this works then try adding your title.
Try this:
<form id="qty" class="cart single-product" enctype="multipart/form-data" method="post">
<div class="quantity"><input class="input-text qty text" title="Qty" max="94" min="1"
name="quantity" pattern="[0-9]*" size="4" step="1" type="number" value="1" />
</div>
<input name="add-to-cart" type="hidden" value="2" />
<button id="buynow" class="single_add_to_cart_button button alt"
type="submit"> Buy Now</button>
</form>

Related

Post Method only posting last value fetched from database

I am displaying list of items(multiple entries) from database on one page. I am trying to post all those values fetched from the database to another php file to add item to cart.
But when I press 'add to cart' button for any item in the list, it is only sending last item through POST. I know it is because of while loop, but I don't know correct way to implement it?
<form action="php/addToCart.php" method= "POST">
<?php
while($product=mysqli_fetch_assoc($featured)):
?>
<div class="col-md-5">
<h4><?= $product['title'] ?></h4>
<image src="php/marketplace_images/<?= $product['image'];?>" alt="<?= $product['title'];?>" height="300" width="300"/>
<p class="lprice"> Price $ <?= $product['price'];?></p>
<p class="desc"> Description: <?= $product['description'];?></p>
<p class="bname"> Brand Name: <?= $product['brandname'];?></p>
<input name="title" type="hidden" value="<?= $product['title'] ?>" />
<input name="price" type="hidden" value="<?= $product['price'] ?>" />
<input name="brandname" type="hidden" value="<?= $product['brandname'] ?>" />
<input name="image" type="hidden" value="<?= $product['image'] ?>" />
<input name="featured" type="hidden" value="<?= $product['featured'] ?>" />
<button type="submit" class="btn btn-success" data-toggle="modal" name="add">Add To Cart</button>
</div>
<?php endwhile;
?>
You're ending up with a single form with n (as many as items) fields with the same name, that are no longer separate. E.g. you'll have n fields named "title", n fields named "price" etc.
You'd never know which title belongs to which price.
Plus, the code you show only shows the rendering of the form, not what you're doing with it on submission (but that wouldn't make it better, as you can't distinguish between all the different inputs). If that code expects one title, it might take the last, or a random one.
So, if you want to send just a single item: Move your <form> and </form> into the while loop, so that you end up with a number of forms that can be individually submitted. In general I'd recommend against posting all of the data and just limit yourself to an id of an item, but that's beyond the scope of this question (but imagine what happens when I manipulate the price to 0 before submitting the item to the cart)

Quantity not working on product single Ajax Woocommerce

I have recently added a cart to Ajax in my store. Everything works except quantity. When I choose the quantity. and then click on the button "add to cart" it always adds only one product.
I don't know how to get the value from INPUT into my "add to cart" button
My single-product.php input
<form>
<input type="number" id="input" value="1" class="uneditable-input" />
</form>
My single-product.php button
<button type="submit"
data-quantity="1" data-product_id="<?php echo $product->id; ?>"
class="button alt ajax_add_to_cart add_to_cart_button product_type_simple">
<?php echo $label; ?>
</button>
I pasted this gist into my functions.php file
Any ideas?

Display item details after clicking on element

I am working on a project where we have a few items in the database that are displayed on a website and that works fine code is below:
<form method="post" action="store.php?action=add&ItemNo=<?php echo $products['ItemNo']; ?>">
<a href="product.php"><div class="products">
<img src="<?php echo $products['img'];?>" class="img-responsive"/>
<!-- this connects to the database and gets the image of item -->
<h4><?php echo $products['ItemName'];?></h4>
<h3>$ <?php echo $products['Price'];?></h3>
<input type="text" name="Quantity" class="form-control" value="1"/>
<!-- This creates an box where the default qnt is set to 1 -->
<input type="hidden" name="ItemName" value="<?php echo $products['ItemName']; ?>" />
<input type="hidden" name="Price" value="<?php echo $products['Price']; ?>" />
<input type="submit" name="addtocart" style="margin-top:5px;" class="btn btn-info"
value="Add to Cart"/>
<!-- this creates the btn that adds item to cart -->
</div>
</a>
</form>
Now I am trying to show more details about each product. All I want is when the user clicks any item they will be sent to item.php page where there will be more details about that specific item.
At this moment I got all info displaying in item.php and I am stuck.
I do not know how to word this question, maybe that is why I cannot find any help out on Google. I am new to PHP as I just started with it few weeks ago.
firstly
<a href="product.php?id=<?php echo $products['ItemNo']; ?>">
then on product.php check the url for $_GET['id'] and use that in your query.
SELECT * from foo where id=$id
but dont use the raw id or a non parametrised query unless you like being hacked

Button onClick not redirecting

SO..i have this project that is totally backwards, but basically i have to give a working form that adds a quantity of some item_ID and then redirects to the cart page of a framework i use.
Of course rather than me just skin my framework, and customize its functionality for him. This guy has some hacked up php pages that he "wrote himself" that he insists on hard coding forms into.
anyways, i have a form that submits to my cart.php in the action
<form name="orderform" method="post" action="http://s429036015.onlinehome.us/cart.php" id="orderform">
<input type="hidden" name="mode" value="add" />
<input type="hidden" name="productid" value="140" />
<input type="hidden" name="cat" value="" />
<input type="hidden" name="page" value="" />
Qty <input type="text" id="product_avail" name="amount" />
<button class="button main-button list-button add-to-cart-button" type="submit" title="" onClick="location.href='cart.php'">
<span class="button-right"><span class="button-left">Add to cart</span></span>
</button>
</form>
The above form submits properly, with the quantity entered, session stores the data and the items are in the cart just fine....however, that onclick event on the <button> doesn't work!
I've even tried this
<button class="button main-button list-button add-to-cart-button" type="submit" title="" onClick="GotoPage(); return:true;">
with this script ahead of the form
<script type="text/javascript">
function GotoPage() {
window.location = "http://s429036015.onlinehome.us/cart.php";
}
</script>
anyone have any ideas? driving me nuts...
In PHP, after you process data from form, try this:
header('location: http://s429036015.onlinehome.us/cart.php');

Virtuemart Repeat Order

The thing is that I would like to implement the following functionality to my site:
In the account settings - order details I would like to add a button that would add all the products from that very order to cart.
Now, I pretty much get it how the add to cart functionality is implemented on the flypage and browse page. I can't really get how should I pass multiple items to the add to cart function on order details page and how it'll be processed by ps_cart if an item is out of stock or is unpublished.
Here's the code of the add to cart form from browse page:
<form action="<?php echo $mm_action_url ?>index.php" method="post" name="addtocart" id="addtocart<?php echo $i ?>" class="addtocart_form" <?php if( $this->get_cfg( 'useAjaxCartActions', 1 ) && !$notify ) { echo 'onsubmit="handleAddToCart( this.id );return false;"'; } ?>>
<?php echo $ps_product_attribute->show_quantity_box($product_id,$product_id); ?><br />
<input type="submit" class="<?php echo $button_cls ?>" value="<?php echo $button_lbl ?>" title="<?php echo $button_lbl ?>" />
<input type="hidden" name="category_id" value="<?php echo #$_REQUEST['category_id'] ?>" />
<input type="hidden" name="product_id" value="<?php echo $product_id ?>" />
<input type="hidden" name="prod_id[]" value="<?php echo $product_id ?>" />
<input type="hidden" name="page" value="shop.cart" />
<input type="hidden" name="func" value="cartadd" />
<input type="hidden" name="Itemid" value="<?php echo $sess->getShopItemid() ?>" />
<input type="hidden" name="option" value="com_virtuemart" />
<input type="hidden" name="set_price[]" value="" />
<input type="hidden" name="adjust_price[]" value="" />
<input type="hidden" name="master_product[]" value="" />
</form>
I wonder if I'd need to pass all of these to ps_cart?
I don't expect from any of you a completely ready extension, it would be great if you could point me in the right direction.
Thanks!
EDIT:
I was able to set up the add to cart button, now I have to pass the needed data.
It would be great if it would work with at least one item, then I'd start thinking how to alter the code.
EDIT 2:
I was able to make it work flawlessly for one product. But how can I pass multiple Product Id's and their quantities to the add to cart function?
The current code in ps_cart already has a loop to iterate through multiple product IDs that are sent to the cart. From the look of the code there, I would simple put all of the product IDs you want to add to the cart in to prod_id array. You will notice that prod_id, set_price, adjust_price, and master_product are all arrays in the form. My guess is that you will need to put in those values for each product you want to add and ps_cart will do the rest.

Categories