I am using two different ways to load the products in my magento page.
Method 1 : Load the category products by Category id
$productId = (int) trim($getId[0]);//HERE $productId IS A CATEGORY ID
$obj = Mage::getModel('catalog/category');
$_product = $obj->load($productId);
$category = Mage::getModel('catalog/category')->load($_product ->getId());
$productslist = Mage::getResourceModel('catalog/product_collection')
->setStoreId(Mage::app()->getStore()->getId())
->addCategoryFilter($category)
->addAttributeToSort('position', 'asc');
foreach ($productslist as $_productslist) { $arrIds[] = $_productslist->getId(); }
//$arrIds[0] :- this variable holds the first product id of the collection.
$product = Mage::getModel('catalog/product')->load($arrIds[0]);
$attributes = $product->getAttributes();
foreach ($attributes as $attribute) {
if ($attribute->getIsVisibleOnFront() && $attribute->getFrontend()->getValue($product) != "") {
$label = $attribute->getStoreLabel($product);
}
}
In This way i can use getStoreLabel
and retrieve the label values of the attribute for the product.
Method 2 : Load the products by Product name
$productName = (int) trim($_POST['partnumber']); //this is user search value for product name
$productslist = Mage::getModel('catalog/product')
->setStoreId(Mage::app()->getStore()->getId())
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('name', ['like' => $productName . '%']);
foreach ($productslist as $_productslist) { $arrIds[] = $_productslist->getId(); }
//$arrIds[0] :- this variable holds the first product id of the collection.
$product = Mage::getModel('catalog/product')->load($arrIds[0]);
$attributes = $product->getAttributes();
foreach ($attributes as $attribute) {
if ($attribute->getIsVisibleOnFront() && $attribute->getFrontend()->getValue($product) != "") {
$label = $attribute->getStoreLabel($product);
}
}
Here in method 2 a 404 page is coming. What is the mistake i am doing here.I used the same way to get the attribute and attribute label for both scenario.
UPDATE
I managed to fix this issue by removing $product
in $label = $attribute->getStoreLabel($product);
, I want to know why this argument work in method 1 and not work in method 2.Please explain me.