Thursday, October 15, 2015

Update record with condition in custom module in magento

I have create a Model and for some development i had to implement Update method. It is very easy. I also try for conditional update. But i am using the following code which will fulfill your requirement for conditional update.



foreach($cart as $item_id=>$item){
        $olddata_id = Mage::getModel('custom/csaabandonedcart')->getCollection()
                ->addFieldToFilter('csa_id', Mage::getSingleton('customer/session')->getCustomerId())
                 ->addFieldToFilter('customer_id', $retailer)->addFieldToFilter('product_id', $item_id)
                ->getColumnValues('id');
                                                                                               
     $abandonedcart = Mage::getModel('custom/csaabandonedcart')->load($olddata_id[0]);
            $abandonedcart->setQuantity($item['qty']);
             $abandonedcart->save();           
 }
 

Delete record with condition in custom module in magento



I have create a Model and for some development i had to implement delete method. It is very easy. I also try for conditional delete. But i am using the following code which will fulfill your requirement for conditional delete.

foreach($pid as $spid){
         $items = Mage::getModel('custom/csaabandonedcart')->getCollection()
        ->addFieldToFilter('csa_id', Mage::getSingleton('customer/session')->getCustomerId())
        ->addFieldToFilter('customer_id', $retailer)->addFieldToFilter('product_id', $spid);
         
 foreach($items as $item){
        $item->delete();
    }
                                               
 }

Creating Magento order programmatically

If you’re working with Magento , most probably you’ll face a situation when you need to create orders programmatically, It’s not relevant to create orders (or customers) using the Magento interface, as you can do it programmatically, which takes less time and effort.
This action can be of great help if you need to create a number of orders quickly to test your store features.
Now, in this article I’ll explain now to create Magento orders programmatically, also adding information on creating customers programmatically, as these two actions are closely connected.

 $order_list = array('32'=>array('12'=>3));
//here 32 is my customer id, 12 is my product id, 3 is my QTY for that product.


foreach($order_list as $retailer_id=>$retailer_order){
                                                 $customer = Mage::getModel('customer/customer')->load($retailer_id);
                                                 $storeId = $customer->getStoreId();
                                                 $quote = Mage::getModel('sales/quote')->setStoreId($storeId);
                                                 $quote->assignCustomer($customer);
                                                 foreach($retailer_order as $item_id=>$item_qty){
                                                                 $product = Mage::getModel('catalog/product')->load($item_id);
                                                                 $params = array();
                                                                 $params['qty'] = $item_qty;
                                                                 $request = new Varien_Object();
                                                                 $request->setData($params);
                                                                 $quote->addProduct($product, $request);
                                                 }
                                                 $customerAddressId = $customer->getDefaultBilling();
                                                 $address = Mage::getModel('customer/address')->load($customerAddressId);
                                                 $addressData = array (
                                                                                'prefix' => $address->getPrefix(),
                                                                                'firstname' => $address->getFirstname(),
                                                                                'lastname' => $address->getLastname(),
                                                                                'street' => $address->getStreet(),
                                                                                'city' => $address->getCity(),
                                                                                'region_id' => $address->getRegionId(),
                                                                                'region' => $address->getRegion(),
                                                                                'postcode' => $address->getPostcode(),
                                                                                'country_id' => $address->getCountryId(),
                                                                                'telephone' => $address->getTelephone(),
                                                                                'email' => $address->getEmail()
                                                );
                                                $billingAddress = $quote->getBillingAddress()->addData($addressData);
                                                $shippingAddress = $quote->getShippingAddress()->addData($addressData);
                                               
                                                $shippingAddress->setFreeShipping( true )
                                                                ->setCollectShippingRates(true)->collectShippingRates()
                                                                ->setShippingMethod('freeshipping_freeshipping')
                                                                ->setPaymentMethod('checkmo');


                                                $quote->getPayment()->importData(array('method' => 'checkmo'));
                                                $quote->collectTotals()->save();
                                                $service = Mage::getModel('sales/service_quote', $quote);
                                                $service->submitAll();
                                                $order = $service->getOrder();
                                                                                                 
                                }