4

How can i add a default product to order with observer event before save the order? I want to do these while i am creating an order for costumer from admin panel. Thank you!

config.xml

<events>
    <sales_order_save_before>
        <observers>
            <library>
                <type>model</type>
                <class>library/observer</class>
                <method>AddProductSaveBefore</method>
            </library>
        </observers>
    </sales_order_save_before>
 </events>

observer.php

 <?php 
 class MyPackage_Library_Model_Observer 
 {
     public function AddProductSaveBefore($observer) 
     {
         $order = $observer->getEvent()->getOrder();
         $product = Mage::getModel('catalog/product')->load("3");
     }
 }
 ?>
1
  • need to add only one product and it is fixed product? eg. product id "1" to add every orders
    – Abdul
    Nov 18 '15 at 12:23
1

Event above is close, but it's sales_order_before_save

Adding a custom entity value in Order table

0

in config.xml

<events>
    <sales_order_save_after>
        <observers>
            <specialitem>
                <type>singleton</type>
                <class>specialitem/observer</class>
                <method>attachSpecialItem</method>
            </specialitem>
        </observers>
    </sales_order_save_after>
</events>

in Observer.php

public function attachSpecialItem(Varien_Event_Observer $obs) {

    $order = $obs->getEvent()->getOrder();
    $sku = 'SKU OF PRODUCT TO ADD DYNAMICALLY';
    $product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
    $qty = 1;

    $rowTotal = $product->getPrice();
    $order_item = Mage::getModel('sales/order_item')
            ->setStoreId($order->getStore()->getStoreId())
            ->setQuoteItemId(NULL)
            ->setQuoteParentItemId(NULL)
            ->setProductId($product->getId())
            ->setProductType($product->getTypeId())
            ->setQtyBackordered(NULL)
            ->setTotalQtyOrdered($qty)
            ->setQtyOrdered($qty)
            ->setName($product->getName())
            ->setSku($product->getSku())
            ->setPrice($product->getPrice())
            ->setBasePrice($product->getPrice())
            ->setOriginalPrice($product->getPrice())
            ->setRowTotal($rowTotal)
            ->setBaseRowTotal($rowTotal)
            ->setOrder($order);
    $order_item->save();
}
1
  • Save before not after. Please revise. You are receiving this error as the item has already been saved. May 30 '16 at 22:09
0

We are on a sales_order_save_before event. So don't make an $order_item->save(), otherwise it's break the magento process, because magento wanna make some save after your code. Just take your $order and use the model method addItem($item)

public function attachSpecialItem(Varien_Event_Observer $obs) {

    /** @var Mage_Sales_Model_Order $order */
    $order = $obs->getEvent()->getOrder();
    $sku = 'SKU OF PRODUCT TO ADD DYNAMICALLY';
    $product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
    $qty = 1;

    $rowTotal = $product->getPrice();
    $orderItem = Mage::getModel('sales/order_item')
            ->setStoreId($order->getStore()->getStoreId())
            ->setQuoteItemId(NULL)
            ->setQuoteParentItemId(NULL)
            ->setProductId($product->getId())
            ->setProductType($product->getTypeId())
            ->setQtyBackordered(NULL)
            ->setTotalQtyOrdered($qty)
            ->setQtyOrdered($qty)
            ->setName($product->getName())
            ->setSku($product->getSku())
            ->setPrice($product->getPrice())
            ->setBasePrice($product->getPrice())
            ->setOriginalPrice($product->getPrice())
            ->setRowTotal($rowTotal)
            ->setBaseRowTotal($rowTotal)
            ->setOrder($order);

            $order->addItem($orderItem);

            return $this;

}

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.