Snippets with the tag: product

Current category name

Description

Get the current category name

Code

Mage::registry('current_category')->getName();

Check stock availability of a product

Description

code to check stock availability of product

Code

protected function _isAvailable(Mage_Sales_Model_Order_Item $orderItem, $qty)
{
	if ($orderItem->getProductId()) {
		$stockItem = Mage::getModel('catalog/product')
				  ->load($orderItem->getProductId())
				  ->getStockItem();

		if ($stockItem->getIsInStock() && (int)$stockItem->getQty() >= $qty) {
			return true;
		}
	}
	return false;
}

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"}}

Generate a csv file

Description

using this code, you can generate a csv file with product data

Code

$file_path = “/your_custom_dir_path/sample.csv”;
//file path of the CSV file in which the data to be saved
$mage_csv = new Varien_File_Csv(); 
//product ids whose data to be written
$products_ids = array(1,2,3); 
//get products model
$products_model = Mage::getModel(‘catalog/product’); 
$products_row = array();
foreach ($products_ids as $pid)
{
$prod = $products_model->load($pid);
$data = array();
$data['sku'] = $prod->getSku();
$data['name'] = $prod->getName();
$data['price'] = $prod->getPrice();
$products_row[] = $data;
}
//write to csv file
$mage_csv->saveData($file_path, $products_row);

Print the SQL queries performed when calling a product collection

Description

Print the SQL queries performed when calling a product collection

Code

$_productCollection->printLogQuery(true);

Check if a product is configurable

Description

Check if a product is configurable.

Code

$product->isConfigurable();

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();

Check cross-sell products attached to a product

Description

Check if there are any cross-sell products attached to a product.

Code

$crosssell = $product->getCrossSellProducts();
if(count($crosssell)) {
    //product have cross-sell products...
}

Is product purchasable

Description

Check if a product is purchasable

Code

if($_product->isSaleable()) { // do stuff }

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;
}

Hersteller anzeigen

Description

Hersteller anzeigen

Code

echo $_product->getAttributeText('manufacturer');

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); ?>

<ul id="manufacturer_list">
<?php foreach ($manufacturers as $manufacturer): ?>
<li>
<a href="http://www.yourdomain.com/catalogsearch/advanced/result/?manufacturer[]=<?php echo $manufacturer['value'] ?>">
<?php echo $manufacturer['label'] ?>
</a>
</li>
<?php endforeach; ?>
</ul>

Display product tax class

Description

Show the product tax class name

Code

$taxClassId = $_product->getTaxClassId();
$taxClass = Mage::getModel('tax/class')->load($taxClassId);
$taxClassName = $taxClass->getClassName();