0

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.

2
  • in your foreach you have used the same variable $productslist, if I am not wrong you just need a product name right ? or the store label please confirm Commented Dec 15, 2016 at 10:33
  • Thanks, i am using two methods with if condition.i need the product attribute labels only.
    – Mujahidh
    Commented Dec 15, 2016 at 10:37

1 Answer 1

0

I think you don't need to load whole collection if you just want the data of one product. please check the below code , hope this will help you.

$prod = Mage::getModel('catalog/product')->load(5); // add your product id
$attributes = $prod->getAttributes();
$labels = array();
foreach ($attributes as $attribute) {
    if (!is_null($attribute->getFrontendLabel())) { // you can get any value you want 
        array_push($labels, $attribute->getFrontendLabel());
// alternate code 

$attributeModel = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product','attrbute_code'); // add attribute code here
array_pust($labels,$attributeModel->getStoreLabel('Store_id')); //add your store id.

    }
}

Sorry about the messy code :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.