0

I created a button to export a specific order from admin. This button apper on each order. Then i created a function that look like this:

public function expButtonAction()
    {
        Mage::app('admin');
        umask(0); 
        Mage::app(); 
        $filename = '/var/www/html/var/varexportEnabled220217.csv';
        $collection = Mage::getModel('sales/order')
                            ->getCollection()
                            ->addAttributeToSelect('*');

        if($collection->count() == 0){
            echo "No products found<br>";
            exit();
        }
        $i = 0;
        //$hasArgs = isset($argv[1])&& isset($argv[2]);
        echo "Nr. produse: " .count($collection) . "<br>";

        if($fh  = fopen($filename, 'w')) {

            foreach($collection as $product) {


                $row = array();
                $product = Mage::getModel('catalog/product')->load($product->getId());
                $qtyStock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->getQty();
                print_r($qtyStock);die();
                $status = $product->getAttributeText('status');
                $isInStock = $product->getStockItem();
                $hasRealImage = ($product->getSmallImage() != null && $product->getSmallImage() != "no_selection");
                $image = null;
                if($hasRealImage){
                    $image = "da";
                } else {
                    $image = "nu";
                }


                if($isInStock->getIsInStock())
                {
                    $isInStock = "in stock";
                }
                else
                {
                    $isInStock = "not in stock";
                }

                        $row[] = $i.";";
                        $row[] = "D1478.";
                        $row[] = "SETA.";
                        $row[] = "5;";
                        $row[] = "1;";
                        fputcsv($fh,$row);
                        /*if($i == 5) {
                            die();
                        }*/
                        echo $i. "-->" . $product->getSku() . "\n";
                        $i++;
                    //}
                //}
            }
            fclose($fh);
        }

        echo "End Script";
            }

This script take all my product from order, and i can't take the prodcuts details like SKU, name or something else.

For example if i want to take this value : $row[] = $product->getSku(); i recive null value

And i alos want to take only the product form a specific order, not all the product ordered on the site.

Does anyone know how can i take only those products from one order, and also to take the product details?

1 Answer 1

0

What you are retrieving is a collection of orders. For every order object, there is an order items collection that needs to be obtained.

$orderCollection = Mage::getModel('sales/order')
                        ->getCollection()
                        ->addAttributeToSelect('*');

foreach($orderCollection as $order) {

    $orderItems = $order->getAllItems();
    foreach($orderItems as $orderItem) {
        // here you have an order ITEM.
        // an order item will give you some product information,
        // or you can load the product
        $product = $orderItem->getProduct();
    }

}

This is just an example. The danger here is that you don't know how BIG the order collection actually is, and looping through it could be dangerous.

You've mentioned that your example is being visited via an order page in the admin, so you should endevour to send the orderid with your request and retrieve it. This means you can load the order directly.

...

$id = $this->getRequest()->getParam('order_id');
$order = Mage::getModel('sales/order')->load($id);

...
1
  • thank you very much. do you know how can i send the order id? Apr 7, 2017 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.