1

We have 2 buttons in Product view page, after we click on 1st button, we creating Product Programtaically.

once click on 2nd button [ after entering email, password] we are assigning that product to that customer.

Here issue is saving "customer id" value in Attribute [ attribute id : custom_uid ] is not working.

as we create the product after click on 1st button, is there any way to update the same product on click 2nd button

protected function _createProduct($type, $doSave = true, $originalProduct, $newImagePath = "")
    {

        Mage::register('isSecureArea', true);
        $session = Mage::getSingleton('customer/session');

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

        $login = "";
        $productId = "";
        if ($this->getRequest()->isPost()) {
            $login = $this->getRequest()->getPost('login');
            $productId = $this->getRequest()->getPost('product_id');
        }

        try {
            if (isset($login) && (is_array($login) && !empty($login)) || ($login != "")) {
                $designId = Mage::getSingleton('core/session')->getData('mydesign_id');
                $session->login($login['username'], $login['password']);
                $result['redirect'] = $this->_getRefererUrl() ? $this->_getRefererUrl() : Mage::getUrl('customer/account', array(
                    '_secure' => true
                ));
                $result['success'] = true;
                $customerId = Mage::getSingleton('customer/session')->getCustomerId();
                $model = Mage::getModel('example/mydesign')->load($designId);
                if ($model->getId()) {
                    $model->setData('customer_id',$customerId);
                    $model->save();
                }

                Mage::getSingleton('core/session')->unsetData('mydesign_id');
                $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
                return $this;
            } else {
                $customerId = "";
            }        

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

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

            $customerId = Mage::getSingleton('customer/session')->getCustomerId(); 
            $product->setCustomUid($customerId);   // fetching cutomer id

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

            $fav1_id = '';
            $data = array(
                'title' => $fav1_id,
                'product_id' => $product->getId(),
                'customer_id' => $customerId

            );
            //echo '<pre/>';
            $model = Mage::getModel('example/mydesign')->setData($data);
            try {
                $fav1_id = $model->save()->getId();
                if (!$customerId) {
                    Mage::getSingleton('core/session')->setData('mydesign_id', $fav1_id);
                }

            } catch (Exception $e) {
                echo $e->getMessage();
            }
            // To save

            return $product;           

        } catch (Mage_Core_Exception $e) {
            switch ($e->getCode()) {
                case Mage_Customer_Model_Customer::EXCEPTION_EMAIL_NOT_CONFIRMED:
                    $message = Mage::helper('customer')->__('This account');
                    break;             
                default:
                    $message = $e->getMessage();
            }
            $result['error'] = $message;
            $session->setUsername($login['username']);

        } catch (Exception $e) {
            $result = "ERROR :" . $e->getMessage();

        }

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

    }
  • A product has a few other important attributes such as store_id, attribute_set_id, type_id, sku, weight, tax_class_id, stock_data, etc., which need to be defined before $product->save();. Suggest you google magento creating product programmatically to learn how to do it properly. – kiatng Apr 30 '17 at 10:35
  • @kiatng We included all those details before $product->save(); , but our requirement is to update the already created product , can you please help for this.... – user52996 May 2 '17 at 5:52
  • For existing product, you need to load the model first before updating its attributes and saving. There are a few ways to load a product, again, you can google it. – kiatng May 3 '17 at 4:25
1

In your _createProduct function

before $product->save();

add below line

$product->setStore(0);
  • I updated your code, but now after entering email id & pw and click on 2nd button, i cant able to login to the site..... – user52996 May 3 '17 at 9:27
  • @vickeycolors please check updated answer – Murtuza Zabuawala May 3 '17 at 9:44
  • updated, now i can able to login as before, but saving "customer id" value in Attribute is still not working, can you please do something..... – user52996 May 3 '17 at 9:46
  • product is created or not ?? – Murtuza Zabuawala May 3 '17 at 9:48
  • product is created fine, but i need to save the customer id value in one of the attribute..... – user52996 May 3 '17 at 9:49
0

There are two major ways to load a product, load() and loadByAttribute. Because a product can be a member of more than one store, which we can set different attributes' values for, we need to specify which store we want for the update. In your case, since the product is associated with a customer, we can get the store_id from it.

 $storeId = $customer->getStoreId();    
 $product = Mage::getModel('catalog/product')
    ->setStoreId($storeId);
 // if you know the product ID
 $product->load($productId);
 // if you know the sku
 $product->loadByAttribute($sku, 'sku');
 // you can load by another attribute, if the name of the product is unique
 $product->loadByAttribute($name, 'name');
  • Thanks , may i use this code in same function as i was using to create product : protected function _createProduct or is i need to include in different function ? – user52996 May 3 '17 at 5:22
  • i tried in same function, but i got : Fatal error : call to member function getStoreId() on a non object , please check full code – user52996 May 3 '17 at 5:35
  • Try define $customer = Mage::getSingleton('customer/session')->getCustomer() before accessing it. – kiatng May 3 '17 at 8:08
  • updated, can you please check error : prnt.sc/f3jrqn – user52996 May 3 '17 at 9:27

Your Answer

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