I am looking for a solution about how to call a javascript function when an item is added to cart on shopify via ajax. Product can be added from homepage, collection-page or product-page.
I am trying with this method but not working for me. These changes i am doing within the theme files "theme.liquid, product.liquid, collection.liquid"
Everytime product go to cart alert should done.
Shopify.onItemAdded = function(line_item) {
alert('success');
};
You'll want to use CartJs... is does exactly what you need and has events etc for when things are added to cart
Related
Woocommerce is probably working properly but I would like my add to cart on every page to be like the detail/single item page ajax actions.
This is what I want:
add to cart -> top of page says successfully added to cart and update total on top. This is how the detail/single item page cart buttons work.
Instead of :
Currently they add to cart and a view cart button shows successfully on the side instead and not refreshing the total on top.
How can I accomplish this.
Thank You.
If the cart is being updated after you refresh the page, then look at running a refresh after the form submits:
onsubmit="setTimeout(function () { window.location.reload(); }, 10)"
I have got problem with Woocommerce.
When I click multiple times "add_to_cart_button" on archive-page.php AJAX stops working and redirects to product page.
First I checked the server response and its just fine. 200 OK :)
Then i looked into scripts. I found add-to-cart.js:
if ( response.error && response.product_url ) {
window.location = response.product_url;
return;
}
Great, I added simple alert:
if ( response.error && response.product_url ) {
alert(response.error);
window.location = response.product_url;
return;
}
Ok, "response.error" is equal to true when script is moving into this if... But why this if statement is being true? How can I read error status? $.fail doesn't work.
What is going on? Please help :/
The redirect to the product page is due to an error adding the product to the cart. If you have inventory, order maximums, product in cart maximums and any other limitations set this will happen. It's built in. Please look into your product setup.
I can add couple of things related to problems with WooCommerce add to cart ajax buttons:
In WooCommerce tab (from WP dashboard) => Settings => Products tab you see two checkboxes, one for enable/disable async in buttons and another for redirect to cart page directly on adding to cart a product.
Also I noticed that if product posts visibility setting is set as private will trigger error in ajax request response so will redirect to product page (if available). So watch out of this too.
I have an observer in Magento that fires whenever the customer registers a new account. What I would like to do is find out if that registration came from checkout, or from the normal registration page. What can I call from the observer to find which page the registration referral came from internally?
You could save the last X pageviews in magento registry by putting something like this into some PHP code which is executed each time.
$urlHistory = (array) Mage::getSingleton('core/session')->getMyUrlHistory();
while (is_array($urlHistory) && count($urlHistory) > 3) {
array_shift($urlHistory);
}
$urlHistory[] = Mage::helper('core/url')->getCurrentUrl();
Mage::getSingleton('core/session')->setMyUrlHistory($urlHistory);
You can then analyse Mage::getSingleton('core/session')->getMyUrlHistory() within your observer.
Has anyone had any success in Applying a custom option to the product object via Ajax.
ie. A ajax request will get triggered when ever I select a custom option which changes the product price.
I have a ajax Block in the Product View page that depends on the Product Price. Since Magento Applies the Custom Options after the product is added to the cart , I am finding it difficult to render the ajax block with my updated product price.
Any idea's ?
Try this module, its good ajax cart and it has exactly what you need and you can customize it easily
https://github.com/hws47a/VF_AjaxCart
I am new to Magento, but I thought I had a grasp on it until today. Here is my problem.
I am writing a new Observer to add a coupon to the cart on page load. The coupon code is passed in the URL and I desire the code to be passable through ANY working URL.
For example: http://magento/?coupon=MYCOUPON
I am catching on the event "controller_front_init_routers" to capture the coupon code.
I have the observer working, but if I already have an item in the cart and I pass a coupon code my cart appears empty, here is how I am adding the coupon:
public function applyCoupon($observer){
$coupon_code = $observer->getEvent()->getData('front')->getRequest()->getParam('coupon');
if(!empty($coupon_code)){
Mage::getSingleton('checkout/session')->setData('coupon_code', $coupon_code);
Mage::getSingleton('checkout/cart')->getQuote()->setCouponCode($coupon_code)->save();
Mage::log('Coupon Code: '. $coupon_code);
}
}
It seems that anytime I call Mage::getSingleton('checkout/session')->anything() I lose the session for the cart.
I thought maybe I simply needed to fetch the current cart ID and load it, but I can't seem to find a way to do that either.
Has any one had experience in this, maybe has a solution?
The problem is in event, that you are observing. Because Magento session wasn't initialized at that moment, so cookie has different name, than core one.
Use controller_action_predispatch for setting up some session data from request.