Snippets with the tag: attribute

Get attribute option labels

Description

code to get attribute option labels

Code

public function getOptionsLabels($attribute_code)
{
	$options = Mage::getModel('eav/entity_attribute')
		->loadByCode('catalog_product', $attribute_code)
		->getSource()->getAllOptions(true, true);

	$labels = array();
	foreach($options as $option) {
		if (trim($option['label']) != '') {
		   $labels[] = $option['label'];
		}
	}
	return $labels;
}

Get attribute set collection

Description

get the entity attribute set collection

Code

$attributeSetCollection = Mage::getResourceModel('eav/entity_attribute_set_collection') ->load();

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

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 associated products

Description

Get all associated products

Code

helper('catalog/output'); ?>
getAllowProducts() ?>



productAttribute($_item, $_item->getName(), 'name'); ?> | getName(); ?> | getPrice(); ?>

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

Hersteller anzeigen

Description

Hersteller anzeigen

Code

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