6

If the Registered user click on "save design" button we are creating new product.

Issue : But if guest user click on "save design" button, we are displaying pop up box to register or login. once guest user logged in, **new Product is not creating****

controllers

protected function _createProduct($type, $doSave=true, $originalProduct, $newImagePath="") 
{       
        $product = Mage::getModel('catalog/product');

        $product->setName($originalProduct->getName()); 
        $product->setDescription('what ever you need here');

        $customerId = Mage::getSingleton('customer/session')->getCustomerId();
        $product->setCreatedByCustomerId($customerId);      

        if ($doSave)
            $product->save();

        return $product;
    }

Edit

Now we can able to create new product once guest user click on button, but guest user is not logged in and we need to assign the Created product to Guest user similar to Registered user

<?php if (!Mage::getSingleton('customer/session')->isLoggedIn()): ?>
    <div id="ajaxlogin-login-window" style="display: none;">        
            <span><?php echo $this->__('Login into your Account 4') ?></span>

       <form action="<?php echo $this->getUrl('example/amasty/createSimpleProductAndRedirect', array('_secure'=>(!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS']==='on'), 'id' => Mage::registry('current_product')->getId())) ?>" id="ajaxlogin-login-form">
            // form code login/Register in pop up box
        </form>
        <script type="text/javascript">
            //<![CDATA[
            var ajaxLoginForm = new VarienForm('ajaxlogin-login-form', true);
            jQuery("#ajaxlogin-login-form").submit(function(e) {
    e.preventDefault();
    if (ajaxLoginForm.validator.validate()) {
        jQuery.ajax({
            type: "POST",
            url: jQuery('#ajaxlogin-login-form').attr('action'),
        }).done(function(result) {
            alert(result);
        }).fail(function() {
            alert('There is some error saving your design.');
        });
    }
});
            //]]>
        </script>
    </div>
<?php endif; ?>
1
  • I mean which session related code i need to take from function loginAction() & add to function _createProduct Apr 10, 2017 at 10:28

3 Answers 3

6

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.

11
  • Thanks, if you can update an answer with related code, that will be really helpfull..... Mar 28, 2017 at 7:26
  • Also Instead of redirecting to login page, we are displaying pop up box to login or register.... Mar 28, 2017 at 7:29
  • 1
    Sure.. will try to come back with code Mar 28, 2017 at 7:29
  • Is it possible to acess front end of site m Mar 28, 2017 at 7:30
  • 1
    as in Add to wishlist feature also after login , user will redirect to wishlist page, in our case is it mandatary to remove refreshing page after login to achieve what we want ? Apr 4, 2017 at 6:48
2
+100

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.

13
  • Thanks , can you please tell how i can find values of my_module_controller_action & setMyModuleCustomProductInfo , so that i will replace correct values and check..... Apr 6, 2017 at 19:42
  • If you just add the observer, you can output $observer->getEvent()->getControllerAction()->getFullActionName(). It's just your a string of your module prefix, the controller, and the action, all lowerecased. For example, if in my XML I defined blocks for my module as: <blocks><my_customModule><class>My_CustomModule_Block</class></my_customModule></blocks>, then your module prefix is: my_moduleName (the same name you'd put before the / when referencing blocks/models/helpers).
    – Brett
    Apr 6, 2017 at 19:50
  • So if I had a controller My_CustomModule_ProductController and the action is addCustomProduct then the full action name is my_custommodule_product_addcustomproduct.
    – Brett
    Apr 6, 2017 at 19:52
  • i tried your code in observer.php as echo $observer->getEvent()->getControllerAction()->getFullActionName(); , after i ran observer.php file in browser as in link , but it gave blank page.... Apr 6, 2017 at 19:57
  • we have module like this image, please check : prnt.sc/etbemv Apr 6, 2017 at 20:00
1
+50

At first, find action call after click "Save Design" Button.
It will be controller ofc.
Then rewrite (or change) function by adding similar code on sunction start:

$isLoggedIn = Mage::helper('customer')->isLoggedIn();
if (! $isLoggedIn) {
    $this->_redirect('customer/account/login');
}
1
  • Thanks, now instead of redirecting to login page, we are displaying pop up once we click on save design..... but issue is we need to save product after login..... Apr 4, 2017 at 6:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.