0

I will tell what i have done till now.

I have made a custom module to override the

Magento\Sales\Model\Order\Pdf\Items\Invoice\DefaultInvoice

file and its working fine.

I am able to get sku, name, price in the invoice pdf in any location. Now i am trying to get the product object inside it to get custom attribute values.

Most of the tutorials i found is advised to use objectmanager that i am not interested in looking into. Rather i need to include those via factory or repository.

my question is how we can get product object inside model file(for example defaultinvoice.php file).

Please advise.

0

ProductRepository dependency injection Method: Need to rewrite this file (DefaultInvoice.php) in app/code..

E:\kjn\magento\vendor\magento\module-sales\Model\Order\Pdf\Items\Invoice\DefaultInvoice.php

Add bellow code before \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,

\Magento\Catalog\Model\ProductRepository $productRepository,

and after $this->string = $string; add

$this->_productRepository = $productRepository;

Before the function public function draw() put

 public function getProductById($id)
{
    return $this->_productRepository->getById($id);
}

Then you can call the function getProductById by passing the product id. passing SKU is another method.

Load the product id from $item

$ProductId = $item->getOrderItem()->getProductId();

then calling product collection function

$product = $this->getProductById($ProductId);

By using the $product collection. i have get the product custom attribute value

 $SizeUnit = '';
            if (strtolower($option['label']) == strtolower($product->getLengthName()))
            {
                $SizeUnit = ' ' . $product->getResource()->getAttribute('size_units')->getFrontend()->getValue($product);
            }

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.