Woocommerce and AJAX add to cart broken - ajax

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.

Related

Capturing Add To Cart Event

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

woocommerce add to cart ajax issue

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)"

Add to cart button redirect to empty cart page

I am using Magento 1.9 edition and building eCommerce website.
My product belongs to simple product type, which has many attributes like color, size, style, occasion and many more and in category page all the attributes shows as it is its work fine but there is one problem coming is that when i click on add to cart button it redirects to empty cart page that there is nothing selected product in shopping basket.
For me, there's nothing in the error/exception log. When I try to use the "Add to Cart" button or the "Add to Compare" link on the home page OR the product page, the only message is produced in the system log:
ERR (3): Notice: Trying to get property of non-object in .../includes/src/__default.php on line 24251
ERR (3): Notice: Trying to get property of non-object in .../includes/src/__default.php on line 24253
This is the code that's referred to in the notice:
public function getEventConfig($area, $eventName)
{
//return $this->getNode($area)->events->{$eventName};
if (!isset($this->_eventAreas[$area])) {
this->_eventAreas[$area] = $this->getNode($area)->events;
}
return $this->_eventAreas[$area]->{$eventName};
}
this -> and return $this -> are lines 24251 and 24253
I'm using Magento 1.9 CE in a multi-store environment. I get the same response in IE, Chrome & Firefox and with Default or any other template.
It doesn't seem like this is the problem, though, because I get the same notice/log entry whether I add to cart or compare from the home page or the product page. The actions work from the product page. They don't work on the home page in any scenario.
I've looked at cookies--they're being set. Sessions are being maintained. The console in Firebug shows no script errors.
So I'm really at a loss here.
The code that does not work (home page) is:
<button type="button" title="Add to Cart" class="button btn-cart" onclick="setLocation('.../checkout/cart/add/uenc/aHR0cHM6Ly93dGZvY2tlci5uZXQv/product/7125/form_key/VbcZK5UdJk7IifcW/')">
The code that does work (product page) is:
<form action=".../checkout/cart/add/uenc/aHR0cHM6Ly93dGZvY2tlci5uZXQvbnd0LXB1bWEteW91dGgtbHMtc29jY2VyLXBsYXllci1wdWxsb3Zlci1qNC0wMTEyODMxLWNpcmNhLTE5OTAtcy1yb3lhbC14bC5odG1sP19fX1NJRD1V/product/7125/form_key/DHocBeqxeGRmuboN/" method="post" id="product_addtocart_form">
So a couple of things?
It's like direct interaction with the cart works; AJAX does not.
There's apparently also some variable that's tacked on to the location once you get to the checkout/cart/add form on the product page.
Both locations start with the same string: aHR0cHM6Ly93dGZvY2tlci5uZXQ The product page version that works has some other string appended to it.
Thank you to anyone who has some advice on this!

Magento "Please specify the product's option(s)" message remains after completing add to cart after initially clicking "Add to Cart" on category page

Sequence is that customer clicks "Add to Cart" button for a configurable product on the category page. As expected, customer is taken to product page with a notification message to "Please specify the product's option(s)." All good. Problem is that once the options have been selected and the customer clicks "Add to Cart" on the product page, the "Please specify the product's option(s)" message re-appears along with a message indicating the product was successfully added to the cart. Confusing for customer, who has to go into cart to be sure they added it successfully.
I've only found the problem described in one other forum post (http://www.magentocommerce.com/boards/viewthread/298926/), but it's unanswered.
A few thoughts:
I have the "After Adding a Product Redirect to Shopping Cart" config setting for the site set to No. Really don't want customers going back and forth to the cart like that--too interruptive to shopping.
It seems to have to do with the URL, which is appended with a "?options=cart" variable both when customer first goes into product page (correct behavior) and after reloading page when successfully adding product to cart (seemingly incorrect behavior).
I've considered tweaking the code to strip the "?options=cart" part of the URL after Add to Cart is hit on the product page, but I figured I'd check with the Internet and see if I can avoid that work. Seems like I'm missing something simple (or I've stumbled upon a bug).
Not a huge problem, but definitely confusing for the end user. Any ideas?
If anyone else encounters this problem in the future, here's the fix. I know that there is a better way to overwrite core Magento php files, but the quick and dirty way is to alter the _goBack() function within app/code/core/Mage/Checkout/controllers/CartController.php. This begins at line 74, approximately.
Change the following from...
elseif (!Mage::getStoreConfig('checkout/cart/redirect_to_cart')
&& !$this->getRequest()->getParam('in_cart')
&& $backUrl = $this->_getRefererUrl()
) {
$this->getResponse()->setRedirect($backUrl);
}
...to...
elseif (!Mage::getStoreConfig('checkout/cart/redirect_to_cart')
&& !$this->getRequest()->getParam('in_cart')
&& $backUrl = $this->_getRefererUrl()
) {
if (strstr($backUrl,'?options=cart')) $backUrl = str_replace('?options=cart', '', $backUrl);
$this->getResponse()->setRedirect($backUrl);
}
Why on Earth this is not default functionality is beyond me.
Had the same problem today. I couldn't edit an order because of the "Please specify the product's option(s)." message.
I checked logs and found one product which stop the operation. Problem was in configurable product at tab Custom Options. I've deleted all of the options there and now everything is OK.

magento show "Sign up to get notified when this product is back in stock" link for simple-associated products

How can we display the "Sign up to get notified when this product is back in stock" link for associated products of a configurable product.
Say, we have a T-Shirt (a configurable product) and we sell it in 3 sizes - Medium, Large and Xtra-Large (simple - associated products).
When the Large size goes out of stock, how can we make user subscribe for it for notification when it is back in stock.
Please note I have already set this setting in admin:
System > Configuration > Catalog > Allow Alert When Product Comes Back in Stock - Yes
I noticed that the url for this alert is like (it is for one of the simple products in my store):
http://localhost/mystore/index.php/productalert/add/stock/product_id/1/[some key]/[some key],,/
Is there any way we can call this url directly, passing the product id of our associated product in question? If so, what about those keys at the end of the url?
please guide.
thanks
I added this link beside the out-of-stock associated product:
http://localhost/mysite/index.php/productalert/add/stock/product_id/[associated_product_id]/uenc/[any_key]/
For e.g.
http://localhost/mysite/index.php/productalert/add/stock/product_id/17/uenc/MTM0MzcyMDk0Mw==/
It does my job.
Only thing is, after successfully adding the alert record, it redirects to home page, instead of same page as in normal case.
This is due to [any_key], if we could somehow encrypt current page url [the any_key] just like magento does, we can have it redirect to same page also, after successful saving of alert record.
Any ideas?
Thanks
Ok I got it finally!
The uenc key [any_key] is actually base64 encoding of current url.
So with the help of https://github.com/carlo/jquery-base64, I did:
var encodedUrl = $.base64.encode(window.location);
var redirectTo = 'http://localhost/mysite/index.php/productalert/add/stock/product_id/[associated_product_id]/uenc/' + encodedUrl + '/';
Hope this helps!
The Magento way to get the url would be:
Mage::helper('productalert')->setProduct($_product)->getSaveUrl('stock')
Magento already avails the functionality to set out of stock from admin panel, unfortunately it is limited to some instances therefore we can not set this from admin panel, either way you can do it in code as above mentioned but its a manual task and can not be done again and again with links.
So i think its better to use ready made extension in case you have specific requirement to set product stock alert functionality for customer and they can subscribe for it.
Here is the well managed extension for Magento 2 which can help you to set Product alert and sign-up / subscription functionality with many more admin manageable features.
Link to Out of stock notification extension for Magento 2 - https://www.mconnectmedia.com/out-of-stock-alert-magento2.html

Resources