Guest can't create product programtaically [ similar to wishlist ]
what happens in wish list product , when a guest is adding a product to a wish list in the preDispatch method on controller they are checking whether the customer is logged in or not. If customer is guest then they set the current product's url customer session like this
Mage::getSingleton('customer/session')->setBeforeWishlistUrl($this->_getRefererUrl());
so we can do the same here.
As i can see in the code that there you are creating a product and then setting the CreatedByCustomerId
attribute of the product. so what we can do here is set the CreatedByCustomerId
to some value and then save the product after that we can set the product id in the session and set the setBeforeAuthUrl()
to a custom controller and redirect to the login page.
After the customer has logged in , customer will be taken to our custom controller which we had set in setBeforeAuthUrl()
we can get the product id from customer session and load the product and set the CreatedByCustomerId
to customer id as customer id and then save.
The way I would solve this is to listen to the generic controller predispatch
event. I would then filter out for only my specific controller. Then check if the customer is logged in and if not, I'd store the data that you want in their session. Then let it flow normally for the login sequence. After the user logs in, I'd check to see if there is data in my session that should create the product and follow that workflow from there.
etc/config.xml
<frontend>
<events>
<controller_action_predispatch>
<observers>
<my_module>
<class>my_module/observer</class>
<method>onControllerActionPredispatch</method>
</my_module>
</observers>
</controller_action_predispatch>
</events>
</frontend>
Model/Observer.php
class Observer
{
public function onControllerActionPredispatch($observer)
{
if (
$observer->getEvent()->getControllerAction()->getFullActionName() == 'my_module_controller_action' &&
!Mage::getSingleton('customer/session')->isLoggedIn()
) {
Mage::getSingleton('customer/session')->setMyModuleCustomProductInfo(
$observer->getEvent()->getControllerAction()
->getRequest()->getPost('my_namespace')
);
}
else if (
Mage::getSingleton('customer/session')->isLoggedIn() &&
Mage::getSingleton('customer/session')->getMyModuleCustomProductInfo()
) {
// Either forward to a new action, or call your helper/model methods to do what you want.
}
}
}
In the above example, if the customer posts to your custom method and isn't logged in, the data is stored in the customer session and then they proceed to the login page (or the login form is shown). After they've logged in, if they have the custom data stored in the session, then you can either automatically create the product and add it to their cart, or forward them to an action that will do that for them.