获取magento 2自定义属性值

jpfvwuh4  于 2022-11-12  发布在  其他
关注(0)|答案(2)|浏览(170)

我可以使用下面的代码显示属性值,但如果属性为空,则只会打印出“否”

<?php echo $_product->getResource()->getAttribute('c_address')->getFrontend()->getValue($_product); ?>
kh212irz

kh212irz1#

要获取customer属性,可以像这样使用:

$customerRepository = $objectManager->get('Magento\Customer\Api\CustomerRepositoryInterface');
$customer = $customerRepository->getById(1);
$cattrValue = $customer->getCustomAttribute('c_address');

要获取产品属性,可以像这样使用:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Catalog\Model\Product')->load('YOUR PRODUCT ID');
echo $product->getAttributeText('your_attribut');
qncylg1j

qncylg1j2#

最简单的方法是,

$customer = $CUSTOMER_OBJECT; // GET customer object
$customer->getCustomAttribute('variable_name')->getValue();

但需要控制$customer->getCustomAttribute('variable_name')不为NULL

相关问题