2013-03-06 91 views
2

我有這樣的代碼:http://pastebin.com/iFwyKM7GMagento的:創建捆綁爲了編程

其內部登記的客戶之後執行的事件觀察器類。它會自動創建訂單,適用於簡單的產品。不過,我想不出我的生活如何使它與捆綁產品的工作!

如何我就與捆綁產品這項工作?

+0

http://inchoo.net/ecommerce/magento/programatically-add-bundle-product-to-cart-n-magento/ – B00MER 2013-03-06 18:27:14

+0

可悲的是沒有幫助。它解釋瞭如何將產品添加到購物車。我需要直接把它給一個訂單。 – NaGeL182 2013-03-07 12:24:36

回答

1

我做到了!

我改變了我的代碼如下:

$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'); 

和它的作品!認爲客戶沒有得到通知。我現在試圖找出答案。

+0

我們怎樣才能做同樣的在Magento 2 – mjdevloper 2016-11-21 10:40:58