Snippets with the tag: catalog

Display new products on home page

Description

code snippet to display new products on home page

Code

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

Display new products from specific category on home page

Description

code to display new products from a particular category on home page

Code

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

Get categories products ordered by number of sales

Description

Get categories products ordered by number of sales

Code

$categoryId = 1; 
$category = Mage::getModel('catalog/category')->load($categoryId); 
$products = Mage::getResourceModel('reports/product_collection') 
          ->addOrderedQty() 
          ->addAttributeToSelect('*') 
          ->setOrder('ordered_qty', 'desc') 
          ->addCategoryFilter($category);

Get all top level categories

Description

Get all of the top level categories. Usually, the first base category added will have the id of 2.

Code

$defaultCategoryId = 2; 
$topLevelCats = Mage::getModel('catalog/category') 
->getCollection() 
->addAttributeToSelect('path','name','id') 
->addAttributeToFilter('parent_id', $defaultCategoryId) 
->setOrder('position', 'ASC');

Check if RSS is enabled

Description

Check if RSS is enabled

Code

Mage::getStoreConfig('rss/catalog/category');

Get product type from product SKU

Description

Get product type from product SKU

Code

$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$product_sku);
$productType = $product->getTypeId();

Get product type from product ID

Description

Get product type from product ID

Code

$product = Mage::getModel('catalog/product')->load($product_id);
$productType = $product->getTypeId();

Reindex catalog

Description

Reindex catalog

Code

// clear cache
Mage::app()->removeCache('catalog_rules_dirty');
// reindex prices
Mage::getModel('index/process')->load(2)->reindexEverything();
/*
1 = Product Attributes
2 = Product Attributes
3 = Catalog URL Rewrites
4 = Product Flat Data
5 = Category Flat Data
6 = Category Products
7 = Catalog Search Index
8 = Tag Aggregation Data
9 = Stock Status
*/

Load products by category ID

Description

Load products by category ID, for example 12.

Code

$_category = Mage::getModel('catalog/category')->load(12);
$_productCollection = $_category->getProductCollection();
if($_productCollection->count()) {
    foreach( $_productCollection as $_product ):
        echo $_product->getProductUrl();
        echo $this->getPriceHtml($_product, true);
        echo $this->htmlEscape($_product->getName());
    endforeach;
}

Get product image

Description

Get product image

Code

$this->helper('catalog/image')->init($_product, 'image');

Get product stock quantity by id

Description

Get product stock quantity by id

Code

$id = 52;
$_product = Mage::getModel('catalog/product')->load($id);

$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product);

print_r($stock->getData());

echo $stock->getQty();
echo $stock->getMinQty();
echo $stock->getMinSaleQty();

Get product stock quantity by sku

Description

Get product stock quantity by sku

Code

$sku = "AB123456789";
$_product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);

$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product);

print_r($stock->getData());

echo $stock->getQty();
echo $stock->getMinQty();
echo $stock->getMinSaleQty();

Get actual and special price of a product

Description

Get actual and special price of a product

Code

$_productId = 102;
$_product = Mage::getModel('catalog/product')->load($_productId);

// without currency sign
$_actualPrice = number_format($_product->getPrice(), 2);
// with currency sign
$_formattedActualPrice = Mage::helper('core')->currency(number_format($_product->getPrice(), 2),true,false);

// without currency sign
$_specialPrice = $_product->getFinalPrice();
// with currency sign
$_formattedSpecialPrice = Mage::helper('core')->currency(number_format($_product->getFinalPrice(), 2),true,false);

Get / filter all products by attribute value

Description

Get / filter all products by attribute value

Get all products related to any particular brand. Let us suppose that we are fetching the products related to ‚Apple‘ brand. The manufacturer ID of Apple = 3.

Code

$manufacturerId = 3;
$attributeCode = 'manufacturer';

$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter($attributeCode, $manufacturerId);

// print all products
print_r($products->getItems());

Get a list of bestsellers

Description

Get a list of bestsellers

Code

$collection = Mage::getResourceModel('sales/report_bestsellers_collection')
        ->setModel('catalog/product')
        ->addStoreFilter(Mage::app()->getStore()->getId()) //if you want the bestsellers for a specific store view. if you want global values remove this
    ->setPageSize(5)//set the number of products you want
    ->setCurPage(1);
foreach ($collection as $_product){
    $realProduct = Mage::getModel('catalog/product')->load($_product->getProductId());
    //do something with $realProduct;
}

Shop by brand

Description

Shop by brand, for example in the sidebar.

Code

$product = Mage::getModel('catalog/product');

$attributes = Mage::getResourceModel('eav/entity_attribute_collection')->setEntityTypeFilter($product->getResource()->getTypeId())->addFieldToFilter('attribute_code', 'manufacturer');

$attribute = $attributes->getFirstItem()->setEntity($product->getResource());

$manufacturers = $attribute->getSource()->getAllOptions(false); ?>