I'm actually working on a website in Magento 2.

I have to print entire order in PDF.

But I don't know how can I get the products attribute value.

There is my code :

 foreach ($order->getAllItems() as $item) {

                $product = $item->getProduct();
                $itemName = $item->getName();
                $itemAdr = $product->getAdressage();
                $itemQty = (int)($item->getData('qty_ordered'));
                $itemEan = $item->getProduct()->getData('ean_code');
                $sku = $item->getProductId();

                $page->drawText($itemQty,20,$y, 'UTF-8');
                $page->drawText($itemAdr,45, $y, 'UTF-8');
                $page->drawText($itemName, 100, $y, 'UTF-8');
                $page->drawText($itemEan, 500,$y, 'UTF-8');
                $page->drawText($sku, 550,$y,'UTF-8');
                $y = $y - 20;

I can only get itemName and Quantity Ordered...

Can you help me on this feature ?

Thanks in advance


To get product attributes, you need to create product collection factory, pass product_id in it and call function to get attribute value. See below code to get guidance. Inside your class add product model factory:

protected $product;
    public function __construct(
        \Magento\Catalog\Model\ProductFactory $product
    ) {
        $this->product = $product; 

and inside your foreach loop:

  $_product = $this->product->create()->load($item->getProduct()->getId());
 // suppose product has "no_shipping_method" attribute, you can get it's value by calling function like getNoShippingMethod() as shown below
            if ($_product->getNoShippingMethod()) {
                //your code in true case;

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.