Reputation: 117
I am having a hard time with this, hoping there is an easy answer
My products are all based on teired pricing, 1-25=$5.00, 26-50=$4.00, etc. How would I go about having the quantity entered on the product page and then passed along to the add to cart page with the correct price?
Essentially this is what I am trying to achieve (taken from the Shopify forums):
Create as many variants for your product as you have different unit prices. In your case, as described above, you will need 2 variants.
Collect quantity on the product page, and from that, determine the corresponding unit price, then update 2 fields in the Add to Cart form: variant id, and quantity. (YES, you can add x items at once on the product page).
How might I achieve this with Jquery? I'm struggling with the Jquery code needed to collect the quantity and price and pass it to the cart.
Thanks!
Upvotes: 0
Views: 2702
Reputation: 5462
I think writing this code without any html and post url is impossible. You must give us the link of this page. But let me try. I think you can send manually request from a jquery file to cart. Or for more security you can send a post quantity and user to another php file. On that php file you can calculate prize and post it to cart. I am not similar with shopify but I tried my luck.
$('#buy').click(function(){
quan=$('#quantity').val();
if(quantity >= 1 && quantity <=25){
//capture the user id
$.post("cart.php", { price: "5", quantity: quan, userId : user } );
}else if(quantity >= 26 && quantity <=50){
//capture the user id
$.post("cart.php", { price: "4", quantity: quan, userId : user } );
}
});
But dont forget client side scripting is dangerous. Try to use server-sided script to calculate price from quantity. Example below.
//Javascript
$('#buy').click(function(){
quan=$('#quantity').val();
//capture the user id
$.post("cart.php", { quantity: quan, userId = user } );
});
Here is the php file to capture the variables from request and work on them.
//PHP
<?php
$user= $_POST['userId'];
$quantity = $_POST['quantity'];
if(quantity >= 1 && quantity <=25){
//Change cart. Mysql or post manually to cart php and send the price 5
}else if(quantity >= 26 && quantity <=50){
//Change cart. Mysql or post manually to cart php and send the price 4
}
?>
Upvotes: 2