BigCommerce - Display buy now button on product page and cart gets automatically empty - cart

Display buy now button on product page which redirects to checkout page. Cart is automatically empty. I Want just that product in the checkout cart on which "Buy Now" button is clicked.
function check()
{
console.log("hi");
$( ".dropdown-menu .previewCart .previewCartItem .mini-cart-item-actions .cart-remove" ).each(function() {
console.log("product-id",$(this).attr('data-cart-itemid'));
$.ajax({url: "/cart.php?action=remove&item="+$(this).attr('data-cart-itemid'),async: false, success: function(result){
console.log("success");
}});
});
});
}

It looks like you are calling the URL to remove a product from the cart (/cart.php?action=remove), which would explain why the cart is empty. I'd recommend using the Add-to-Cart URLs documented here:
https://developer.bigcommerce.com/api-docs/cart-and-checkout/add-to-cart-url#add-cart-url_add-specific-sku-cart
For example, this URL will add a specific URL to the cart and redirect to the cart page:
/cart.php?action=add&sku=INSERT-SKU-HERE

Related

Ajax Success to Refresh Cart Contents on Shopify Capital Theme

I have been trying to update the cart drawer using my upsell product button. I have added product variant add to cart button in the cart drawer liquid code, which is using ajax cart to add the item to cart drawer but i am not sure what to put in success of the ajax request i am making.
here's my example code, please let me know what should I put in the success so it refreshes the cart drawer contents upon success. website address is: https://sailpak.myshopify.com/
$('#cart-btn-2').click(function(){
addItemToCart2( 41162793713847, 1);
});
function addItemToCart2(variant_id, qty) {
jQuery.ajax({
type: 'POST',
url: '/cart/add.js',
data: {
id: variant_id,
quantity: qty
},
dataType: 'json',
success: // what to put here so cart contents refresh with newly added item
})
}
thanks
Some premium themes have a refresh cart object that you can call to, but most themes don't.
Try to check your theme's script if it has one, if not, then you have to get content from the cart using their API and re-render contents to the DOM.

Magento 2 Product List widgets ajax add to cart

I am clueless on how to bind product listing widgets add to cart button to ajax.
Below code is working only for category page and product detail page.
<script type="text/x-magento-init">
{
"[data-role=tocart-form], .form.map.checkout": {
"catalogAddToCart": {
"bindSubmit": true
}
}
}
</script>
Any ideas is really appreciated :)

Redirect to home page if cart is empty

I want to redirect user to home page if shopping cart is empty, is this possible to do from admin panel, if yes please guide, otherwise I have to redirect by controller overloading.
I don't think you can do this from the admin, but you could try
In app/design/frontend/default/your-theme/template/checkout/cart/noItems.phtml add (this may not be the best solution but does work)
<?php Mage::app()->getResponse()->setRedirect($this->getContinueShoppingUrl()); ?>
Create an observer (try controller_action_predispatch_checkout_cart_delete) that check if your cart is empty then redirect see to the home page
(for redirecting from observer see)
Using javascript and timer so that the user will see that there cart is empty before redirecting to home page (see time delayed redirect?) Add code below to noItems.phtml see solution #1
<script>
setTimeout(function () {
window.location.href = "<?php echo $this->getContinueShoppingUrl() ?>"; //will redirect to your blog page (an ex: blog.html)
}, 2000); //will call the function after 2 secs.
</script>
Add this to your functions.php
function cart_empty_redirect_to_shop() {
global $woocommerce;
if ( is_page('cart') and !sizeof($woocommerce->cart->cart_contents) ) {
wp_redirect( get_permalink( wc_get_page_id( 'shop' ) ) ); exit;
}
}
add_action( 'wp_head', 'cart_empty_redirect_to_shop' );
The above will redirect an empty cart to the shop page.

WordPress Woocommerce ajax submit through plugin on cart page

Created plugin, which showing form on shopping cart page after submit i am sending value to third party server.
jQuery.ajax({
type: 'POST',
url: 'http://www.yoursitename.com/wp-content/plugin/my-ajax.php',
How can i call woocommerce data on 'my-ajax.php'?
$subtotal = $woocommerce->cart->subtotal;
$shipping = $woocommerce->cart->shipping_total;
$orderTotal = $woocommerce->cart->total;
Above value i want to first call on my.ajax.php and then redirect page to shopping cart with some message etc.
Please help me i am really stuck here.
You'll need to start by using the $woocommerce global! You're on the right track.
function () {
global $woocommerce;
$subtotal = $woocommerce->cart->subtotal;
$shipping = $woocommerce->cart->shipping_total;
$orderTotal = $woocommerce->cart->total;
}

send hyperlink query string to server and get result using jquery ajax

i am making a simple site in php. I have a product page and there is a link on product page that says add to wishlist so when a user clicks on that link the product is posted to the server and the page is redirected from the backend. but I want to do it using jquery ajax so that my page is not reloaded. can somebody please provide a snippet of code on how to do that ?
$('#anchorId').click(function(){
$.ajax({
url:"foo",
data : "the query string",
...
...
success: function(result){
// success code.
}
});
return false; // prevents the default behavior of anchor click.
});
The best way to learn jQuery, is to visit the API site. (Which seems to be down at the moment)
The ajax category
Update:
$('body').on('click', 'a.foo', function(){
// What you want here.
return false;
}
This will catch any click on anchors with the foo class under <body> no matter when they were created("runtime" or with the page load) .

Resources