duanjianxi8439 2013-03-06 17:41
浏览 38
已采纳

Magento:以编程方式创建捆绑订单

I have this code: http://pastebin.com/iFwyKM7G

Inside an event-observer class which executes after the customer registered. It creates an order automatically and it works for simple products. However I cannot figure out for the life of me how to make it work with Bundled product!

How can I make this work with a bundled product?

  • 写回答

1条回答 默认 最新

  • 普通网友 2013-03-07 16:29
    关注

    I DID IT!

    I changed my code to the following:

    $customer = Mage::getSingleton('customer/customer')->load($observer->getCustomer()->getId());
            $session = Mage::getSingleton('adminhtml/session_quote');
            $order_create_model = Mage::getSingleton('adminhtml/sales_order_create');
            Mage::log($customer->debug(), null, 'toszodj_meg.log');
            //$transaction = Mage::getModel('core/resource_transaction');
            $storeId = $customer->getStoreId();
            Mage::log($customer->getDefaultBillingAddress()->debug(), null, 'toszodj_meg.log');
            $reservedOrderId = Mage::getSingleton('eav/config')->getEntityType('order')->fetchNewIncrementId($storeId);
            $session->setCustomerId((int) $customer->getId());
            $session->setStoreId((int) $storeId);
            $orderData = array(
            'session'       => array(
                'customer_id'   => $customer->getId(),
                'store_id'      => $storeId,
            ),
            'payment'       => array(
                'method'    => 'banktransfer',
                'po_number' => (string) '-',
            ),
            // 123456 denotes the product's ID value
            'add_products'  =>array(
                '2'    => array(
                        'qty' => 1,
                        'bundle_option' => array(
                            2 => 2,
                            1 => 1,
                        ),
                        'bundle_option_qty' => array(
                            2 => 1,
                            1 => 1,
                            ),
                        ),
                    ),
            'order'         => array(
                'currency'  => 'EUR',
                'account'   => array(
                    'group_id'  => $customer->getGroupId(),
                    'email'     => (string) $customer->getEmail(),
                ),
                'comment'           => array('customer_note' => 'API ORDER'),
                'send_confirmation' => 1,
                'shipping_method'   => 'flatrate_flatrate',
                'billing_address'   => array(
                    'customer_address_id' => $customer->getDefaultBillingAddress()->getEntityId(),
                    'prefix'             => $customer->getDefaultBillingAddress()->getPrefix(),
                    'firstname'           => $customer->getDefaultBillingAddress()->getFirstname(),
                    'middlename'          => $customer->getDefaultBillingAddress()->getMiddlename(),
                    'lastname'            => $customer->getDefaultBillingAddress()->getLastname(),
                    'suffix'             => $customer->getDefaultBillingAddress()->getSuffix(),
                    'company'              => $customer->getDefaultBillingAddress()->getCompany(),
                    'street'               => $customer->getDefaultBillingAddress()->getStreet(),
                    'city'                   => $customer->getDefaultBillingAddress()->getCity(),
                    'country_id'           => $customer->getDefaultBillingAddress()->getCountryId(),
                    'region'               => $customer->getDefaultBillingAddress()->getRegion(),
                    'region_id'           => $customer->getDefaultBillingAddress()->getRegionId(),
                    'postcode'               => $customer->getDefaultBillingAddress()->getPostcode(),
                    'telephone'           => $customer->getDefaultBillingAddress()->getTelephone(),
                    'fax'                   => $customer->getDefaultBillingAddress()->getFax(),
                ),
                'shipping_address'  => array(
                    'customer_address_id' => $customer->getDefaultShippingAddress()->getEntityId(),
                    'prefix'               => $customer->getDefaultShippingAddress()->getPrefix(),
                    'firstname'           => $customer->getDefaultShippingAddress()->getFirstname(),
                    'middlename'           => $customer->getDefaultShippingAddress()->getMiddlename(),
                    'lastname'               => $customer->getDefaultShippingAddress()->getLastname(),
                    'suffix'               => $customer->getDefaultShippingAddress()->getSuffix(),
                    'company'               => $customer->getDefaultShippingAddress()->getCompany(),
                    'street'               => $customer->getDefaultShippingAddress()->getStreet(),
                    'city'                   => $customer->getDefaultShippingAddress()->getCity(),
                    'country_id'           => $customer->getDefaultShippingAddress()->getCountryId(),
                    'region'               => $customer->getDefaultShippingAddress()->getRegion(),
                    'region_id'           => $customer->getDefaultShippingAddress()->getRegionId(),
                    'postcode'               => $customer->getDefaultShippingAddress()->getPostcode(),
                    'telephone'           => $customer->getDefaultShippingAddress()->getTelephone(),
                    'fax'                   => $customer->getDefaultShippingAddress()->getFax(),
                ),
            ),
        );
        $order_create_model->importPostData($orderData['order']);
        $order_create_model->getBillingAddress();
        $order_create_model->setShippingAsBilling(true);
        $order_create_model->addProducts($orderData['add_products']);
        $order_create_model->collectShippingRates();
        $order_create_model->getQuote()->getPayment()->addData($orderData['payment']);
        $order_create_model
                 ->initRuleData()
                 ->saveQuote();
        $order_create_model->getQuote()->getPayment()->addData($orderData['payment']);
        $order_create_model->setPaymentData($orderData['payment']);
        $order_create_model
                             ->importPostData($orderData['order'])
                             ->createOrder();
        $session->clear();
        Mage::unregister('rule_data');
        Mage::log('Order Successfull', null, 'siker_bammer.log');
    

    And it works! thought the customer doesn't get notified. which iam trying to figure out now.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 simulink中怎么使用solve函数?
  • ¥30 dspbuilder中使用signalcompiler时报错Error during compilation: Fitter failed,求解决办法
  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题
  • ¥15 51单片机显示器问题