我正在使用自定义脚本来创建订单,它适用于没有自定义属性的简单产品。
现在我想为可配置的产品创建一个订单,比如产品价格从原始价格变为不同的产品属性选择。
我有一个要求苛刻的产品示例,我安装了带有示例数据的 magento 1.7.0.2,其中产品 [T-Shirt] 的产品 ID 为 126,其原价为 13.5,它是可配置的产品。
在商品详情页面上,其价格会通过选择 [Size:Medium] 和 [Color:Green] 更改为 18.75。
就我而言,我将其所有信息发送到我的网站。
在我的网站上,我将此产品的 SKU 命名为“zol_g_m”,并带有特定数量。现在我将它的信息发送到 magento 商店以创建订单,但我在 magento 商店的脚本将其视为简单产品并为其创建订单,价格为 13.5 而不是 18.75
我的脚本基于这样的两个函数
public function PrepareOrder($params, $paymentData, array $shippingAddress, array $billingAddress, $shippingMethod, $couponCode = null)
{
require_once 'app/Mage.php';
$app = Mage::app();
$req_result = array();
Mage::register('isSecureArea', true);
$customerModel = Mage::getModel('customer/customer');
$customerModel->setWebsiteId(Mage::app()->getWebsite()->getId());
$customerObj = $customerModel->loadByEmail('abc@hotmail.com'); //with customer email
$storeId=$customerObj->getStoreId();
$quoteObj=Mage::getModel('sales/quote')->assignCustomer($customerObj); //sets ship/bill address
$storeObj=$quoteObj->getStore()->load($storeId);
$quoteObj->setStore($storeObj);
$product_id = Mage::getModel('catalog/product')->getIdBySku('zol_g_med');
$productModel=Mage::getModel('catalog/product');
$productObj=$productModel->load(129);
$productObj->setSkipCheckRequiredOption(true);
$quoteItem=Mage::getModel('sales/quote_item')->setProduct($productObj);
$quoteItem->setQuote($quoteObj);
$quoteItem->setQty($product_qty);
$quoteObj->addItem($quoteItem);
$productObj->unsSkipCheckRequiredOption();
$quoteItem->checkData();
$quoteObj->getPayment()->importData(array('method' => 'ccsave',
'cc_owner' => $paymentData['cc_owner'],
'cc_type' => $paymentData['cc_type'],
'cc_number' => $paymentData['cc_number'],
'cc_exp_month' => $paymentData['cc_exp_month'],
'cc_exp_year' => $paymentData['cc_exp_year'],
'cc_cid' => $paymentData['cc_cid']));
// addresses
$quoteShippingAddress = new Mage_Sales_Model_Quote_Address();
$quoteShippingAddress->setData($shippingAddress);
$quoteBillingAddress = new Mage_Sales_Model_Quote_Address();
$quoteBillingAddress->setData($billingAddress);
$quoteObj->setShippingAddress($quoteShippingAddress);
$quoteObj->setBillingAddress($quoteBillingAddress);
// coupon code
$quoteObj->getShippingAddress()->setShipping_method($shippingMethod)->setShippingDescription($shippingMethod);
$quoteObj->getShippingAddress()->setCollectShippingRates(true); //
$quoteObj->getShippingAddress()->collectShippingRates(); //
$quoteObj -> addMessage('Auto Order Code'); //after separately
$quoteObj->setTotalsCollectedFlag(false);
$quoteObj->getShippingAddress()->unsetData('cached_items_all');
$quoteObj->getShippingAddress()->unsetData('cached_items_nominal');
$quoteObj->getShippingAddress()->unsetData('cached_items_nonnominal');
$quoteObj->collectTotals();
//end shipping charges block
$quoteObj->setIsActive(0); //if it is commented the shopping cart has last order items against customer logged in, also the order is displayed in my account block in frontend
$quoteObj->save();
$quoteId=$quoteObj->getId();
return $quoteId;
}
public function createOrderCustom($quoteId,$cartData)
{
$status_arr = array();
$quoteObj = Mage::getModel('sales/quote')->load($quoteId); // Mage_Sales_Model_Quote
$items = $quoteObj->getAllItems();
$quoteObj->reserveOrderId();
// convert quote to order
$convertQuoteObj = Mage::getSingleton('sales/convert_quote');
$orderObj = $convertQuoteObj->addressToOrder($quoteObj->getShippingAddress());
// convert quote addresses
$orderObj->setBillingAddress($convertQuoteObj->addressToOrderAddress($quoteObj->getBillingAddress()));
$orderObj->setShippingAddress($convertQuoteObj->addressToOrderAddress($quoteObj->getShippingAddress()));
// set payment options
$orderObj->setPayment($convertQuoteObj->paymentToOrderPayment($quoteObj->getPayment()));
// convert quote items
foreach ($items as $item) {
// @var $item Mage_Sales_Model_Quote_Item
$item->setOriginalPrice($item->getProduct()->getPrice());
$orderItem = $convertQuoteObj->itemToOrderItem($item);
//custom block of code to add/show product attributes/optinns, ***STAR FROM HERE...............................................//
$existentOptions['additional_options'] = array();
$current_product_id = '';
$current_product_id = $item->getProductId();
foreach($cartData as $part){
$productIdBySkuManual = Mage::getModel('catalog/product')->getIdBySku($part['product_sku']);
if(($current_product_id==$productIdBySkuManual) && isset($part['attributes']) && sizeof($part['attributes'])>0){
if($part['attributes']['attribute']['label']!='' && $part['attributes']['attribute']['value']!=''){
$existentOptions['additional_options'][] = array(
'label' => $part['attributes']['attribute']['label'],
'value' => $part['attributes']['attribute']['value'],
'print_value' => $part['attributes']['attribute']['print_value']
);
}else{
foreach($part['attributes']['attribute'] as $ic_product_options){
$existentOptions['additional_options'][] = array(
'label' => $ic_product_options['label'],
'value' => $ic_product_options['value'],
'print_value' => $ic_product_options['print_value']
);
}
}
}
}
$orderItem->setProductOptions($existentOptions);
$existentOptions['additional_options']='';
if ($productOptions = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct())) {
// $productOptions['info_buyRequest']['options'] = prepareOptionsForRequest($item);
$options = $productOptions;
}
if ($addOptions = $item->getOptionByCode('additional_options')) {
$options['additional_options'] = unserialize($addOptions->getValue());
}
if ($options) {
//$orderItem->setProductOptions($options);
}
if ($item->getParentItem()) {
$orderItem->setParentItem($orderObj->getItemByQuoteItemId($item->getParentItem()->getId()));
}
$orderObj->addItem($orderItem);
}
$orderObj->setCanShipPartiallyItem(false);
$orderObj->setState(Mage_Sales_Model_Order::STATE_NEW, true);
try {
$orderObj->place();
} catch (Exception $e){
Mage::log($e->getMessage());
Mage::log($e->getTraceAsString());
}
//$orderObj->sendNewOrderEmail(); //it doesnot work
$orderObj->setEmailSent(true);
$orderObj->save();
$last_insert_order_id = $orderObj->getRealOrderId();
return $last_insert_order_id;
}
请帮助调整此代码以用于可配置产品,因此根据属性选择以价格下单。