0

I'm interested in getting frontend input strings. I understand that there are better ways other than checking for the "frontend_input" key, but I'm wanting a proof of concept. Here's my code:

$attributesForUpload = [];
$collection = $this->_filter->getCollection($this->_collectionFactory->create());
foreach ($collection->getItems() as $product) {
   $productId = $product->getId();
   $attributes = $product->getAttributes();
   foreach ($attributes as $attribute) {
       $data = $attribute->getData();
       if (isset($data['frontend_input'])) {
           if ($data['frontend_input'] === 'text' || $data['frontend_input'] === 'textarea') {
               $attributeCode = $data['attribute_code'];
               var_dump($attributeCode);
               $translatableValue = $product->getData($attributeCode);
               var_dump($translatableValue);
               $attributesForUpload[$productId][$attributeCode] = $translatableValue;
           }
       }
   }
}

Among others, product name, description, and short description are all null. Only the SKU is being returned. I'm wondering why this is since I specifically save them in the UI; I'm sure I'm missing something simple.

enter image description here

3 Answers 3

0

Try replace this

$this->_filter->getCollection($this->_collectionFactory->create());

with this

$this->_filter->getCollection($this->_collectionFactory->create()->addAttributeToSelect('*'));
0

It turns out that the product maybe wasn't really the full product. I had to inject an \Magento\Catalog\Model\ProductRepository object into my constructor and call:

$theProduct = $this->_productRepository->getById($productId);

Then the attributes weren't null anymore.

0

If you want to get a product custom attribute you can access it like this:

/** @var $product \Magento\Catalog\Api\Data\ProductInterface */
$product = $this->_productRepository->getById($productId);

$myCustomAttribute = $product->getCustomAttribute('my_custom_attribute')->getValue();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.