0

I am overriding Magento_sales core pdf file like following:

enter image description here

In di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Sales\Model\Order\Pdf" type="Mobistore\Modulesales\Model\Sales\Order\Pdf" />
</config>

IN module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Mobistore_Modulesales" setup_version="1.0.1">
<sequence>
    <module name="Magento_Sales"/>
</sequence>
</module>
</config>

In invoice.pdf

<?php
  /**
    * Copyright © Magento, Inc. All rights reserved.
    * See COPYING.txt for license details.
 */
 namespace Mobistore\Modulesales\Model\Sales\Order\Pdf;

/**
* Sales Order Invoice PDF model
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Invoice extends \Magento\Sales\Model\Order\Pdf\Invoice
{

 public function getPdf($invoices = [])
 {
    $this->_beforeGetPdf();
    $this->_initRenderer('invoice');

    $pdf = new \Zend_Pdf();
    $this->_setPdf($pdf);
    $style = new \Zend_Pdf_Style();
    $this->_setFontBold($style, 10);

    foreach ($invoices as $invoice) {
        if ($invoice->getStoreId()) {
            $this->_localeResolver->emulate($invoice->getStoreId());
            $this->_storeManager->setCurrentStore($invoice->getStoreId());
        }
        $page = $this->newPage();
        $order = $invoice->getOrder();
        /* Add image */
        $this->insertLogo($page, $invoice->getStore());
        /* Add address */
        $this->insertAddress($page, $invoice->getStore());
        /* Add head */
        $this->insertOrder(
            $page,
            $order,
            $this->_scopeConfig->isSetFlag(
                self::XML_PATH_SALES_PDF_INVOICE_PUT_ORDER_ID,
                \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
                $order->getStoreId()
            )
        );
        /* Add document text and number */
        $this->insertDocumentNumber($page, __('Invoice # ') . $invoice->getIncrementId());
        /* Add table */
        $this->_drawHeader($page);
        /* Add body */
        foreach ($invoice->getAllItems() as $item) {
            if ($item->getOrderItem()->getParentItem()) {
                continue;
            }
            /* Draw item */
            $this->_drawItem($item, $page, $order);
            $page = end($pdf->pages);
        }
        /* Add totals */
        $this->insertTotals($page, $invoice);
        if ($invoice->getStoreId()) {
            $this->_localeResolver->revert();
        }

    }


    foreach ($invoice->getAllItems() as $item) {
        if ($item->getOrderItem()->getParentItem()) {
            continue;
        }
        $sku                =   $item->getSku();

        $objectManager      =   \Magento\Framework\App\ObjectManager::getInstance();
        $id                 =   $objectManager->get('Magento\Catalog\Model\Product')->getIdBySku($sku);
        $product            =   $objectManager->get('Magento\Catalog\Model\Product')->load($id);
        $refurbComment      =   $product->getRefurbComment();
        $is_refurb          =   $product->getAttributeText('is_refurb');
        if($is_refurb=='Yes'){
            $this->insertConditions($page,$refurbComment);
        }

    }


    $this->_afterGetPdf();
    return $pdf;
}


public function insertConditions($page,$comment)
{
    $this->y -= 25;
    $page->drawText($comment, 35, $this->y, 'UTF-8');
}   


}

But it is still running the core model file

I made changes in the getPdf() function but not working

I run these command also number of times:

php bin/magento module:enable Magenticians_Moduleproduct
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento cache:clean
php bin/magento cache:flush

I followed this tutorial on how to extend MODEL files

https://magenticians.com/override-model-magento-2/

Any help in letting me know where I am doing wrong.

0

Update your preference of di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Sales\Model\Order\Pdf\Invoice" type="Mobistore\Modulesales\Model\Sales\Order\Pdf\Invoice" />
</config>

Then after check.

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.