Friday, December 6, 2013

Magento | Get Product Minimum Sale Qty on Category View / Product List Page

Here is the following code snippet required to be added into your file where you want to display the Minimum Sale Quantity of the Product -

<?php $loadProduct = Mage::getModel('catalog/product')->load( $_product->getId() ); ?>
<?php echo $this->getMinimalQty($loadProduct); ?>

Paste and Enjoy!

Tuesday, December 3, 2013

Magento | Create Custom CSV and Download

Here is the following code we can use to Create a Custom CSV using some values and Download.

Currently, I am trying to download the csv for all products of Orders. Here is the Sample Code I am using in Controller given below -

$orders = Mage::getModel("sales/order")->getCollection();
// prepare CSV header
            $csv = '';
            $_columns = array(
                "Order Id",
                "Product Name",
                "Sku",
                "Price"
            );
            $data = array();
            // prepare CSV header...
            foreach ($_columns as $column) {
                $data[] = '"'.$column.'"';
            }
            $csv .= implode(',', $data)."\n";
            foreach ($orders as $order) {
                $items = $order->getAllItems();
                // echo "Order - ".$order->getId()."<br>";
                foreach ($items as $item) {
                    $loadProduct = Mage::getModel('catalog/product')->load($item->getProductId());
                    //prepare csv contents
                    $data = array();
                    $data[] = $loadProduct['id'];
                    $data[] = $loadProduct['name'];
                    $data[] = $loadProduct['sku'];
                    $data[] = $loadProduct['price'];
                    //...
                    $csv .= implode(',', $data)."\n";
                    //now $csv varaible has csv data as string
                }
            }
        }
        $this->_redirect('*/*/');
        $this->_prepareDownloadResponse('file.csv', $csv, 'text/csv');