Magento getAttribute not working with listing product - magento

echo $_product->getResource()->getAttribute($attribute)->getFrontend()->getValue($_product);
This code is not working fine in view.phtml it is not return first attribute code value.
when i write this code on view page, it is not showing first product attribute and all after first in loop are showing fine.
This is my all code
<?php
$productAttributeTh = array('Color','Item','Size');
$configurableProduct = Mage::getModel('catalog/product')->load($_product->getId());
$childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null,$configurableProduct);
foreach($childProducts as $child) {
$product_id = $child->getId();
$obj = Mage::getModel('catalog/product');
$_childProduct = $obj->load($product_id); // Enter your Product Id in $product_id
foreach ($productAttributeTh as $key => $productAttributeValue){
$productAttribute = $_childProduct->getResource()->getAttribute($productAttributeValue)->getFrontend()->getValue($_childProduct);
echo $productAttribute;
}
} ?>

You need to make sure your attribute is set to be used in list. Go to;
Catalog > Attributes > Manage Attributes
Find your attribute, and open it. Scroll down to the option 'used in product listing' and set it 'yes'. Save and then reindex attributes.

Try this.
Mage::getResourceModel('catalog/product')->getAttributeRawValue($productId, 'attribute_code', $storeId);
Or
$attribute_value = $product->getResource()->getAttribute($attribute_code)->getFrontend()->getValue($product);
i hope this will help you.

Related

Get magento 2 custom attribute value

I am able to display attribute values using the code below BUT if the attribute is empty it just prints out the word "No"
<?php echo $_product->getResource()->getAttribute('c_address')->getFrontend()->getValue($_product); ?>
To get the customer attribute,you can use like this:
$customerRepository = $objectManager->get('Magento\Customer\Api\CustomerRepositoryInterface');
$customer = $customerRepository->getById(1);
$cattrValue = $customer->getCustomAttribute('c_address');
To get the product attribute,you can use like this:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Catalog\Model\Product')->load('YOUR PRODUCT ID');
echo $product->getAttributeText('your_attribut');
The simplest way is,
$customer = $CUSTOMER_OBJECT; // GET customer object
$customer->getCustomAttribute('variable_name')->getValue();
But you need to control $customer->getCustomAttribute('variable_name') is not NULL

Remove attribute value from product

We have attributes with a default value, even if the attribute is not in the products attribute set, the default value for these products are shown in layered navigation.
Example
My keyboards category gets Mouse attributes shown in layered navigation with the default attribute value. So all my keyboards get "Right handed".
How can i remove the attribute value for these products ?
Maybe something like this:
$catid = 'your cat. id';
$category = Mage::getModel('catalog/category');
$category->load($catid);
$prodCollection = $category->getProductCollection();
foreach ($prodCollection as $product) {
$prdIds[] = $product->getId(); ///Store all the product id in $prdIds array
}
foreach($prdIds as $product){
$attributeCode = "your attribute code";
$obj = Mage::getModel('catalog/product');
$_product = $obj->load($product);
$_product->setData($attributeCode, "")
->getResource()
->saveAttribute($_product, $attributeCode);
echo $_product->getName() . ' is succesfully corrected' . '<BR>'; ;
}
Please let me know if this helps you out.

Magento Show custom attribute dropdown option for a simple product in cart page

how to Show custom attribute dropdown option for a simple product in cart page
my attribute name is meal_time
<?php
$product = Mage::getModel('catalog/product')->load($_item->getProductId());
echo $product->getAttributeText('meal_time');
?>
To get the custom attribute option in the cart , first we need to get quote visible product items and then load the product to get its options.
$cart = Mage::getSingleton('checkout/session')->getQuote()->getAllVisibleItems();
foreach ($cart as $item) {
$product_id = $item->getProduct()->getId();
$_product = Mage::getModel('catalog/product')->load($product_id);
echo $_product->getMealTime();
}
To display all the option of an attribute
$cart = Mage::getSingleton('checkout/session')->getQuote()->getAllVisibleItems();
foreach ($cart as $item) {
$product_id = $item->getProduct()->getId();
$_product = Mage::getModel('catalog/product')->load($product_id);
$attribute = Mage::getModel('eav/config')-getAttribute('catalog_product', 'meal_time');
foreach ($attribute->getSource()->getAllOptions(true, true) as $instance) {
echo $myattribute[$instance['value']] = $instance['label'];
}
}
If you want to display as select dropdown , use select tag including the label and value as options

Magento - getting category custom attribute value

How to get current custom category attribute value in product list view?
I'm trying like this
$attribute = Mage::getModel('catalog/category')->getAttributes();
And I see it's there but how to get it?
My custom attribue name is catalog_pdf
Also tryed in this way, but get nothing:
$attribute = Mage::getModel('catalog/category')->getAttribute('catalog_category','catalog_pdf');
This should work.
If you are int the product list then you should have the current category in
Mage::registry('current_category');
So do like this:
$category = Mage::registry('current_category');
if ($category){ //this is necessary in case you are in a product listing that is's not a category
$value = $category->getData('catalog_pdf');//catalog_pdf is the attribute code
//or
//$value = $category->getCatalogPdf();
}
This should work:
$id = $this->getCurrentCategory()->getId();
$category = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getCode()->getId())->load($id);
echo $category->getData('catalog_pdf');
//or
echo $category->getCatalogPdf();
Edited to include missing get

Magento - How to get attributes of a child product of a bundled item

I need to retrieve a simple text attribute from a child product belonging to a bundled product if it exists. This needs to be done on the shopping cart page. The below is the code I've been messing around with to see what I can retrieve.
The file is template/checkout/cart/item/default.phtml in the theme folder.
$_item = $this->getItem();
$_product = $this->getProduct();
$_product = Mage::getModel('catalog/product')->load($_product->getId());
$isVisibleProduct = $_item->getProduct()->isVisibleInSiteVisibility();
$itemsCollection = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
foreach($itemsCollection as $item) {
echo $item->getId();
$_bProduct = Mage::getModel('catalog/product')->load($item->getId());
echo '<pre>';
var_dump($_bProduct);
echo '</pre>';
echo '<br>';
echo $_bProduct->getData('backorder_shipment_date');
}
To access custom attributes on all pages, you should use :
getItemCollection()
or
getProductCollection()
also, check this link Accessing Custom Product Attributes in the Cart/Checkout Area

Resources