简体   繁体   English

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

[英]Magento: create bundle order programmatically

I have this code: http://pastebin.com/iFwyKM7G 我有以下代码: 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? 如何使用捆绑的产品进行此项工作?

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. 我现在想找出来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM