2014-01-29 114 views
0

我想在创建订单时设置自定义价格programatically.order成功地放置在后端,但与产品具有相同的价格。我想为产品设置自定义价格时,下订单。 我已经使用下面的代码来下订单。如何以编程方式创建订单时设置自定义价格?

foreach ($singleOrderOutput['OrderLines'] as $key => $value) { 
         $productModel = Mage::getModel('catalog/product')->loadByAttribute('sku', $value['StockCode']); 
         if ($productModel) { 
          $productArray[$productModel->getId()]['qty'] = $value['OrderedQty']; 
         } 
        } 
        if (!empty($productArray)) { 
         $quote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore('default')->getId()); 
         $customer = Mage::getModel('customer/customer')->setWebsiteId(1)->loadByEmail($customer->getEmail()); 
         $quote->assignCustomer($customer); 


         foreach ($productArray as $productId => $product) { 
          $productMo = Mage::getModel('catalog/product')->load($productId); 
          $buyInfo = array(
           'qty' => 1 
          ); 
          $quote->addProduct($productMo, new Varien_Object($buyInfo)); 
         } 

         $debtorApi = "https://202.83.91.140/exoapi/exo/" . $customerData->getDebtorApiKey() . "/debtor/details"; 
         $debtorDetail = curl_init($debtorApi); 
         curl_setopt($debtorDetail, CURLOPT_RETURNTRANSFER, true); 
         curl_setopt($debtorDetail, CURLOPT_BINARYTRANSFER, true); 
         curl_setopt($debtorDetail, CURLOPT_SSL_VERIFYPEER, false); 
         $debtorDetailOutput = curl_exec($debtorDetail); 
         $debtorOutput  = json_decode($debtorDetailOutput, true); 
         $addressData  = array(
          'firstname' => $customer->getFirstname(), 
          'lastname' => $customer->getLastname(), 
          'street' => $debtorOutput['Address1'] . ',' . $debtorOutput['Address2'] . ',' . $debtorOutput['Address3'] . ',' . $debtorOutput['Address4'] . ',', 
          'city' => 'Somewhere', 
          'postcode' => $debtorOutput['Postcode'], 
          'telephone' => $debtorOutput['Phone'], 
          'country_id' => 'AU' 
         ); 
         $billingAddress  = $quote->getBillingAddress()->addData($addressData); 
         $shippingAddress = $quote->getShippingAddress()->addData($addressData); 
         $shippingAddress->setCollectShippingRates(true)->collectShippingRates()->setShippingMethod('matrixrate_matrixrate_11')->setPaymentMethod('checkmo'); 

         $quote->getPayment()->importData(array(
          'method' => 'checkmo' 
         )); 

         $quote->collectTotals()->save(); 

         $service = Mage::getModel('sales/service_quote', $quote); 
         $service->submitAll(); 
         $order = $service->getOrder(); 

         printf("Created order %s\n", $order->getIncrementId()); 

任何帮助将不胜感激。

+1

我认为这将有助于你http://stackoverflow.com/questions/20871364/magento-soap-v1-cart-product-add-需要设置价格在选项,阵列为SKU –

回答

3

解决

foreach ($singleOrderOutput['OrderLines'] as $key => $value) { 
         $productModel = Mage::getModel('catalog/product')->loadByAttribute('sku', $value['StockCode']); 
         if ($productModel) { 
          $productArray[$productModel->getId()]['qty'] = $value['OrderedQty']; 
         } 
        } 
        if (!empty($productArray)) { 
         $quote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore('default')->getId()); 
         $customer = Mage::getModel('customer/customer')->setWebsiteId(1)->loadByEmail($customer->getEmail()); 
         $quote->assignCustomer($customer); 


         foreach ($productArray as $productId => $product) { 
          $productMo = Mage::getModel('catalog/product')->load($productId); 
          $buyInfo = array(
           'qty' => 1 
          ); 
          //$quote->addProduct($productMo, new Varien_Object($buyInfo)); /*Before change */ 
$quote->addProduct($productMo, new Varien_Object($buyInfo))->setOriginalCustomPrice(20); /*after change */ 
         } 

         $debtorApi = "https://202.83.91.140/exoapi/exo/" . $customerData->getDebtorApiKey() . "/debtor/details"; 
         $debtorDetail = curl_init($debtorApi); 
         curl_setopt($debtorDetail, CURLOPT_RETURNTRANSFER, true); 
         curl_setopt($debtorDetail, CURLOPT_BINARYTRANSFER, true); 
         curl_setopt($debtorDetail, CURLOPT_SSL_VERIFYPEER, false); 
         $debtorDetailOutput = curl_exec($debtorDetail); 
         $debtorOutput  = json_decode($debtorDetailOutput, true); 
         $addressData  = array(
          'firstname' => $customer->getFirstname(), 
          'lastname' => $customer->getLastname(), 
          'street' => $debtorOutput['Address1'] . ',' . $debtorOutput['Address2'] . ',' . $debtorOutput['Address3'] . ',' . $debtorOutput['Address4'] . ',', 
          'city' => 'Somewhere', 
          'postcode' => $debtorOutput['Postcode'], 
          'telephone' => $debtorOutput['Phone'], 
          'country_id' => 'AU' 
         ); 
         $billingAddress  = $quote->getBillingAddress()->addData($addressData); 
         $shippingAddress = $quote->getShippingAddress()->addData($addressData); 
         $shippingAddress->setCollectShippingRates(true)->collectShippingRates()->setShippingMethod('matrixrate_matrixrate_11')->setPaymentMethod('checkmo'); 

         $quote->getPayment()->importData(array(
          'method' => 'checkmo' 
         )); 

         $quote->collectTotals()->save(); 

         $service = Mage::getModel('sales/service_quote', $quote); 
         $service->submitAll(); 
         $order = $service->getOrder(); 

         printf("Created order %s\n", $order->getIncrementId()); 

变更从$quote->addProduct($productMo, new Varien_Object($buyInfo)) TO $quote->addProduct($productMo, new Varien_Object($buyInfo))->setOriginalCustomPrice(20);

+0

你确定这实际上工作? $ quote-> addProduct()方法不返回lineitem,它返回报价。因此,您实际上将在报价上设置原始自定义价格,这不是有效的字段。我必须重写Mage_Adminhtml_Model_Sales_Order_Create并扩展addProduct方法才能正确使用setOriginalCustomPrice()。 – Corgalore

+0

是以上代码对我来说工作正常。 – Zaheerabbas

相关问题