1

Once the user enters details & click on Register, we are creating an account in site successfully, along with this we are trying to create new product Programmatically as we did this successfully when we click on login button.

But for Register we have an error as below :

Fatal error: Call to a member function getName() on a non-object in

$product->setName($originalProduct->getName());

controllers :

protected function _createit()
    {
        // Clear the messages each time we call it
        Mage::getSingleton('core/session')->getMessages(true);

        if (version_compare(Mage::getVersion(),"1.9.2.2",">="))
        {
            // Only from 1.9.2.2
            if (!$this->_validateFormKey()) {
                return;
            }
        }

        $session = Mage::getSingleton('customer/session');

        if ($session->isLoggedIn()) {
            return;
        }

        $session->setEscapeMessages(true); // prevent XSS injection in user input
        if (!$this->getRequest()->isPost()) {
            return;
        }

        $result = array(
            'success' => false
        );

        $customer = $this->_getCustomer();

        try {
            $errors = $this->_getCustomerErrors($customer);

            if (empty($errors)) {
                if (version_compare(Mage::getVersion(),"1.9.1.0",">="))
                {
                    // Only from 1.9.1.0
                    $customer->cleanPasswordsValidationData();
                }
                $customer->save();
                Mage::dispatchEvent('customer_register_success',
                    array('account_controller' => $this, 'customer' => $customer)
                );
                $result['redirect'] = $this->_successProcessRegistration($customer);
                $customerId = Mage::getSingleton('customer/session')->getCustomerId();
                 $productId=$this->getRequest()->getPost('product_id');

              // code for register end                      

                    Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

                    $product = Mage::getModel('catalog/product');

                    /
                    $rand = rand(1, 9999);
                    $product->setTypeId($type) 
                        ->setAttributeSetId(17) 
                        ->setSku('CDSB1_sku' . $rand) / 
                        ->setWebsiteIDs(array(
                        1
                    ));


                    $product->setName($originalProduct->getName());

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

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

                    return $product;

                    // code for register start
            }

        } catch (Mage_Core_Exception $e) {

            $session->setCustomerFormData($this->getRequest()->getPost());
            if ($e->getCode() === Mage_Customer_Model_Customer::EXCEPTION_EMAIL_EXISTS) {
                $url = Mage::getUrl('customer/account/forgotpassword');
                $message = $this->__('There is already an account.', $url);
                $session->setEscapeMessages(false);
            } else {
                $message = $e->getMessage();
            }
            $result['error'] = $message;

        } 

        catch (Exception $e) {

           // $result['error'] = $this->__('Cannot save the customer.');
        }


         Mage::unregister('isSecureArea');
        $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));   

    }

register form :

<?php if (!Mage::getSingleton('customer/session')->isLoggedIn()): ?>
<form action="<?php echo $this->getUrl('example/amasty/createSimpleProductAndRedirect1', 
array('_secure'=>(!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS']==='on'), 
'id' => Mage::registry('current_product')->getId() )) ?>" 
method="post" id="ajaxlogin-create-form">

<?php echo $this->getChildHtml(); ?>
</form>

<?php endif; ?>
  • where is $originalProduct ? – Minesh Patel Apr 26 '17 at 12:31
  • you are getting error because there is no $originalProduct defined in your controller code – Minesh Patel Apr 26 '17 at 12:33
  • working well, please post ur answer..... – raj Apr 26 '17 at 12:37
4

Converting comment to answer.

You are getting an error because there is no $originalProduct defined in your controller code.

Just define $originalProduct and that will solve your problem.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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