2

I have in some Vendor's Module di.xml file this entry :

<type name="Amasty\RequestQuote\Model\Pdf\PdfInformation">
    <arguments>
        <argument name="variables" xsi:type="array">
            <item name="0" xsi:type="array">
                <item name="objectType" xsi:type="string">quote</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::USERNAME</item>
                <item name="method" xsi:type="string">getCustomerName</item>
            </item>
            <item name="1" xsi:type="array">
                <item name="objectType" xsi:type="string">custom</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::CUSTOMER_STREET</item>
                <item name="method" xsi:type="string">getBillingStreet</item>
            </item>
            <item name="2" xsi:type="array">
                <item name="objectType" xsi:type="string">billing</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::CUSTOMER_CITY</item>
                <item name="method" xsi:type="string">getCity</item>
            </item>
            <item name="3" xsi:type="array">
                <item name="objectType" xsi:type="string">custom</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::CUSTOMER_REGION</item>
                <item name="method" xsi:type="string">getBillingRegion</item>
            </item>
            <item name="4" xsi:type="array">
                <item name="objectType" xsi:type="string">billing</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::CUSTOMER_POSTCODE</item>
                <item name="method" xsi:type="string">getPostcode</item>
            </item>
            <item name="5" xsi:type="array">
                <item name="objectType" xsi:type="string">custom</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::CUSTOMER_COUNTRY</item>
                <item name="method" xsi:type="string">getBillingCountryName</item>
            </item>
            <item name="6" xsi:type="array">
                <item name="objectType" xsi:type="string">billing</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::CUSTOMER_TELEPHONE</item>
                <item name="method" xsi:type="string">getTelephone</item>
            </item>
            <item name="7" xsi:type="array">
                <item name="objectType" xsi:type="string">quote</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::QUOTE_NUMBER</item>
                <item name="method" xsi:type="string">getIncrementId</item>
            </item>
            <item name="8" xsi:type="array">
                <item name="objectType" xsi:type="string">quote</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::QUOTE_STATUS</item>
                <item name="method" xsi:type="string">getStatusLabel</item>
            </item>
            <item name="9" xsi:type="array">
                <item name="objectType" xsi:type="string">quote</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::QUOTE_DATE</item>
                <item name="method" xsi:type="string">getCreatedAt</item>
            </item>
            <item name="10" xsi:type="array">
                <item name="objectType" xsi:type="string">quote</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::QUOTE_EXPIRY_DATE</item>
                <item name="method" xsi:type="string">getExpiredDate</item>
            </item>
            <item name="11" xsi:type="array">
                <item name="objectType" xsi:type="string">custom</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::SHIPPING_STREET</item>
                <item name="method" xsi:type="string">getShippingStreet</item>
            </item>
            <item name="12" xsi:type="array">
                <item name="objectType" xsi:type="string">shipping</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::SHIPPING_CITY</item>
                <item name="method" xsi:type="string">getCity</item>
            </item>
            <item name="13" xsi:type="array">
                <item name="objectType" xsi:type="string">custom</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::SHIPPING_REGION</item>
                <item name="method" xsi:type="string">getShippingRegion</item>
            </item>
            <item name="14" xsi:type="array">
                <item name="objectType" xsi:type="string">shipping</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::SHIPPING_POSTCODE</item>
                <item name="method" xsi:type="string">getPostcode</item>
            </item>
            <item name="15" xsi:type="array">
                <item name="objectType" xsi:type="string">custom</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::SHIPPING_COUNTRY</item>
                <item name="method" xsi:type="string">getShippingCountryName</item>
            </item>
            <item name="16" xsi:type="array">
                <item name="objectType" xsi:type="string">custom</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::SHIPPING_TELEPHONE</item>
                <item name="method" xsi:type="string">getTelephone</item>
            </item>
            <item name="17" xsi:type="array">
                <item name="objectType" xsi:type="string">custom</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::SHIPPING_METHOD</item>
                <item name="method" xsi:type="string">getShippingMethod</item>
            </item>
            <item name="18" xsi:type="array">
                <item name="objectType" xsi:type="string">custom</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::PRODUCT_GRID</item>
                <item name="method" xsi:type="string">getProductGrid</item>
            </item>
            <item name="19" xsi:type="array">
                <item name="objectType" xsi:type="string">custom</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::STORE_PHONE</item>
                <item name="method" xsi:type="string">getStorePhone</item>
            </item>
            <item name="20" xsi:type="array">
                <item name="objectType" xsi:type="string">custom</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::HAS_SHIPPING_INFO</item>
                <item name="method" xsi:type="string">hasShippingInfo</item>
            </item>
            
        </argument>
    </arguments>
</type>

I would like to add in this another argument like

<item name="21" xsi:type="array">
                <item name="objectType" xsi:type="string">custom</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::QUOTE_REMARKS</item>
                <item name="method" xsi:type="string">getComments</item>
            </item>

I was able to ovveride the Model (with preferences in my own custom module di.xml), but how should I edit the original parameters ?

Thanks


Ill try to be a bit more descriptive.

What I try is to override some third party module; I succeed to do what I want, if I edit directly the vendor's files.

This is the vendor's edited di.xml file (see at the end)

<?xml version="1.0"?>
 
...


<type name="Amasty\RequestQuote\Model\Pdf\PdfInformation">
    <arguments>
        <argument name="variables" xsi:type="array">
            <item name="0" xsi:type="array">
                <item name="objectType" xsi:type="string">quote</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::USERNAME</item>
                <item name="method" xsi:type="string">getCustomerName</item>
            </item>
            <item name="1" xsi:type="array">
                <item name="objectType" xsi:type="string">custom</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::CUSTOMER_STREET</item>
                <item name="method" xsi:type="string">getBillingStreet</item>
            </item>
            <item name="2" xsi:type="array">
                <item name="objectType" xsi:type="string">billing</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::CUSTOMER_CITY</item>
                <item name="method" xsi:type="string">getCity</item>
            </item>
            <item name="3" xsi:type="array">
                <item name="objectType" xsi:type="string">custom</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::CUSTOMER_REGION</item>
                <item name="method" xsi:type="string">getBillingRegion</item>
            </item>
            <item name="4" xsi:type="array">
                <item name="objectType" xsi:type="string">billing</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::CUSTOMER_POSTCODE</item>
                <item name="method" xsi:type="string">getPostcode</item>
            </item>
            <item name="5" xsi:type="array">
                <item name="objectType" xsi:type="string">custom</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::CUSTOMER_COUNTRY</item>
                <item name="method" xsi:type="string">getBillingCountryName</item>
            </item>
            <item name="6" xsi:type="array">
                <item name="objectType" xsi:type="string">billing</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::CUSTOMER_TELEPHONE</item>
                <item name="method" xsi:type="string">getTelephone</item>
            </item>
            <item name="7" xsi:type="array">
                <item name="objectType" xsi:type="string">quote</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::QUOTE_NUMBER</item>
                <item name="method" xsi:type="string">getIncrementId</item>
            </item>
            <item name="8" xsi:type="array">
                <item name="objectType" xsi:type="string">quote</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::QUOTE_STATUS</item>
                <item name="method" xsi:type="string">getStatusLabel</item>
            </item>
            <item name="9" xsi:type="array">
                <item name="objectType" xsi:type="string">quote</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::QUOTE_DATE</item>
                <item name="method" xsi:type="string">getCreatedAt</item>
            </item>
            <item name="10" xsi:type="array">
                <item name="objectType" xsi:type="string">quote</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::QUOTE_EXPIRY_DATE</item>
                <item name="method" xsi:type="string">getExpiredDate</item>
            </item>
            <item name="11" xsi:type="array">
                <item name="objectType" xsi:type="string">custom</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::SHIPPING_STREET</item>
                <item name="method" xsi:type="string">getShippingStreet</item>
            </item>
            <item name="12" xsi:type="array">
                <item name="objectType" xsi:type="string">shipping</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::SHIPPING_CITY</item>
                <item name="method" xsi:type="string">getCity</item>
            </item>
            <item name="13" xsi:type="array">
                <item name="objectType" xsi:type="string">custom</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::SHIPPING_REGION</item>
                <item name="method" xsi:type="string">getShippingRegion</item>
            </item>
            <item name="14" xsi:type="array">
                <item name="objectType" xsi:type="string">shipping</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::SHIPPING_POSTCODE</item>
                <item name="method" xsi:type="string">getPostcode</item>
            </item>
            <item name="15" xsi:type="array">
                <item name="objectType" xsi:type="string">custom</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::SHIPPING_COUNTRY</item>
                <item name="method" xsi:type="string">getShippingCountryName</item>
            </item>
            <item name="16" xsi:type="array">
                <item name="objectType" xsi:type="string">custom</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::SHIPPING_TELEPHONE</item>
                <item name="method" xsi:type="string">getTelephone</item>
            </item>
            <item name="17" xsi:type="array">
                <item name="objectType" xsi:type="string">custom</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::SHIPPING_METHOD</item>
                <item name="method" xsi:type="string">getShippingMethod</item>
            </item>
            <item name="18" xsi:type="array">
                <item name="objectType" xsi:type="string">custom</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::PRODUCT_GRID</item>
                <item name="method" xsi:type="string">getProductGrid</item>
            </item>
            <item name="19" xsi:type="array">
                <item name="objectType" xsi:type="string">custom</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::STORE_PHONE</item>
                <item name="method" xsi:type="string">getStorePhone</item>
            </item>
            <item name="20" xsi:type="array">
                <item name="objectType" xsi:type="string">custom</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::HAS_SHIPPING_INFO</item>
                <item name="method" xsi:type="string">hasShippingInfo</item>
            </item>
            <item name="21" xsi:type="array">
                <item name="objectType" xsi:type="string">custom</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::QUOTE_REMARKS</item>
                <item name="method" xsi:type="string">getComments</item>
            </item>
        </argument>
    </arguments>
</type>

...

I just added the last argument :

<item name="21" xsi:type="array">
                <item name="objectType" xsi:type="string">custom</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::QUOTE_REMARKS</item>
                <item name="method" xsi:type="string">getComments</item>
            </item>

This is the edited Amasty/RequestQuote/Model/Pdf/PdfInformation.php

class PdfInformation { 
private $quote;
private $variables;
private $billing;
private $shipping;
private $dataObjectFactory;
private $countryInformationAcquirer;
private $status;
private $productGrid;
private $addressRepository;
private $registry;
private $scopeConfig;

public function __construct(
    DataObjectFactory $dataObjectFactory,
    CountryInformationAcquirerInterface $countryInformationAcquirer,
    Status $status,
    Items $productGrid,
    AddressRepository $addressRepository,
    Registry $registry,
    ScopeConfigInterface $scopeConfig,
    array $variables = []
) {
    $this->variables = $variables;
    $this->dataObjectFactory = $dataObjectFactory;
    $this->countryInformationAcquirer = $countryInformationAcquirer;
    $this->status = $status;
    $this->productGrid = $productGrid;
    $this->addressRepository = $addressRepository;
    $this->registry = $registry;
    $this->scopeConfig = $scopeConfig;
}

Where I just added the following function :

private function getComments(): string
{
    $json = $this->quote->getRemarks();
    if ($json != ""){
        $object = json_decode($json);
        foreach($object as $key=>$value){
            $result = $key." : ".$value."<br />";
        }
    }
    else {
        $result = "";
    }

    return (string)$result;
}

And finally, I edited the file Amasty\RequestQuote\Model\Source\PdfVariables to add the constant REMARKS, so it becomes

class PdfVariables implements OptionSourceInterface {

public const USERNAME = 'username'; public const CUSTOMER_STREET = 'customer_street'; public const CUSTOMER_CITY = 'customer_city'; public const CUSTOMER_REGION = 'customer_region'; public const CUSTOMER_POSTCODE = 'customer_postcode'; public const CUSTOMER_COUNTRY = 'customer_country'; public const CUSTOMER_TELEPHONE = 'customer_telephone'; public const QUOTE_NUMBER = 'quote_number'; public const QUOTE_STATUS = 'quote_status'; public const QUOTE_DATE = 'quote_date'; public const QUOTE_EXPIRY_DATE = 'quote_expiry_date'; public const HAS_SHIPPING_INFO = 'has_shipping_info'; public const SHIPPING_STREET = 'shipping_street'; public const SHIPPING_CITY = 'shipping_city'; public const SHIPPING_REGION = 'shipping_region'; public const SHIPPING_POSTCODE = 'shipping_postcode'; public const SHIPPING_COUNTRY = 'shipping_country'; public const SHIPPING_TELEPHONE = 'shipping_telephone'; public const SHIPPING_METHOD = 'shipping_method'; public const PRODUCT_GRID = 'product_grid'; public const STORE_PHONE = 'store_phone'; public const QUOTE_REMARKS = 'quote_remarks';

/**
 * @return array|array[]
 */
public function toOptionArray()
{
    return [
        [
            'value' => self::USERNAME,
            'label' => __('Username')
        ],
        [
            'value' => self::CUSTOMER_STREET,
            'label' => __('Customer Street')
        ],
        [
            'value' => self::CUSTOMER_REGION,
            'label' => __('Customer Region')
        ],
        [
            'value' => self::CUSTOMER_POSTCODE,
            'label' => __('Customer Postcode')
        ],
        [
            'value' => self::CUSTOMER_COUNTRY,
            'label' => __('Customer Country')
        ],
        [
            'value' => self::CUSTOMER_TELEPHONE,
            'label' => __('Customer Telephone')
        ],
        [
            'value' => self::QUOTE_NUMBER,
            'label' => __('Quote Number')
        ],
        [
            'value' => self::QUOTE_STATUS,
            'label' => __('Quote Status')
        ],
        [
            'value' => self::QUOTE_DATE,
            'label' => __('Quote Date')
        ],
        [
            'value' => self::QUOTE_EXPIRY_DATE,
            'label' => __('Quote Expiry Date')
        ],
        [
            'value' => self::HAS_SHIPPING_INFO,
            'label' => __('Has Shipping Info')
        ],
        [
            'value' => self::SHIPPING_STREET,
            'label' => __('Shipping Street')
        ],
        [
            'value' => self::SHIPPING_CITY,
            'label' => __('Shipping City')
        ],
        [
            'value' => self::SHIPPING_REGION,
            'label' => __('Shipping Region')
        ],
        [
            'value' => self::SHIPPING_POSTCODE,
            'label' => __('Shipping Postcode')
        ],
        [
            'value' => self::SHIPPING_COUNTRY,
            'label' => __('Shipping Country')
        ],
        [
            'value' => self::SHIPPING_TELEPHONE,
            'label' => __('Shipping Telephone')
        ],
        [
            'value' => self::SHIPPING_METHOD,
            'label' => __('Shipping Method')
        ],
        [
            'value' => self::PRODUCT_GRID,
            'label' => __('Product Grid')
        ],
        [
            'value' => self::STORE_PHONE,
            'label' => __('Store Phone')
        ],
        [
            'value' => self::QUOTE_REMARKS,
            'label' => __('Quote Remarks')
        ],
    ];
}

}

My question is : how to implement this with a custopm module without editing the vendor ?

These are my files into my own module Paul/RequestQuote/

my di.xml in Paul/RequestQuote/etc/

<preference for="Amasty\RequestQuote\Model\Pdf\PdfInformation"
            type="Paul\RequestQuote\Model\Pdf\PdfInformation" />

<preference for="Amasty\RequestQuote\Model\Source\PdfVariables"
            type="Paul\RequestQuote\Model\Source\PdfVariables" />

<type name="Amasty\RequestQuote\Model\Pdf\PdfInformation">
    <arguments>
        <argument name="variables" xsi:type="array">
            <item name="21" xsi:type="array">
                <item name="objectType" xsi:type="string">custom</item>
                <item name="variable" xsi:type="const">\Paul\RequestQuote\Model\Source\PdfVariables::QUOTE_REMARKS</item>
                <item name="method" xsi:type="string">getComments</item>
            </item>
        </argument>
    </arguments>
</type>

This is my new Paul/RequestQuote/Model/Pdf/PdfInformation.php

namespace Paul\RequestQuote\Model\Pdf;
use Amasty\RequestQuote\Model\Quote;
class PdfInformation extends \Amasty\RequestQuote\Model\Pdf\PdfInformation {
private $quote; 

private function getComments(): string

{ $json = $this->quote->getRemarks(); if ($json != ""){ $object = json_decode($json); foreach($object as $key=>$value){ $result = $key." : ".$value."
"; } } else { $result = ""; }

return (string)$result;

}

And finally,; for my new Class PdfVariables, I created a Paul/RequestQuote/Model/Source/Pdfvariables and added the constant like :

namespace Paul\RequestQuote\Model\Source;
use Magento\Framework\Data\OptionSourceInterface;
class PdfVariables extends \Amasty\RequestQuote\Model\Source\PdfVariables
{ ... and I add the constant and edited fuction

It does not work. I get the following error :

Firstly, the errors says that I'm trying to call a private function (getComments() in my overrided Pdfinformation class) from the amasty PdfInformation file.

Then I did change the "private" function getComments to "public"...but in that case the object "quote" is empty (it doesnt inherit from the original $this->quote

I got this error :

Error: Call to a member function getRemarks() on null in /var/www/site/app/code/Paul/RequestQuote/Model/Pdf/PdfInformation.php:20 Stack trace: #0 /var/www/site/vendor/amasty/module-request-quote/Model/Pdf/PdfInformation.php(115): Paul\RequestQuote\Model\Pdf\PdfInformation->getComments() #1 /var/www/site/vendor/amasty/module-request-quote/Model/Pdf/PdfInformation.php(106): Amasty\RequestQuote\Model\Pdf\PdfInformation->g ...

3 Answers 3

0

Define the virtualType type of PdfInformation Class like below and try it:

<virtualType name="your_custom_name" type="Amasty\RequestQuote\Model\Pdf\PdfInformation">
        <arguments>
           <argument name="variables" xsi:type="array">
                 <item name="21" xsi:type="array">
                    <item name="objectType" xsi:type="string">custom</item>
                    <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::QUOTE_REMARKS</item>
                    <item name="method" xsi:type="string">getComments</item>
                 </item>
            </argument>
        </arguments>
</virtualType>

And try also this link as reference : https://bitbucket.org/magstaging/blockwithcustomlogger/src/master/

0

To answer Tu Van, This is the full PdfInformation.php Vendor's file

    <?php
declare(strict_types=1);

namespace Amasty\RequestQuote\Model\Pdf;

use Amasty\RequestQuote\Block\Pdf\Items;

use Amasty\RequestQuote\Model\Quote;

use Amasty\RequestQuote\Model\Registry;

use Amasty\RequestQuote\Model\RegistryConstants;

use Amasty\RequestQuote\Model\Source\Status;

use Magento\Customer\Model\ResourceModel\AddressRepository;

use Magento\Directory\Api\CountryInformationAcquirerInterface;

use Magento\Framework\App\Config\ScopeConfigInterface;

use Magento\Framework\DataObjectFactory;

use Magento\Framework\Exception\NoSuchEntityException;

use Magento\Quote\Model\Quote\Address;

use Magento\Store\Model\Information;

class PdfInformation
{

    /**
     * @var Quote
     */

    private $quote;

    /**
     * @var array
     */
    private $variables;

    /**
     * @var Address
     */
    private $billing;

    /**
     * @var Address
     */
    private $shipping;

    /**
     * @var DataObjectFactory
     */
    private $dataObjectFactory;

    /**
     * @var CountryInformationAcquirerInterface
     */
    private $countryInformationAcquirer;

    /**
     * @var Status
     */
    private $status;

    /**
     * @var Items
     */
    private $productGrid;

    /**
     * @var AddressRepository
     */
    private $addressRepository;

    /**
     * @var Registry
     */
    private $registry;

    /**
     * @var ScopeConfigInterface
     */
    private $scopeConfig;

    public function __construct(
        DataObjectFactory $dataObjectFactory,
        CountryInformationAcquirerInterface $countryInformationAcquirer,
        Status $status,
        Items $productGrid,
        AddressRepository $addressRepository,
        Registry $registry,
        ScopeConfigInterface $scopeConfig,
        array $variables = []
    ) {
        $this->variables = $variables;
        $this->dataObjectFactory = $dataObjectFactory;
        $this->countryInformationAcquirer = $countryInformationAcquirer;
        $this->status = $status;
        $this->productGrid = $productGrid;
        $this->addressRepository = $addressRepository;
        $this->registry = $registry;
        $this->scopeConfig = $scopeConfig;
    }

    public function getQuoteDataForPdf(): array
    {
        $data = [];
        $this->quote = $this->registry->registry(RegistryConstants::AMASTY_QUOTE);
        $this->getCustomerInfo($data);

        return $data;
    }

    private function getCustomerInfo(array &$data): void
    {
        foreach ($this->variables as $variableData) {
            $object = $this->getObjectWithData($variableData['objectType']);
            $data[$variableData['variable']] = (string) $object->{$variableData['method']}();
        }
    }

    /**
     * @param string $objectType
     * @return PdfInformation|Quote|\Magento\Customer\Api\Data\AddressInterface|\Magento\Framework\DataObject|Address
     */
    private function getObjectWithData(string $objectType)
    {
        switch ($objectType) {
            case 'quote':
                $result = $this->quote;
                break;
            case 'billing':
                $result = $this->getDefaultBilling();
                break;
            case 'shipping':
                $result = $this->getDefaultShipping();
                break;
            default:
                $result = $this;
        }

        return $result;
    }

    /**
     * @return \Magento\Customer\Api\Data\AddressInterface|\Magento\Framework\DataObject|Address
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    private function getDefaultBilling()
    {
        if (!$this->billing) {
            $this->billing = $this->quote->getBillingAddress();
            if (!$this->billing->getId()) {
                $this->billing = $this->quote->getCustomer()->getDefaultBilling();
            }
        }

        return $this->billing;
    }

    /**
     * @return \Magento\Quote\Model\Quote\Address|\Magento\Customer\Api\Data\AddressInterface
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    private function getDefaultShipping()
    {
        if (!$this->shipping) {
            $this->shipping = $this->quote->getShippingAddress();
            if (!$this->shipping->getId()) {
                $this->shipping = $this->quote->getCustomer()->getDefaultShipping();
            }
        }

        return $this->shipping;
    }

    /**
     * @param int $id
     * @return \Magento\Customer\Api\Data\AddressInterface|\Magento\Framework\DataObject
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    private function getAddressById(int $id)
    {
        try {
            $address = $this->addressRepository->getById($id);
        } catch (NoSuchEntityException $e) {
            $address = $this->dataObjectFactory->create();
        }

        return $address;
    }

    private function getBillingCountryName(): ?string
    {
        return $this->prepareCountryName($this->getDefaultBilling());
    }

    private function getShippingCountryName(): ?string
    {
        return $this->prepareCountryName($this->getDefaultShipping());
    }

    /**
     * @param \Magento\Customer\Api\Data\AddressInterface|\Magento\Framework\DataObject|Address $address
     * @return string
     */
    private function prepareCountryName($address): string
    {
        $countryName = '';
        $countryId = $address->getCountryId();

        if ($countryId) {
            $countryInformation = $this->countryInformationAcquirer->getCountryInfo($countryId);
            $countryName = $countryInformation->getFullNameLocale();
        }

        return $countryName;
    }

    private function getBillingStreet(): ?string
    {
        return $this->prepareStreet($this->getDefaultBilling());
    }

    private function getShippingStreet(): ?string
    {
        return $this->prepareStreet($this->getDefaultShipping());
    }

    private function getTelephone(): ?string
    {
        $telephone = $this->getDefaultShipping()->getTelephone();
        if ($telephone) {
            //phpcs:ignore
            return '<span>' . __('T') . '</span>' . $telephone;
        }
        return null;
    }

    private function hasShippingInfo(): bool
    {
        return $this->getShippingStreet()
            || $this->getShippingRegion()
            || $this->getShippingCountryName()
            || $this->getDefaultShipping()->getCity()
            || $this->getDefaultShipping()->getPostcode()
            || $this->getTelephone();
    }

    /**
     * @param \Magento\Customer\Api\Data\AddressInterface|\Magento\Framework\DataObject|Address $address
     * @return string|null
     */
    private function prepareStreet($address): ?string
    {
        $street = $address->getStreet();

        return is_array($street) ? implode(',', $street) : $street;
    }

    private function getProductGrid(): ?string
    {
        return $this->productGrid->setQuote($this->quote)->toHtml();
    }

    private function getShippingMethod(): ?string
    {
        return $this->quote->getShippingAddress()->getShippingDescription();
    }

    private function getBillingRegion(): string
    {
        return $this->getRegion($this->getDefaultBilling()->getRegion());
    }

    private function getShippingRegion(): string
    {
        return $this->getRegion($this->getDefaultShipping()->getRegion());
    }

    /**
     * @param \Magento\Customer\Api\Data\RegionInterface|string $region
     * @return string
     */
    private function getRegion($region): string
    {
        $result = '';
        if ($region) {
            $result = is_string($region) ? $region : $region->getRegion();
        }

        return (string)$result;
    }

    private function getStorePhone(): string
    {
        $storeId = (int)$this->quote->getStoreId();
        $configValue = $this->scopeConfig->getValue(
            Information::XML_PATH_STORE_INFO_PHONE,
            'store',
            $storeId
        );
        return $configValue ?: '';
    }
}
0

You should use type to inject a new argument item to Amasty\RequestQuote\Model\Pdf\PdfInformation instead of overriding the Model by preference.

Assume your vendor name is Paul, module name is AmastyRequestQuote. Let's create app/code/Paul/AmastyRequestQuote/etc/di.xml file:

<type name="Amasty\RequestQuote\Model\Pdf\PdfInformation">
    <arguments>
        <argument name="variables" xsi:type="array">
            <item name="21" xsi:type="array">
                <item name="objectType" xsi:type="string">custom</item>
                <item name="variable" xsi:type="const">\Amasty\RequestQuote\Model\Source\PdfVariables::QUOTE_REMARKS</item>
                <item name="method" xsi:type="string">getComments</item>
            </item>
        </argument>
    </arguments>
</type>

And don't forget to config your module depends on the Amasty_RequestQuote module:

app/code/Amasty/RequestQuote/etc/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="Paul_AmastyRequestQuote">
        <sequence>
            <module name="Amasty_RequestQuote"/>
        </sequence>
    </module>
</config>

Remember to remove generated folder, re-compile code, and clear the full_page cache type:

rm -rf generated
bin/magento setup:di:compile
bin/magento cache:clean full_page
12
  • When I do that, I cannot compil the code, it says "Constant name is expected." Commented Oct 27, 2022 at 16:12
  • I think \Amasty\RequestQuote\Model\Source\PdfVariables::QUOTE_REMARKS does not exist. Please check, if it does not exist, declare this constant in your override PdfVariables class, and replace it with the constant in your override model. If the issues persist, please paste the error log in your question, I'll take a look.
    – Tu Van
    Commented Oct 27, 2022 at 17:43
  • I made a much more descriptive post to explain what it does Commented Oct 28, 2022 at 15:00
  • You have to copy completely getComments method from Amasty Pdfinformation` to your override class. Then remove generated folder and re compile code
    – Tu Van
    Commented Oct 28, 2022 at 15:27
  • GetComments does NOT exist in the Amasty's PdfInformation. Thats why I created this function to my own PdfInformation override Commented Oct 28, 2022 at 16:13

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.