如何以编程方式创建订单时设置自定义价格?

时间:2014-01-29 09:10:22

标签: magento

我想在以编程方式创建订单时设置自定义价格。订单成功地放在后端但是产品具有相同的价格。我想在下订单时设置产品的自定义价格。 我使用下面的代码来下订单。

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 个答案:

答案 0 :(得分: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))更改为$quote->addProduct($productMo, new Varien_Object($buyInfo))->setOriginalCustomPrice(20);