0

I need to get values from some custom attributes from products. I'm using separate php file outside Magento folder and I'm using mostly $shippingRates to get the information I need.

I need to first load the product from SKU. I got SKU from Order.as follows,

$orderObj = $objectManager->create('Magento\Sales\Model\Order')->load($orderID);
$items =$orderObj->getAllVisibleItems();
foreach($items as $item):
$itemSku = $item->getSku();
endforeach;

I need to filter the product by SKU to get only needed products. But the way I did wasn't a success.

So I tried loading products separately.

I loaded product using ProductRepository and Product Model,

$productRepository = $objectManager->get('\Magento\Catalog\Model\ProductRepository');

and

$getProduct = $objectManager->get('\Magento\Catalog\Model\Product');

I tried to filter product loading by sku as follows,

$productRepository = $objectManager->get('\Magento\Catalog\Model\ProductRepository');
$skuLoopProd = $item->getSku();
$getProduct = $productRepository->get($skuLoopProd);

And I tried as follows to get custom attribute value,

$axs = $getProduct->getResource()->getAttribute('product_weight_class')->getOptions();
foreach ($axs as $keyAttr) {
echo $keyAttr->getLabel();
}

but in this, I got all available options for the attribute, not the attribute value applied to the product.

What was wrong with what I tried so far?

Can please someone help me to get text value from a custom attribute of a product that I can filter with SKU?

1 Answer 1

1

First get the product ID using Product Repository and Object manager

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

// Get the Product Repository
$productRepository = $objectManager->get('\Magento\Catalog\Model\ProductRepository');

// Get the Product Id using sku
$product = $productRepository->get($productSku);

// Create the product object using product ID
$product = $productRepository->getById( $product->getId());

// Call the Attributes 
$product->getData('product_weight_class');
or
$product->getAttributeText('product_weight_class');

Try this and update.

7
  • I use this inside foreach loop. As I mentioned in my question. foreach($items as $item): //I used your code here. endforeach; So I need to get custom attribute to few products. Problem is, when I use this, I only get attribute value for one product and loop gets stopped next time It comes to above code.
    – Joey
    Apr 19, 2018 at 10:21
  • @Joey - Did you debug the values ?
    – Lasantha
    Apr 19, 2018 at 10:37
  • Yeah, it was caused by older orders. some products were updated with new skus. but in Order, they were'n updated. so there was a conflict
    – Joey
    Apr 19, 2018 at 12:43
  • @Joey - You can use product id without using sku, try to get the product ID.
    – Lasantha
    Apr 19, 2018 at 16:16
  • still the same. Fatal error on=> Fatal error: Uncaught exception 'Magento\Framework\Exception\NoSuchEntityException' with message 'Requested product doesn't exist' => Magento\Catalog\Model\ProductRepository\Interceptor->getById('15') #2
    – Joey
    Apr 20, 2018 at 5:16

Your Answer

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

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