Magento 1.9 - Product not being added to basket fo

2019-07-07 08:25发布

问题:

Like most Magento related issues, banging your head against the wall is often necessary to find a solution. This time however, nothing.

I am having a strange issue for customers who are logged out and trying to add items to an empty cart. It won't work for some specific product types.

I have extended the Cart Controller to add support for user inputted values instead of the standard Magento dropdown approach. This means that users could potentially input a number that doesn't yet exist yet as an associated product to my configurable (see image below).

Therefore I have implemented a system that will create the product should it not exist, then add it to the basket. This works beautifully for logged in users (and even logged out users with existing items in the basket). But it doesn't work when a logged out user has an empty cart despite it adding the success message.

My initial thought was that it was a Session related issue because users without accounts don't access the database, and also because it randomly starts working once a logged out user has items existing in the basket.

I have attempted refreshing sessions, initialising the cart, adding it twice for good measure but still nothing. Any help on this would be greatly appreciated.

Many thanks in advance.

回答1:

Thanks to Bezzie It turned out to be an emulation problem. I never knew such a thing existed. Whenever you need to do admin functionality outside the admin you must set the current store as "admin". Emulation was introduced in Magneto 1.5 for this reason. Source here.

$appEmulation = Mage::getSingleton('core/app_emulation');

//Start environment emulation of the specified store
$initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation(Mage_Core_Model_App::ADMIN_STORE_ID);

/*
 * Any code thrown here will be executed as we are currently running that store
 * with applied locale, design and similar
 */

//Stop environment emulation and restore original store
$appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);

It seems obvious when you look at it, but figuring this one out was mad! Thanks Bezzie