Mage::registry('current_category')->getName();
code to check stock availability of product
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; }
code snippet 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”}}
code to display new products from a particular category on home page
{{block type="catalog/product_list" category_id="10" name="home.catalog.product.new" alias="product_homepage" template="catalog/product/new.phtml"}}
using this code, you can generate a csv file with product data
$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);
$_productCollection->printLogQuery(true);
$product->isConfigurable();
Get product type from product SKU
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$product_sku); $productType = $product->getTypeId();
Get product type from product ID
$product = Mage::getModel('catalog/product')->load($product_id); $productType = $product->getTypeId();
Check if there are any cross-sell products attached to a product.
$crosssell = $product->getCrossSellProducts(); if(count($crosssell)) { //product have cross-sell products... }
Check if a product is purchasable
if($_product->isSaleable()) { // do stuff }
Load products by category ID, for example 12.
$_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
$this->helper('catalog/image')->init($_product, 'image');
Get product stock quantity by id
$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
$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
$_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
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.
$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
$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
echo $_product->getAttributeText('manufacturer');
Shop by brand, for example in the sidebar.
$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); ?>
Show the product tax class name
$taxClassId = $_product->getTaxClassId(); $taxClass = Mage::getModel('tax/class')->load($taxClassId); $taxClassName = $taxClass->getClassName();