Showing posts with label Catalog. Show all posts
Showing posts with label Catalog. Show all posts

Friday, August 24, 2012

Magento Print Collection Query

There come several situations where you need to print the collection query in magento and here is the way how to print collection query:

Print Collection Query

$collection->printLogQuery(true);


Friday, August 10, 2012

All products from a category

Here is the way to show all products from a specific magento category to home page.


{{block type="catalog/product_list" name="home.catalog.product.list" alias="products_homepage" category_id="4" template="catalog/product/list.phtml"}}

Show All Products on Home Page

This is very common now a days to show all products on magento home page. Here is the code to achieve this:

{{block type="catalog/product_list" name="home.catalog.product.list" alias="products_homepage" template="catalog/product/list.phtml"}}

Thursday, August 9, 2012

Magento Display New Products on Home Page

There are many ways to show new products on home page but this is the simple way to display new products on home page.

{{block type="catalog/product_new" name="home.catalog.product.new" alias="product_homepage" template="catalog/product/new.phtml"}}

Wednesday, July 18, 2012

Add Product to Cart with Price Change

If you want to add a product to the cart after change its price, then you have to write an Observer that listens the checkout_cart_product_add_after or checkout_cart_update_items_after event. The code is same except checkout_cart_product_add_after is called for only one item and checkout_cart_update_items_after is called for all items in the cart.


/**
* @param Varien_Event_Observer $observer
*/
public function myDiscount(Varien_Event_Observer $observer)
{
$item = $observer->getQuoteItem();
if ($item->getParentItem()) {
$item = $item->getParentItem();
}

//discount 20% off
$discount = 0.20;

// Check if the discount isn't applied over and over while refreshing
$specialPrice = $item->getOriginalPrice() - ($item->getOriginalPrice() * $discount);

if ($specialPrice > 0) {
$item->setCustomPrice($specialPrice);
$item->setOriginalCustomPrice($specialPrice);
$item->getProduct()->setIsSuperMode(true);
}
}

Saturday, June 30, 2012

Retrieve Magento Products with Attribute Value

Magento uses EAV Model to retrieve products, so you'll need to add additional attributes that you want to return.


$collection = Mage::getModel('catalog/product')->getCollection();
//fetch name and orig_price into data
$collection->addAttributeToSelect('name');
$collection->addAttributeToSelect('orig_price');

The following shows how to filter by a range of values (greater than AND less than):


$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('name');
$collection->addAttributeToSelect('orig_price');
//filter for products whose orig_price is greater than (gt) 50
$collection->addFieldToFilter(array(array('attribute'=>'orig_price','gt'=>'50'),));
//AND filter for products whose orig_price is less than (lt) 100
$collection->addFieldToFilter(array(array('attribute'=>'orig_price','lt'=>'100'),));

While this will filter by a name that equals one thing OR another.


$collection = Mage::getModel('catalog/product')->getCollection();
//filter for products who name is equal (eq) to Widget A, or equal (eq) to Widget B
$collection->addFieldToFilter(array(array('attribute'=>'name','eq'=>'Widget A'), array('attribute'=>'name','eq'=>'Widget B'),));

A full list of the supported short conditionals (eq,lt, etc.) can be found in the _getConditionSql method in lib/Varien/Data/Collection/Db.php

Finally, all Magento collections may be iterated over (the base collection class implements on of the the iterator interfaces). This is how you'll grab your products once filters are set.



$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('name');
$collection->addAttributeToSelect('orig_price');
//filter for products who name is equal (eq) to Widget A, or equal (eq) to Widget B
$collection>addFieldToFilter(array(array('name'=>'orig_price','eq'=>'Widget A'),array('name'=>'orig_price','eq'=>'Widget B'),));
foreach ($collection as $product) {
var_dump($product->getData());
}

Solution by Alan Storm