2

Im trying to programmatically create an order. I created a model with the following structure:

<?php
class Pricebinc_App_Model_OrderCreate extends Mage_Core_Model_Abstract
{
    const CUSTOMER_RANDOM = null;
    protected $_shippingMethod = 'freeshipping_freeshipping';
    protected $_paymentMethod = 'cashondelivery';
    protected $_customer = self::CUSTOMER_RANDOM;
    protected $_subTotal = 0;
    protected $_order;
    protected $_storeId;

    public function setShippingMethod($methodName)
    {
        $this->_shippingMethod = $methodName;
    }

    public function setPaymentMethod($methodName)
    {
        $this->_paymentMethod = $methodName;
    }

    public function setCustomer($customer)
    {
        if ($customer instanceof Mage_Customer_Model_Customer) {
            $this->_customer = $customer;
        }
        if (is_numeric($customer)) {
            $this->_customer = Mage::getModel('customer/customer')->load($customer);
        } else if ($customer === self::CUSTOMER_RANDOM) {
            $customers = Mage::getResourceModel('customer/customer_collection');

            $customers
                ->getSelect()
                ->limit(1)
                ->order('RAND()');

            $id = $customers->getFirstItem()->getId();

            $this->_customer = Mage::getModel('customer/customer')->load($id);
        }
    }

    public function createOrder($products)
    {
        if (!($this->_customer instanceof Mage_Customer_Model_Customer)) {
            $this->setCustomer(self::CUSTOMER_RANDOM);
        }

        $transaction = Mage::getModel('core/resource_transaction');
        $this->_storeId = Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);
        $reservedOrderId = Mage::getSingleton('eav/config')
            ->getEntityType('order')
            ->fetchNewIncrementId($this->_storeId);

        $currencyCode = Mage::app()->getBaseCurrencyCode();
        $this->_order = Mage::getModel('sales/order')
            ->setIncrementId($reservedOrderId)
            ->setStoreId($this->_storeId)
            ->setQuoteId(0)
            ->setGlobalCurrencyCode($currencyCode)
            ->setBaseCurrencyCode($currencyCode)
            ->setStoreCurrencyCode($currencyCode)
            ->setOrderCurrencyCode($currencyCode);


        $this->_order->setCustomerEmail($this->_customer->getEmail())
            ->setCustomerFirstname($this->_customer->getFirstname())
            ->setCustomerLastname($this->_customer->getLastname())
            ->setCustomerGroupId($this->_customer->getGroupId())
            ->setCustomerIsGuest(0)
            ->setCustomer($this->_customer);


        $billing = $this->_customer->getDefaultBillingAddress();
        $billingAddress = Mage::getModel('sales/order_address')
            ->setStoreId($this->_storeId)
            ->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_BILLING)
            ->setCustomerId($this->_customer->getId())
            ->setCustomerAddressId($this->_customer->getDefaultBilling())
            ->setCustomerAddress_id($billing->getEntityId())
            ->setPrefix($billing->getPrefix())
            ->setFirstname($billing->getFirstname())
            ->setMiddlename($billing->getMiddlename())
            ->setLastname($billing->getLastname())
            ->setSuffix($billing->getSuffix())
            ->setCompany($billing->getCompany())
            ->setStreet($billing->getStreet())
            ->setCity($billing->getCity())
            ->setCountry_id($billing->getCountryId())
            ->setRegion($billing->getRegion())
            ->setRegion_id($billing->getRegionId())
            ->setPostcode($billing->getPostcode())
            ->setTelephone($billing->getTelephone())
            ->setFax($billing->getFax());
        $this->_order->setBillingAddress($billingAddress);

        $shipping = $this->_customer->getDefaultShippingAddress();
        $shippingAddress = Mage::getModel('sales/order_address')
            ->setStoreId($this->_storeId)
            ->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_SHIPPING)
            ->setCustomerId($this->_customer->getId())
            ->setCustomerAddressId($this->_customer->getDefaultShipping())
            ->setCustomer_address_id($shipping->getEntityId())
            ->setPrefix($shipping->getPrefix())
            ->setFirstname($shipping->getFirstname())
            ->setMiddlename($shipping->getMiddlename())
            ->setLastname($shipping->getLastname())
            ->setSuffix($shipping->getSuffix())
            ->setCompany($shipping->getCompany())
            ->setStreet($shipping->getStreet())
            ->setCity($shipping->getCity())
            ->setCountry_id($shipping->getCountryId())
            ->setRegion($shipping->getRegion())
            ->setRegion_id($shipping->getRegionId())
            ->setPostcode($shipping->getPostcode())
            ->setTelephone($shipping->getTelephone())
            ->setFax($shipping->getFax());

        $this->_order->setShippingAddress($shippingAddress)
            ->setShippingMethod($this->_shippingMethod);

        $orderPayment = Mage::getModel('sales/order_payment')
            ->setStoreId($this->_storeId)
            ->setCustomerPaymentId(0)
            ->setMethod($this->_paymentMethod)
            ->setPoNumber(' – ');

        $this->_order->setPayment($orderPayment);

        $this->_addProducts($products);

        $this->_order->setSubtotal($this->_subTotal)
            ->setBaseSubtotal($this->_subTotal)
            ->setGrandTotal($this->_subTotal)
            ->setBaseGrandTotal($this->_subTotal);

        $transaction->addObject($this->_order);
        $transaction->addCommitCallback(array($this->_order, 'place'));
        $transaction->addCommitCallback(array($this->_order, 'save'));
        $transaction->save();
    }

    protected function _addProducts($products)
    {
        $this->_subTotal = 0;

        foreach ($products as $productRequest) {
            if ($productRequest['product'] == 'rand') {

                $productsCollection = Mage::getResourceModel('catalog/product_collection');

                $productsCollection->addFieldToFilter('type_id', 'simple');
                Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($productsCollection);

                $productsCollection->getSelect()
                    ->order('RAND()')
                    ->limit(rand($productRequest['min'], $productRequest['max']));

                foreach ($productsCollection as $product) {
                    $this->_addProduct(array(
                        'product' => $product->getId(),
                        'qty' => rand(1, 2)
                    ));
                }
            } else {
                $this->_addProduct($productRequest);
            }
        }
    }

    protected function _addProduct($requestData)
    {
        $request = new Varien_Object();
        $request->setData($requestData);

        $product = Mage::getModel('catalog/product')->load($request['product']);

        $cartCandidates = $product->getTypeInstance(true)
            ->prepareForCartAdvanced($request, $product);

        if (is_string($cartCandidates)) {
            throw new Exception($cartCandidates);
        }

        if (!is_array($cartCandidates)) {
            $cartCandidates = array($cartCandidates);
        }

        $parentItem = null;
        $errors = array();
        $items = array();
        foreach ($cartCandidates as $candidate) {
            $item = $this->_productToOrderItem($candidate, $candidate->getCartQty());

            $items[] = $item;

            /**
             * As parent item we should always use the item of first added product
             */
            if (!$parentItem) {
                $parentItem = $item;
            }
            if ($parentItem && $candidate->getParentProductId()) {
                $item->setParentItem($parentItem);
            }
            /**
             * We specify qty after we know about parent (for stock)
             */
            $item->setQty($item->getQty() + $candidate->getCartQty());

            // collect errors instead of throwing first one
            if ($item->getHasError()) {
                $message = $item->getMessage();
                if (!in_array($message, $errors)) { // filter duplicate messages
                    $errors[] = $message;
                }
            }
        }
        if (!empty($errors)) {
            Mage::throwException(implode("\n", $errors));
        }

        foreach ($items as $item) {
            $this->_order->addItem($item);
        }

        return $items;
    }

    function _productToOrderItem(Mage_Catalog_Model_Product $product, $qty = 1)
    {
        $rowTotal = $product->getFinalPrice() * $qty;

        $options = $product->getCustomOptions();

        $optionsByCode = array();

        foreach ($options as $option) {
            $quoteOption = Mage::getModel('sales/quote_item_option')->setData($option->getData())
                ->setProduct($option->getProduct());

            $optionsByCode[$quoteOption->getCode()] = $quoteOption;
        }

        $product->setCustomOptions($optionsByCode);

        $options = $product->getTypeInstance(true)->getOrderOptions($product);

        $orderItem = Mage::getModel('sales/order_item')
            ->setStoreId($this->_storeId)
            ->setQuoteItemId(0)
            ->setQuoteParentItemId(NULL)
            ->setProductId($product->getId())
            ->setProductType($product->getTypeId())
            ->setQtyBackordered(NULL)
            ->setTotalQtyOrdered($product['rqty'])
            ->setQtyOrdered($product['qty'])
            ->setName($product->getName())
            ->setSku($product->getSku())
            ->setPrice($product->getFinalPrice())
            ->setBasePrice($product->getFinalPrice())
            ->setOriginalPrice($product->getFinalPrice())
            ->setRowTotal($rowTotal)
            ->setBaseRowTotal($rowTotal)
            ->setWeeeTaxApplied(serialize(array()))
            ->setBaseWeeeTaxDisposition(0)
            ->setWeeeTaxDisposition(0)
            ->setBaseWeeeTaxRowDisposition(0)
            ->setWeeeTaxRowDisposition(0)
            ->setBaseWeeeTaxAppliedAmount(0)
            ->setBaseWeeeTaxAppliedRowAmount(0)
            ->setWeeeTaxAppliedAmount(0)
            ->setWeeeTaxAppliedRowAmount(0)
            ->setProductOptions($options);

        $this->_subTotal += $rowTotal;

        return $orderItem;
    }
}

Then I include the following statement in the controller:

        $orderGenerator = Mage::getModel('app/ordercreate');

        $orderGenerator->createOrder(array(
            array(
                'product' => 24, //product id
                'qty' => 1
            )
        ));

But I constantly receive error 500.

I took that code from amasty https://blog.amasty.com/creating-magento-order-programmatically/


UPDATE:

finally this is the code if you need to create an order programmatically in magento 1.9.

<?php
class NameApp_App_Model_OrderCreate extends Mage_Core_Model_Abstract
{
    const CUSTOMER_RANDOM = null;
    protected $_shippingMethod = 'freeshipping_freeshipping';
    protected $_paymentMethod = 'cashondelivery';
    protected $_subTotal = 0;
    protected $_order;
    protected $_storeId = '0';

    public function _construct()
    {
        parent::_construct();
        $this->_init('app/ordercreate');
    }

    public function setShippingMethod($methodName)
    {
        $this->_shippingMethod = $methodName;
    }
    public function setPaymentMethod($methodName)
    {
        $this->_paymentMethod = $methodName;
    }

    public function createOrder($products)
    {
        $transaction = Mage::getModel('core/resource_transaction');
        $this->_storeId = Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);
        $this->_customer = Mage::getSingleton('customer/session')->getCustomer();
        $reservedOrderId = Mage::getSingleton('eav/config')
            ->getEntityType('order')
            ->fetchNewIncrementId($this->_storeId);

        $currencyCode = Mage::app()->getBaseCurrencyCode();
        $this->_order = Mage::getModel('sales/order')
            ->setIncrementId($reservedOrderId)
            ->setStoreId($this->_storeId)
            ->setQuoteId(0)
            ->setGlobalCurrencyCode($currencyCode)
            ->setBaseCurrencyCode($currencyCode)
            ->setStoreCurrencyCode($currencyCode)
            ->setOrderCurrencyCode($currencyCode);


        $this->_order->setCustomerEmail($this->_customer->getEmail())
            ->setCustomerFirstname($this->_customer->getFirstname())
            ->setCustomerLastname($this->_customer->getLastname())
            ->setCustomerGroupId($this->_customer->getGroupId())
            ->setCustomerIsGuest(0)
            ->setCustomer($this->_customer);


        $billing = $this->_customer->getDefaultBillingAddress();
        $billingAddress = Mage::getModel('sales/order_address')
            ->setStoreId($this->_storeId)
            ->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_BILLING)
            ->setCustomerId($this->_customer->getId())
            ->setCustomerAddressId($this->_customer->getDefaultBilling())
            ->setCustomerAddress_id($billing->getEntityId())
            ->setPrefix($billing->getPrefix())
            ->setFirstname($billing->getFirstname())
            ->setMiddlename($billing->getMiddlename())
            ->setLastname($billing->getLastname())
            ->setSuffix($billing->getSuffix())
            ->setCompany($billing->getCompany())
            ->setStreet($billing->getStreet())
            ->setCity($billing->getCity())
            ->setCountry_id($billing->getCountryId())
            ->setRegion($billing->getRegion())
            ->setRegion_id($billing->getRegionId())
            ->setPostcode($billing->getPostcode())
            ->setTelephone($billing->getTelephone())
            ->setFax($billing->getFax());
        $this->_order->setBillingAddress($billingAddress);

        $shipping = $this->_customer->getDefaultShippingAddress();
        $shippingAddress = Mage::getModel('sales/order_address')
            ->setStoreId($this->_storeId)
            ->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_SHIPPING)
            ->setCustomerId($this->_customer->getId())
            ->setCustomerAddressId($this->_customer->getDefaultShipping())
            ->setCustomer_address_id($shipping->getEntityId())
            ->setPrefix($shipping->getPrefix())
            ->setFirstname($shipping->getFirstname())
            ->setMiddlename($shipping->getMiddlename())
            ->setLastname($shipping->getLastname())
            ->setSuffix($shipping->getSuffix())
            ->setCompany($shipping->getCompany())
            ->setStreet($shipping->getStreet())
            ->setCity($shipping->getCity())
            ->setCountry_id($shipping->getCountryId())
            ->setRegion($shipping->getRegion())
            ->setRegion_id($shipping->getRegionId())
            ->setPostcode($shipping->getPostcode())
            ->setTelephone($shipping->getTelephone())
            ->setFax($shipping->getFax());

        $this->_order->setShippingAddress($shippingAddress)
            ->setShippingMethod($this->_shippingMethod);

        $orderPayment = Mage::getModel('sales/order_payment')
            ->setStoreId($this->_storeId)
            ->setCustomerPaymentId(0)
            ->setMethod($this->_paymentMethod)
            ->setPoNumber(' – ');

        $this->_order->setPayment($orderPayment);

        $this->_addProducts($products);

        $this->_order->setSubtotal($this->_subTotal)
            ->setBaseSubtotal($this->_subTotal)
            ->setGrandTotal($this->_subTotal)
            ->setBaseGrandTotal($this->_subTotal);

        $transaction->addObject($this->_order);
        $transaction->addCommitCallback(array($this->_order, 'place'));
        $transaction->addCommitCallback(array($this->_order, 'save'));
        $transaction->save();
    }

    protected function _addProducts($products)
    {
        $this->_subTotal = 0;

        foreach ($products as $productRequest) {
            if ($productRequest['product'] == 'rand') {

                $productsCollection = Mage::getResourceModel('catalog/product_collection');

                $productsCollection->addFieldToFilter('type_id', 'simple');
                Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($productsCollection);

                $productsCollection->getSelect()
                    ->order('RAND()')
                    ->limit(rand($productRequest['min'], $productRequest['max']));

                foreach ($productsCollection as $product) {
                    $this->_addProduct(array(
                        'product' => $product->getId(),
                        'qty' => rand(1, 2)
                    ));
                }
            } else {
                $this->_addProduct($productRequest);
            }
        }
    }

    protected function _addProduct($requestData)
    {
        $request = new Varien_Object();
        $request->setData($requestData);

        $product = Mage::getModel('catalog/product')->load($request['product']);

        $cartCandidates = $product->getTypeInstance(true)
            ->prepareForCartAdvanced($request, $product);

        if (is_string($cartCandidates)) {
            throw new Exception($cartCandidates);
        }

        if (!is_array($cartCandidates)) {
            $cartCandidates = array($cartCandidates);
        }

        $parentItem = null;
        $errors = array();
        $items = array();
        foreach ($cartCandidates as $candidate) {
            $item = $this->_productToOrderItem($candidate, $candidate->getCartQty());

            $items[] = $item;

            /**
             * As parent item we should always use the item of first added product
             */
            if (!$parentItem) {
                $parentItem = $item;
            }
            if ($parentItem && $candidate->getParentProductId()) {
                $item->setParentItem($parentItem);
            }
            /**
             * We specify qty after we know about parent (for stock)
             */
            $item->setQty($item->getQty() + $candidate->getCartQty());

            // collect errors instead of throwing first one
            if ($item->getHasError()) {
                $message = $item->getMessage();
                if (!in_array($message, $errors)) { // filter duplicate messages
                    $errors[] = $message;
                }
            }
        }
        if (!empty($errors)) {
            Mage::throwException(implode("\n", $errors));
        }

        foreach ($items as $item) {
            $this->_order->addItem($item);
        }

        return $items;
    }

    function _productToOrderItem(Mage_Catalog_Model_Product $product, $qty = 1)
    {
        $rowTotal = $product->getFinalPrice() * $qty;

        $options = $product->getCustomOptions();

        $optionsByCode = array();

        foreach ($options as $option) {
            $quoteOption = Mage::getModel('sales/quote_item_option')->setData($option->getData())
                ->setProduct($option->getProduct());

            $optionsByCode[$quoteOption->getCode()] = $quoteOption;
        }

        $product->setCustomOptions($optionsByCode);

        $options = $product->getTypeInstance(true)->getOrderOptions($product);

        $orderItem = Mage::getModel('sales/order_item')
            ->setStoreId($this->_storeId)
            ->setQuoteItemId(0)
            ->setQuoteParentItemId(NULL)
            ->setProductId($product->getId())
            ->setProductType($product->getTypeId())
            ->setQtyBackordered(NULL)
            ->setTotalQtyOrdered($product['rqty'])
            ->setQtyOrdered($product['qty'])
            ->setName($product->getName())
            ->setSku($product->getSku())
            ->setPrice($product->getFinalPrice())
            ->setBasePrice($product->getFinalPrice())
            ->setOriginalPrice($product->getFinalPrice())
            ->setRowTotal($rowTotal)
            ->setBaseRowTotal($rowTotal)
            ->setWeeeTaxApplied(serialize(array()))
            ->setBaseWeeeTaxDisposition(0)
            ->setWeeeTaxDisposition(0)
            ->setBaseWeeeTaxRowDisposition(0)
            ->setWeeeTaxRowDisposition(0)
            ->setBaseWeeeTaxAppliedAmount(0)
            ->setBaseWeeeTaxAppliedRowAmount(0)
            ->setWeeeTaxAppliedAmount(0)
            ->setWeeeTaxAppliedRowAmount(0)
            ->setProductOptions($options);

        $this->_subTotal += $rowTotal;

        return $orderItem;
    }
}

Then you can call it from controller:

    $orderGenerator = Mage::getModel('app/ordercreate');

    $orderGenerator->createOrder(array(
        array(
            'product' => 41, //product id
            'qty' => 1
        )
    ));

thats it. you can make product => 41 dynamic instead of static. its your choice.

  • @meenakshi-sundaram-r thanks for the corrections – s_h Jul 15 '15 at 5:15
  • 1
    I've updated my answer. Please see below – willem wigman Jul 18 '15 at 10:43
  • @willemwigman yes I have 4. even if I forced the customerid to an existing one eg: 2 , zend debugger says _customer=2 and receive same error with _customer details. weird – s_h Jul 18 '15 at 20:36
2
+50

You are calling the function createOrder() on Model initialization (which needs arguments). So $orderGenerator = Mage::getModel('app/ordercreate')->createOrder(); will throw an error.

Try:

$orderGenerator = Mage::getModel('app/ordercreate'); // removed ->createOrder()

$orderGenerator->createOrder(array(
        array(
            'product' => 24, //product id
            'qty' => 1
        )
    ));

Following up on your new edit (with customer error). Do you have any customers in your store? If not, create a customer first, otherwise the script can't get any random customer to attach the order to.


So, as you found out partly yourself, the customer-load function didn't work. Why would still be an interesting question though. Here's the working code, although it could still use some improvements (or have a look at this online diff):

<?php
 class NameApp_App_Model_OrderCreate extends Mage_Core_Model_Abstract
{
const CUSTOMER_RANDOM = null;
protected $_shippingMethod = 'freeshipping_freeshipping';
protected $_paymentMethod = 'cashondelivery';
protected $_subTotal = 0;
protected $_order;
protected $_storeId = '0';

public function _construct()
{
    parent::_construct();
    $this->_init('app/ordercreate');
}

public function setShippingMethod($methodName)
{
    $this->_shippingMethod = $methodName;
}
public function setPaymentMethod($methodName)
{
    $this->_paymentMethod = $methodName;
}

public function createOrder($products)
{
    $transaction = Mage::getModel('core/resource_transaction');
    $this->_storeId = Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);
    $this->_customer = Mage::getSingleton('customer/session')->getCustomer();
    $reservedOrderId = Mage::getSingleton('eav/config')
        ->getEntityType('order')
        ->fetchNewIncrementId($this->_storeId);

    $currencyCode = Mage::app()->getBaseCurrencyCode();
    $this->_order = Mage::getModel('sales/order')
        ->setIncrementId($reservedOrderId)
        ->setStoreId($this->_storeId)
        ->setQuoteId(0)
        ->setGlobalCurrencyCode($currencyCode)
        ->setBaseCurrencyCode($currencyCode)
        ->setStoreCurrencyCode($currencyCode)
        ->setOrderCurrencyCode($currencyCode);


    $this->_order->setCustomerEmail($this->_customer->getEmail())
        ->setCustomerFirstname($this->_customer->getFirstname())
        ->setCustomerLastname($this->_customer->getLastname())
        ->setCustomerGroupId($this->_customer->getGroupId())
        ->setCustomerIsGuest(0)
        ->setCustomer($this->_customer);


    $billing = $this->_customer->getDefaultBillingAddress();
    $billingAddress = Mage::getModel('sales/order_address')
        ->setStoreId($this->_storeId)
        ->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_BILLING)
        ->setCustomerId($this->_customer->getId())
        ->setCustomerAddressId($this->_customer->getDefaultBilling())
        ->setCustomerAddress_id($billing->getEntityId())
        ->setPrefix($billing->getPrefix())
        ->setFirstname($billing->getFirstname())
        ->setMiddlename($billing->getMiddlename())
        ->setLastname($billing->getLastname())
        ->setSuffix($billing->getSuffix())
        ->setCompany($billing->getCompany())
        ->setStreet($billing->getStreet())
        ->setCity($billing->getCity())
        ->setCountry_id($billing->getCountryId())
        ->setRegion($billing->getRegion())
        ->setRegion_id($billing->getRegionId())
        ->setPostcode($billing->getPostcode())
        ->setTelephone($billing->getTelephone())
        ->setFax($billing->getFax());
    $this->_order->setBillingAddress($billingAddress);

    $shipping = $this->_customer->getDefaultShippingAddress();
    $shippingAddress = Mage::getModel('sales/order_address')
        ->setStoreId($this->_storeId)
        ->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_SHIPPING)
        ->setCustomerId($this->_customer->getId())
        ->setCustomerAddressId($this->_customer->getDefaultShipping())
        ->setCustomer_address_id($shipping->getEntityId())
        ->setPrefix($shipping->getPrefix())
        ->setFirstname($shipping->getFirstname())
        ->setMiddlename($shipping->getMiddlename())
        ->setLastname($shipping->getLastname())
        ->setSuffix($shipping->getSuffix())
        ->setCompany($shipping->getCompany())
        ->setStreet($shipping->getStreet())
        ->setCity($shipping->getCity())
        ->setCountry_id($shipping->getCountryId())
        ->setRegion($shipping->getRegion())
        ->setRegion_id($shipping->getRegionId())
        ->setPostcode($shipping->getPostcode())
        ->setTelephone($shipping->getTelephone())
        ->setFax($shipping->getFax());

    $this->_order->setShippingAddress($shippingAddress)
        ->setShippingMethod($this->_shippingMethod);

    $orderPayment = Mage::getModel('sales/order_payment')
        ->setStoreId($this->_storeId)
        ->setCustomerPaymentId(0)
        ->setMethod($this->_paymentMethod)
        ->setPoNumber(' – ');

    $this->_order->setPayment($orderPayment);

    $this->_addProducts($products);

    $this->_order->setSubtotal($this->_subTotal)
        ->setBaseSubtotal($this->_subTotal)
        ->setGrandTotal($this->_subTotal)
        ->setBaseGrandTotal($this->_subTotal);

    $transaction->addObject($this->_order);
    $transaction->addCommitCallback(array($this->_order, 'place'));
    $transaction->addCommitCallback(array($this->_order, 'save'));
    $transaction->save();
}

protected function _addProducts($products)
{
    $this->_subTotal = 0;

    foreach ($products as $productRequest) {
        if ($productRequest['product'] == 'rand') {

            $productsCollection = Mage::getResourceModel('catalog/product_collection');

            $productsCollection->addFieldToFilter('type_id', 'simple');
            Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($productsCollection);

            $productsCollection->getSelect()
                ->order('RAND()')
                ->limit(rand($productRequest['min'], $productRequest['max']));

            foreach ($productsCollection as $product) {
                $this->_addProduct(array(
                    'product' => $product->getId(),
                    'qty' => rand(1, 2)
                ));
            }
        } else {
            $this->_addProduct($productRequest);
        }
    }
}

protected function _addProduct($requestData)
{
    $request = new Varien_Object();
    $request->setData($requestData);

    $product = Mage::getModel('catalog/product')->load($request['product']);

    $cartCandidates = $product->getTypeInstance(true)
        ->prepareForCartAdvanced($request, $product);

    if (is_string($cartCandidates)) {
        throw new Exception($cartCandidates);
    }

    if (!is_array($cartCandidates)) {
        $cartCandidates = array($cartCandidates);
    }

    $parentItem = null;
    $errors = array();
    $items = array();
    foreach ($cartCandidates as $candidate) {
        $item = $this->_productToOrderItem($candidate, $candidate->getCartQty());

        $items[] = $item;

        /**
         * As parent item we should always use the item of first added product
         */
        if (!$parentItem) {
            $parentItem = $item;
        }
        if ($parentItem && $candidate->getParentProductId()) {
            $item->setParentItem($parentItem);
        }
        /**
         * We specify qty after we know about parent (for stock)
         */
        $item->setQty($item->getQty() + $candidate->getCartQty());

        // collect errors instead of throwing first one
        if ($item->getHasError()) {
            $message = $item->getMessage();
            if (!in_array($message, $errors)) { // filter duplicate messages
                $errors[] = $message;
            }
        }
    }
    if (!empty($errors)) {
        Mage::throwException(implode("\n", $errors));
    }

    foreach ($items as $item) {
        $this->_order->addItem($item);
    }

    return $items;
}

function _productToOrderItem(Mage_Catalog_Model_Product $product, $qty = 1)
{
    $rowTotal = $product->getFinalPrice() * $qty;

    $options = $product->getCustomOptions();

    $optionsByCode = array();

    foreach ($options as $option) {
        $quoteOption = Mage::getModel('sales/quote_item_option')->setData($option->getData())
            ->setProduct($option->getProduct());

        $optionsByCode[$quoteOption->getCode()] = $quoteOption;
    }

    $product->setCustomOptions($optionsByCode);

    $options = $product->getTypeInstance(true)->getOrderOptions($product);

    $orderItem = Mage::getModel('sales/order_item')
        ->setStoreId($this->_storeId)
        ->setQuoteItemId(0)
        ->setQuoteParentItemId(NULL)
        ->setProductId($product->getId())
        ->setProductType($product->getTypeId())
        ->setQtyBackordered(NULL)
        ->setTotalQtyOrdered($product['rqty'])
        ->setQtyOrdered($product['qty'])
        ->setName($product->getName())
        ->setSku($product->getSku())
        ->setPrice($product->getFinalPrice())
        ->setBasePrice($product->getFinalPrice())
        ->setOriginalPrice($product->getFinalPrice())
        ->setRowTotal($rowTotal)
        ->setBaseRowTotal($rowTotal)
        ->setWeeeTaxApplied(serialize(array()))
        ->setBaseWeeeTaxDisposition(0)
        ->setWeeeTaxDisposition(0)
        ->setBaseWeeeTaxRowDisposition(0)
        ->setWeeeTaxRowDisposition(0)
        ->setBaseWeeeTaxAppliedAmount(0)
        ->setBaseWeeeTaxAppliedRowAmount(0)
        ->setWeeeTaxAppliedAmount(0)
        ->setWeeeTaxAppliedRowAmount(0)
        ->setProductOptions($options);

    $this->_subTotal += $rowTotal;

    return $orderItem;
}

}

  • 1
    Tested and can conform it works this way. – willem wigman Jul 17 '15 at 11:57
  • not working for me. are you testing in Magento 1.9? thanks in advance – s_h Jul 17 '15 at 18:06
  • 1
    absolutely. Is your model located at /app/code/[community or local]/Pricebinc/App/Model/OrderCreate.php ? Is your custom extension "Pricebinc_App" loaded correctly? Does it show up in system>config>advanced>advanced (module output screen) ? – willem wigman Jul 17 '15 at 18:08
  • 1
    Does the controller still work when you only include the first line? What happens when you include Zend_Debug::dump($orderGenerator); after initiating the Model? – willem wigman Jul 17 '15 at 18:39
  • 1
    Then it's about time to check var/system.log and your servers www_error_log files. It's either that, if you have the access to the the server logs, or the 'hacky way'. Start commenting out functions in createOrder(), starting all the way down. If you can get the last lines of the www_error_log and post it that would help. – willem wigman Jul 17 '15 at 18:57

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.