Magento 1.9:如何以编程方式创建订单

时间:2014-09-25 16:59:51

标签: magento

我发现了类似的主题,但它们并不是最新的 我必须用随机产品生成大约1000个订单。我找到了关于创建订单http://inchoo.net/magento/programmatically-create-order-in-magento/的好文章,但它有点旧,而且它不适用于版本1.9。

我收到以下错误:

array (size=5)
  0 => string 'Please specify the product's option(s).' (length=39)
  1 => string 'Please specify the product's option(s).' (length=39)
  2 => string 'Please specify the product's option(s).' (length=39)
  3 => string 'Please specify the product's option(s).' (length=39)
  4 => string 'Please specify the product's option(s).' (length=39)

#0 E:\xampp\htdocs\magento\app\code\core\Mage\Adminhtml\Model\Sales\Order\Create.php(1631): Mage::throwException('')
#1 E:\xampp\htdocs\magento\app\code\core\Mage\Adminhtml\Model\Sales\Order\Create.php(1523): Mage_Adminhtml_Model_Sales_Order_Create->_validate()

正如您在我的创建方法中所看到的,我向产品添加选项,为什么我会收到此错误?

这是我的班级

class MyNamespace_MyModule_Model_Generate_Sample {

    protected $orderData;
    protected $product;




    public function run()
    {
        //$customer is a random customer object

        $product = Mage::getModel('catalog/product')->load(342);

        $this->product = $product;


        $this->orderData = array(
            'session'       => array(
                'customer_id'   => $customer->getId(),
                'store_id'      => Mage::app()->getStore('default')->getId(),
            ),
            'payment'       => array(
                'method'    => 'checkmo',
            ),
            'add_products'  =>array(
                $product->getId() => array('qty' => 1),
            ),
            'order' => array(
                'currency' => 'USD',
                'account' => array(
                    'group_id' => $customer->getGroupId(),
                    'email' => $customer->getEmail()
                ),
                'billing_address' => array(
                    'customer_address_id' => $customer->getCustomerAddressId(),
                    'prefix' => '',
                    'firstname' => $customer->getFirstname(),
                    'middlename' => '',
                    'lastname' => $customer->getLastname(),
                    'suffix' => '',
                    'company' => '',
                    'street' => array($customer->getStreet(),''),
                    'city' => $customer->getCity(),
                    'country_id' => $customer->getCountryId(),
                    'region' => '',
                    'region_id' => $customer->getRegionId(),
                    'postcode' => $customer->getPostcode(),
                    'telephone' => $customer->getTelephone(),
                    'fax' => '',
                ),
                'shipping_address' => array(
                    'customer_address_id' => $customer->getCustomerAddressId(),
                    'prefix' => '',
                    'firstname' => $customer->getFirstname(),
                    'middlename' => '',
                    'lastname' => $customer->getLastname(),
                    'suffix' => '',
                    'company' => '',
                    'street' => array($customer->getStreet(),''),
                    'city' => $customer->getCity(),
                    'country_id' => $customer->getCountryId(),
                    'region' => '',
                    'region_id' => $customer->getRegionId(),
                    'postcode' => $customer->getPostcode(),
                    'telephone' => $customer->getTelephone(),
                    'fax' => '',
                ),
                'shipping_method' => 'flatrate_flatrate',
                'comment' => array(
                    'customer_note' => 'This order has been programmatically created via import script.',
                ),
                'send_confirmation' => 0
            ),
        );


        $this->create();


    }


    /**
     * Creates order
     */
    public function create()
    {
        $orderData = $this->orderData;

        if (!empty($orderData)) {

            $this->_initSession($orderData['session']);

            $this->_processQuote($orderData);
            if (!empty($orderData['payment'])) {
                $this->_getOrderCreateModel()->setPaymentData($orderData['payment']);
                $this->_getOrderCreateModel()->getQuote()->getPayment()->addData($orderData['payment']);
            }



            $item = $this->_getOrderCreateModel()->getQuote()->getItemByProduct($this->product);


            foreach($this->product->getAttributes() as $option)
            {
                if ($option->getIsVisibleOnFront()) {
                    $item->addOption(new Varien_Object(
                        array(
                            'product' => $this->product,
                            'code' => $option->getAttributeCode(),
                            'value' => $option->getFrontend()->getValue($this->product)
                        )
                    ));;
                }
            }



            Mage::app()->getStore()->setConfig(Mage_Sales_Model_Order::XML_PATH_EMAIL_ENABLED, "0");


            $_order = $this->_getOrderCreateModel()
                ->importPostData($orderData['order'])
                ->createOrder();


            $this->_getSession()->clear();
            Mage::unregister('rule_data');

            return $_order;
        }

        return null;
    }



    protected function _processQuote($data = array())
    {
        /* Saving order data */
        if (!empty($data['order'])) {
            $this->_getOrderCreateModel()->importPostData($data['order']);
        }

        $this->_getOrderCreateModel()->getBillingAddress();
        $this->_getOrderCreateModel()->setShippingAsBilling(true);

        /* Just like adding products from Magento admin grid */
        if (!empty($data['add_products'])) {
            $this->_getOrderCreateModel()->addProducts($data['add_products']);
        }

        /* Collect shipping rates */
        $this->_getOrderCreateModel()->collectShippingRates();

        /* Add payment data */
        if (!empty($data['payment'])) {
            $this->_getOrderCreateModel()->getQuote()->getPayment()->addData($data['payment']);
        }

        $this->_getOrderCreateModel()
            ->initRuleData()
            ->saveQuote();

        if (!empty($data['payment'])) {
            $this->_getOrderCreateModel()->getQuote()->getPayment()->addData($data['payment']);
        }

        return $this;
    }





    /**
     * Retrieve order create model
     *
     * @return  Mage_Adminhtml_Model_Sales_Order_Create
     */
    protected function _getOrderCreateModel()
    {
        return Mage::getSingleton('adminhtml/sales_order_create');
    }




    /**
     * Retrieve session object
     *
     * @return Mage_Adminhtml_Model_Session_Quote
     */
    protected function _getSession()
    {
        return Mage::getSingleton('adminhtml/session_quote');
    }




    /**
     * Initialize order creation session data
     *
     * @param array $data
     * @return Mage_Adminhtml_Sales_Order_CreateController
     */
    protected function _initSession($data)
    {
        /* Get/identify customer */
        if (!empty($data['customer_id'])) {
            $this->_getSession()->setCustomerId((int) $data['customer_id']);
        }

        /* Get/identify store */
        if (!empty($data['store_id'])) {
            $this->_getSession()->setStoreId((int) $data['store_id']);
        }

        return $this;
    }


}

感谢您的帮助

0 个答案:

没有答案