簡體   English   中英

magento以編程方式為可配置產品創建訂單

[英]magento programmatically create order for configurable product

我正在使用自定義腳本來創建訂單,它對於沒有自定義屬性的簡單產品效果很好。

現在,我想為可配置產品創建一個訂單,例如產品價格從原始價格更改為關於產品屬性選擇的不同。

我有一個要求苛刻的產品的示例,我已經安裝了帶有樣品數據的magento 1.7.0.2,該樣品數據具有產品ID為126的產品[T恤],其原始價格為13.5,並且是可配置的產品。

在商品詳細信息頁面上,其價格可以通過選擇[大小:中]和[顏色:綠色]更改為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;
}

請為調整此可配置產品的代碼而需要幫助,因此根據屬性選擇確定訂單的價格。

不要用$quote->addItem()手工$quote->addItem()報價,因為要考慮的東西太多了,也沒有必要使其變得復雜。

您需要的是$quote->addProduct() 該方法具有以下簽名:

 /**
  * Add product to quote
  *
  * return error message if product type instance can't prepare product
  *
  * @param mixed $product
  * @param null|float|Varien_Object $request
  * @return Mage_Sales_Model_Quote_Item|string
  */
  public function addProduct(Mage_Catalog_Model_Product $product, $request = null)

$request可以只是一個數字(數量),也可以是Varien_Object ,它包含“添加到購物車”請求將發送的所有數據。 您需要此用於可配置產品:

$ buyRequest可以包含哪些數據?

除了產品和數量外,它還是捆綁產品的捆綁選項,可配置產品的可配置屬性,可下載產品的選定鏈接和自定義選項。

所有產品類型

  • 產品 :產品編號
  • 數量 :要求數量。 當報價項目或願望清單項目中的數量發生更改時,也會在此處進行調整。
  • original_qty :如果qty的值已更改,則original_qty包含原始請求的數量。
  • reset_count :用於更新購物車中的物品[...]
  • 添加到購物車表單中與報價生成無關的其他請求參數:
    • 相關產品
    • uenc
    • form_key

可配置產品

  • super_attribute :具有配置屬性的數組,格式為attribute_id => value_id

(資源)

可配置產品的最小請求對象將如下所示:

new Varien_Object(array(
    'qty' => $qty,
    'super_attribute' => [ $sizeAttribute->getId() => $valueMedium, $colorAttribute->getId() => $valueGreen ]
));

這應該為您指明正確的方向。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM