0

I have custom attribute value in my store.

How can I get the product id based on that custom attribute value?

0

To fetch value of attribute Use:

First get Your Product Collection and then

$this->getProduct()->getAttributeText('attribute_text');

e.g: $this->getProduct()->getAttributeText('capacity');

Or

$this->getProduct()->getResource()->getAttributeRawValue($this->getProduct()->getId(),'capacity',$this->_storeManager->getStore()->getId());

You can check this question as reference:

How to show the dropdown attibute value using getData() method in magento 2?

Hopefully, It will help you

1
  • Also var_dump your product collection using get_class_methods(). Will find getCapacity() methods if your attribute was correctly configured. Jul 2 '18 at 7:03
0

check loadByAttribute fucntion of product model Magento\Catalog\Model\AbstractModel class, it will give you idea how to get product id / object based on attribute code and value:

/**
 * Load entity by attribute
 *
 * @param \Magento\Eav\Model\Entity\Attribute\AttributeInterface|integer|string|array $attribute
 * @param null|string|array $value
 * @param string $additionalAttributes
 * @return bool|\Magento\Catalog\Model\AbstractModel
 */
public function loadByAttribute($attribute, $value, $additionalAttributes = '*')
{
    $collection = $this->getResourceCollection()->addAttributeToSelect(
        $additionalAttributes
    )->addAttributeToFilter(
        $attribute,
        $value
    )->setPage(
        1,
        1
    );

    foreach ($collection as $object) {
        return $object;
    }
    return false;
}

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.