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.

  • 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 – Ashish Ranade Dec 15 '16 at 10:33
  • Thanks, i am using two methods with if condition.i need the product attribute labels only. – Mujahidh Dec 15 '16 at 10:37
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, privacy policy and cookie policy

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