Pregunta

I am doing a magento project in which i have to select all pending order at once and move to complete status from the order management page in admin.

I have done this modification and it is working fine. But the requirement is, while moving those orders to complete status, if the order consist of one particular product i have to remove that product or have to change the quantity to 0 and process it.

As a newbie to magento i fall short of logic's/ideas here. Any help will be greatly appreciated.

¿Fue útil?

Solución

After some analysis and googling i have achieved it using the below code,

$orderIds = $this->getRequest()->getPost('order_ids', array());  // get all order ids
$countCompleteOrder = 0;
$_resource = Mage::getSingleton('catalog/product')->getResource();
$mailBody = '';
$mailSend = 0;
foreach ($orderIds as $orderId) {
    $order = Mage::getModel('sales/order')->load($orderId);
    //echo '<pre>'; print_r($order); exit;
    $customer = Mage::getModel("customer/customer")->load($order->getCustomerId());
    $items = $order->getAllItems();
    $pass = 0;
    $true = $k = 0;
    $items_count = count($items);
    foreach ($items as $item) {
        //echo '<pre>'; print_r($item); exit;
        $k++;
        if ($k == 1) {
            $product = Mage::getModel('catalog/product')->load($item->getProductId());
            $cats = $product->getCategoryIds();
            $category_id = $cats[0];

            $_cat = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->load($category_id);
        }

        $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();

        $minorderqty = $_resource->getAttributeRawValue($item->getProductId(), 'min_order_qty', Mage::app()->getStore());

        $report = Mage::getResourceModel('reports/product_sold_collection')
                ->addOrderedQty()
                ->addAttributeToSelect(array('name', 'id'))
                ->addFieldToFilter('entity_id', $item->getProductId())
                ->getFirstItem();
        $ordered_qty = intval($report->getOrderedQty());

        for ($i = 0; $i < $items_count; $i++) {
            if ($ordered_qty >= $minorderqty) {
                $true++;
            }
        }

        if (($ordered_qty < $minorderqty) && ($true > 0)) {
            $item_price = $item->getPrice();
            $item_tva = $item->getTaxAmount();
            if ($items_count > 1) {

                $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();

                $mailSend++;
            }
        }
    }

}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a magento.stackexchange
scroll top