0

How to get all attribute of a product. I try to manage this line of code from internet or some other sources where i can easily able to fetch Product name,image,price and description but I failed to fetch all product Frontend and FrontendLabel like color,brand etc.I am new to php.So any help would be appreciate.

<?php 
    // Get products in category
    require_once('../app/Mage.php');//Path to Magento umask(0);

    if(isset($_GET["categoryId"])){
      $categoryId=$_GET["categoryId"];
        $proxy = new SoapClient('http://www.prashant.com/index.php/api/v2_soap/?wsdl'); // TODO : change url
        $sessionId = $proxy->login('prash', 'prashant123@'); // TODO : change login and pwd if necessary

        $products= $proxy->catalogCategoryAssignedProducts($sessionId,$categoryId);


        $itemDetails=array();
        Mage::app(); 

      foreach ($products as $item) { 

        $product2 = Mage::getModel('catalog/product')->load($item->product_id);
        $productMediaConfig = Mage::getModel('catalog/product_media_config');
        $baseImageUrl = $productMediaConfig->getMediaUrl($product2->getImage());
        $price2=0;
        $price2=$product2->getFinalPrice();
        if($price2==0){
        $price2=$product2->getPrice();
        }



        $desc=$product2->getDescription();
               if($desc==""){
                $desc="blank";
               }

                $attributes=$product2->getAttributes();
                foreach ($attributes as $attribute) {
                        if ($attribute->getIsVisibleOnFront()) {
                        $attributeCode = $attribute->getAttributeCode();
                        $label = $attribute->getFrontend()->getLabel($product2);        
                        $value = $attribute->getFrontend()->getValue($product2);
                        echo $attributeCode . '-' . $label . '-' . $value; echo "<br />";        
        }
    }



               $baseImageUrl = $productMediaConfig->getMediaUrl($product2->getImage());
               if($baseImageUrl=="http://www.prashant.com/media/catalog/product/"){
                $baseImageUrl="http://www.prashant.com/customApi/icon.png";
               }

               //"desc"=>$desc,

               $statusp=$product2->getStatus();
        $statusen= Mage_Catalog_Model_Product_Status::STATUS_ENABLED;


        if($statusp==$statusen){
             $itemDetails[]=array(
          "productId"=>$item->product_id,
          "name"=>$product2->getName(),
          "price"=>$product2->getPrice(),
          "spprice"=>$price2,
          "desc"=>$desc,
          "attributes"=>$attributes,
          "imageurl"=>$baseImageUrl,
          );
        }


      }
      //return array of products
      //echo "<pre>";
      //print_r($itemDetails);
      //echo "</pre>";
      echo json_encode($itemDetails);
      exit();
    }else{
      echo "error";
      exit();
    }
    ?>

2 Answers 2

0

You can try below code that will work for you.

$block = Mage::app()->getLayout()->createBlock('catalog/product_view_attributes');
    $_helper = Mage::helper('catalog/output');
    $_product = $product2;
    $_additional = $block->getAdditionalData();  
     foreach ($_additional as $_data): 

            echo $block->escapeHtml($this->__($_data['label'])); 
            echo $_helper->productAttribute($_product, $_data['value'], $_data['code']);
     endforeach; 
0

For the code seems to be fine,

I think there is some problem with your setting,

For fetching this way your attribute should display yes in view page.

Because I think This is the same kind of code you will get into catalog/product/view/attribute.phtml file

For this setting go to

Catalog->Attribute->Manage Attribute-> click on Your Attribute-> Frontend Properties ->Visible on Product View Page on Front-end set to Yes

In your code remove this if condition and might be It work for you

 $attributes=$product2->getAttributes();
            foreach ($attributes as $attribute) {
                    //if ($attribute->getIsVisibleOnFront()) { remove if condi
                    $attributeCode = $attribute->getAttributeCode();
                    $label = $attribute->getFrontend()->getLabel($product2);        
                    $value = $attribute->getFrontend()->getValue($product2);
                    echo $attributeCode . '-' . $label . '-' . $value; echo "<br />";        
   // } end remove if condi
}

May be this issue because of you have not set current store. please set current store and check add below code after Mage::app();

Mage::app()->setCurrentStore($your_store_id);
7
  • its already YES Dec 5, 2016 at 11:33
  • try this code $product2->getResource()->getAttribute('attribute_code')->getFrontend()->getValue($product2) Dec 5, 2016 at 11:40
  • again not working but when i write color in place of 'attribute_code' it shows black and i want it should fetch label name too. Dec 5, 2016 at 11:56
  • @PrashantDas So the code you have written worked only on home page only , let me check detail and I will answer you later for this Dec 5, 2016 at 11:58
  • @PrashantDas check my updated answer Dec 5, 2016 at 12:00

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.