Magento Ordered Items删除

时间:2012-09-13 09:34:46

标签: magento orders

我必须通过从订单中删除一些商品来更新订单。然后必须更新订单。

现在我找到了表: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

            }

        }

    }

$命令 - >保存();

有什么建议吗?

2 个答案:

答案 0 :(得分: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(); 
            }

        }

    }

答案 1 :(得分:0)

从现有订单中删除商品。

从magento root运行以下代码行作为外部代码:

  1. 将其保存为' order_item_remove.php'
  2. http://example.com/order_item_remove.php
  3. 运行它

    代码阻止如下:

    <?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";
    }