0

A customer of us has a special need for his site.He wants each customer to be able to download his wishlist as CSV.

Now my question is:

I know how to do that on the back-end for my module using in the controller the function exportCsvAction() . Is there anything similar i can do with the front-end? Or if not does Magento has something similar i can use or i should do something like getting the wishlist and add it to a CSV?? Thanks in advance.

1

Add these to your custom module controller:

class Some_Module_IndexController extends Mage_Core_Controller_Front_Action
{
    //Check if customer logged in
    public function preDispatch()
    {
        parent::preDispatch();
        if (!Mage::getSingleton('customer/session')->authenticate($this)) {
            $this->setFlag('', self::FLAG_NO_DISPATCH, true);
        }
    }

    public function downloadWishlistAction()
    {
        /**
         * @var $wishlist Mage_Wishlist_Model_Wishlist
         * @var $item Mage_Wishlist_Model_Item
         */
        $wishlist = Mage::getModel('wishlist/wishlist');
        $wishlist = $wishlist->loadByCustomer(Mage::getSingleton('customer/session')->getCustomer());
        $items = $wishlist->getItemCollection();

        $data = array(
            'Id',
            'Product Id',
            'Name',
            'Price',
            'Qty',
            'Added',
            'Store',
        );
        $fp = fopen('php://temp', 'r+');
        fputcsv($fp, $data, ',', '"');

        foreach ($items as $item) {
            $data = array(
                'id' => $item->getId(),
                'product_id' => $item->getProductId(),
                'product_name' => $item->getProduct()->getName(),
                'product_price' => $item->getProduct()->getPrice(),
                'qty' => $item->getQty(),
                'added_at' => $item->getAddedAt(),
                'store_name' => Mage::app()->getStore($item->getStoreId())->getName(),
            );
            fputcsv($fp, $data, ',', '"');
        }
        rewind($fp);
        $data = fread($fp, 1048576);
        fclose($fp);

        return $this->_prepareDownloadResponse(
            'MyWishList.csv',
            $data,
            'application/csv'
        );
    }
}

And customer can download own wishlist entering yourdomain.com/module_frontend/index/downloadWishlist

  • I was wondering how _prepareDownloadResponse($fileName, $content, $contentType = 'application/octet-stream',$contentLength = null) is working.That's from the class: param: string|array $content set to null to avoid starting output, $contentLength should be set explicitly in. How else can we set the content? string or array? I' m a little confused..But thanks it works great – vbak Jan 28 '14 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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