Get magento 2 custom attribute value - magento

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

Related

Magento2.4: How to get Salable quantity in list.phtml file?

How can I get salable quantity on list.phtml or category page file, I want to show labels on products with 0 salable quantity.
Are there any other approaches without using object manager?
Please use this code in phtml file to get salable qty
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$StockState = $objectManager->get('\Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku');
$qty = $StockState->execute($_product->getSku());
echo($qty[0]['qty']);
Try the below code to get salable QTY.
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$StockState = $objectManager->get('\Magento\InventorySalesApi\Api\GetProductSalableQtyInterface');
$qty = $StockState->execute($_product->getSku(), 2);
?>
Either Object Manager is not a good approach, but you need to inject in your custom module such as:
namespace Cloudways\Module\ModelName;
use Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku;
Refer Link: https://magento.stackexchange.com/questions/301956/how-to-get-salable-qty-in-magento-2-3-3/302187#302187

Magento getAttribute not working with listing product

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.

How to show the value of an attribute in shopping cart?

I added an attribute 'items' having input type textbox in admin.I want to show the value of that attribute in shopping cart.For that i have added this code in template/checkout/cart/sidebar-top.phtml which is below:
<?php
$productId = $_item->getId();
$productInfo = Mage::getModel("catalog/product")->load($productId);
echo $productInfo->getAttributeText('product_type');
?>
but when i am adding this above code showing
Fatal error: Call to a member function getId() on a non-object
if anyone knows this,please help me out.thanks!
Use the below code
$productId = $_item->getProduct()->getId();
$productInfo = Mage::getModel("catalog/product")->load($productId);
echo $productInfo->getAttributeText('product_type');
Are you talking about custom option or simple attribute?
For Simple attribute (text) and if you are on product page try belo code:
<?php $_item = $this->getItem()?>
<?php $_product= Mage::getSingleton('catalog/product')->load($_item->getProductId()) ?>
<?php echo $_product->getResource()->getAttribute('attribute_code')->getFrontend()->getValue($_product); ?>
If you are not on the product page then you can use the below code, load the product by sku and get your attribute value
$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $_item->getSku());
echo $product->getResource()->getAttribute('attribute_code')->getFrontEnd()->getValue($product);

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

how to get sku and product name on the head.php page?

how to get sku and product name on the head.php (\app\code\core\Mage\Page\Block\Html\head.php) page? thank you.
when i used $this->_data['sku']; or $this->getSku(); are all not work.
The previous answer is fine, except it doesn't check if product exists in registry. So you will get fatal error on non-product pages. Always make a check if variable exists.
$product = Mage::registry('current_product');
if ($product) //sometimes need check for instanse, use instanseof
{
$product->getSku();
}
You should be able to pull the current product back from the registry and access the values from that.
$product = Mage::registry('current_product');
$product->getSku();
Working Code:
if($_product = Mage::registry('current_product')){
$id = $_product->getId();
$sku = $_product->getSku();
}

Resources