2012-09-13 116 views
1

我必須通過從訂單中刪除一些商品來更新訂單,如果它有多個。然後必須更新訂單。Magento已訂購商品刪除

現在我找到了表格:sales_flat_order_item,其中包含所有訂單商品。

$ items = $ order-> getAllItems();

foreach($items as $item){ 

     if($item->getParentItemId() == '' || $item->getParentItemId() == null){ 

      $product_id = $item->getProductId(); 
      if($product_id == $booking_product_id){    

         // this item have to be deleted 

      } 

     } 

    } 

$ order-> save();

有什麼建議嗎?

+0

什麼是總體目標,你爲什麼要從訂單中刪除一件物品? –

+0

這是客戶的要求,他可以從他的訂單中取消物品,我將單獨顯示所有訂單物品。 –

+0

我正在嘗試一些東西,像$ item-> delete()。現在這樣好了。然後我通過扣除最近從訂單中刪除的這個項目價格來更新訂單總價格。 –

回答

3

這樣我就成功了。

$base_grand_total = $order->getBaseGrandTotal(); 
    $base_subtotal = $order->getBaseSubtotal(); 
    $grand_total = $order->getGrandTotal(); 
    $subtotal = $order->getSubtotal(); 

    $base_subtotal_incl_tax = $order->getBaseSubtotalInclTax(); 
    $subtotal_incl_tax = $order->getSubtotalInclTax(); 
    $total_item_count = $order->getTotalItemCount(); 

    $items = $order->getAllItems(); 
    foreach($items as $item){  

     if($item->getParentItemId() == '' || $item->getParentItemId() == null){ 

      $product_id = $item->getProductId(); 
      if($product_id == $booking_product_id){    

      //remove item price from total price of order 
      $item_price = $item->getPrice(); 
      $item->delete(); 

      $order->setBaseGrandTotal($base_grand_total-$item_price); 
      $order->setBaseSubtotal($base_subtotal-$item_price); 
       $order->setGrandTotal($grand_total-$item_price); 
      $order->setSubtotal($subtotal-$item_price); 

      $order->setBaseSubtotalInclTax($base_subtotal_incl_tax-$item_price); 
      $order->setSubtotalInclTax($subtotal_incl_tax-$item_price); 
      $order->setTotalItemCount($total_item_count-1); 
      $order->save(); 
      } 

     } 

    } 
+0

通過使用$ item-> delete(),我刪除該項目,然後通過從訂單價格中刪除此項目的價格,我更新訂單總計。可能對任何人都有幫助。 –

0

刪除現有訂單中的項目。

  1. 另存爲 'order_item_remove.php'
  2. 運行它作爲http://example.com/order_item_remove.php

代碼塊如下:下面從根的magento作爲外部代碼的代碼行

運行

<?php 

require_once('app/Mage.php'); //Path to Magento 
umask(0); 
Mage::app(); 

try{ 
$_order = Mage::getModel('sales/order')->loadByIncrementId('100000011'); // Order Increment Id 
$items = $_order->getAllItems(); 

foreach ($items as $item){ 
    Mage::log('All items ' . print_r($item->getData(), true), null, 'items_data.log'); 
    $base_grand_total = $_order->getBaseGrandTotal(); 

    $base_subtotal = $_order->getBaseSubtotal(); 
    $base_tva = $_order->getBaseTaxAmount(); 

    $grand_total = $_order->getGrandTotal(); 

    $subtotal = $_order->getSubtotal(); 
    $tva = $_order->getTaxAmount(); 


    $base_subtotal_incl_tax = $_order->getBaseSubtotalInclTax(); 

    $subtotal_incl_tax = $_order->getSubtotalInclTax(); 

    $total_item_count = $_order->getTotalItemCount(); 
Mage::log('Items ' . $item->getSku(), null, 'items_data.log'); 


    if($item->getSku()=='SAMPLEKIT'){ 
     $item_price = $item->getPrice(); 
     $item_tva = $item->getTaxAmount(); 
     Mage::log('Item deleted ' . $item->getSku(), null, 'items_data.log'); 
     $item->delete(); 
     $_order->setBaseGrandTotal($base_grand_total-$item_price-$item_tva); 

     $_order->setBaseSubtotal($base_subtotal-$item_price); 

     $_order->setBaseTaxAmount($base_tva-$item_tva); 

     $_order->setGrandTotal($grand_total-$item_price-$item_tva); 

     $_order->setSubtotal($subtotal-$item_price); 

     $_order->setTaxAmount($tva-$item_tva); 


     $_order->setBaseSubtotalInclTax($base_subtotal_incl_tax-$item_price); 

     $_order->setSubtotalInclTax($subtotal_incl_tax-$item_price); 

     $_order->setTotalItemCount(count($items)-1); 

     $_order->save(); 
    } 

} 
echo "success"; 
} catch(Exception $e) { 
    echo "failed"; 
}