1

By using preference I am overriding third party (custom built extension) model class. For the process I have added di.xml file as shown below.

Path: Test/Test/etc/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="ShipperHQ\Shipper\Model\Carrier\Processor\ShipperMapper" type="Test\Test\Model\Carrier\Processor\ShipperMapper" />
</config>

Below represants code of the file which is extending the third part extension model class. In this file function "cartFactory" is inside class - \ShipperHQ\Shipper\Model\Carrier\Processor\ShipperMapper.

But it raise an error saying :

Notice: Undefined property: Test\Test\Model\Carrier\Processor\ShipperMapper::$cartFactory in app/code/Test/Test/Model/Carrier/Processor/ShipperMapper.php on line 41

Path: app/code/Test/Test/Model/Carrier/Processor/ShipperMapper.php

namespace Test\Test\Model\Carrier\Processor;

class ShipperMapper extends \ShipperHQ\Shipper\Model\Carrier\Processor\ShipperMapper
{
    /**
     * Format cart for from shipper for Magento
     *
     * @param $request
     * @return array
     */
    public function getCartDetails($request)
    {
        $cartDetails = $this->cartFactory->create([
            'declaredValue' => $request->getPackageValue(),
            'freeShipping'  => (bool)$request->getFreeShipping(),
            'items'         => $this->getFormattedItems($request, $request->getAllItems())
        ]);

        //My custom code is here

        return $cartDetails;
    }
}

Parent model file Code Path: app/code/ShipperHQ/Shipper/Model/Carrier/Processor/ShipperMapper.php

<?php
namespace ShipperHQ\Shipper\Model\Carrier\Processor;

/**
 * Class Shipperhq_Shipper_Model_Carrier_Convert_ShipperMapper
 *
 * This class converts the Magento Request into a format that
 * is usable by the ShipperHQ webservice
 */
use ShipperHQ\WS;
use ShipperHQ\WS\Rate\Request;


class ShipperMapper
{

    protected static $ecommerceType = 'magento';
    protected static $stdAttributeNames = [
        'shipperhq_shipping_group', 'shipperhq_post_shipping_group',
        /* 'shipperhq_warehouse',*/ 'shipperhq_royal_mail_group', 'shipperhq_shipping_qty',
        'shipperhq_shipping_fee', 'shipperhq_additional_price', 'freight_class',
        'shipperhq_nmfc_class', 'shipperhq_nmfc_sub', 'shipperhq_handling_fee', 'shipperhq_carrier_code',
        'shipperhq_volume_weight', 'shipperhq_declared_value', 'ship_separately',
        'shipperhq_dim_group', 'shipperhq_poss_boxes', 'ship_box_tolerance', 'must_ship_freight', 'packing_section_name'
    ];

    protected static $dim_height = 'ship_height';
    protected static $dim_width = 'ship_width';
    protected static $dim_length = 'ship_length';
    protected static $alt_height = 'height';
    protected static $alt_width = 'width';
    protected static $alt_length = 'length';
    protected static $origin = 'shipperhq_warehouse';
    protected static $location = 'shipperhq_location';
    protected static $available_date = 'shipperhq_availability_date';

    protected static $useDefault = 'Use Default';

    protected static $dim_group = 'shipperhq_dim_group';
    protected static $conditional_dims = ['shipperhq_poss_boxes',
        'shipperhq_volume_weight', 'ship_box_tolerance', 'ship_separately', 'ship_height', 'ship_width', 'ship_length',
        'height', 'width', 'length'
    ];

    protected static $legacyAttributeNames = [
        'package_id', 'special_shipping_group', 'volume_weight', 'warehouse', 'handling_id',
        'package_type' // royal mail
    ];

    protected static $shippingOptions = ['liftgate_required', 'notify_required', 'inside_delivery', 'destination_type'];

    protected static $prodAttributes;

    /**
     * @var \ShipperHQ\Shipper\Helper\Data
     */
    protected $shipperDataHelper;
    /**
     * @var \Magento\Customer\Model\GroupFactory
     */
    private $groupFactory;
    /**
     * @var \Magento\Framework\App\ProductMetadata
     */
    private $productMetadata;
    /**
     * @var \Magento\Tax\Model\Calculation
     */
    private $taxCalculation;
    /**
     * @var \Magento\Catalog\Helper\Product\Configuration
     */
    private $productConfiguration;
    /**
     * @var Request\RateRequestFactory
     */
    private $rateRequestFactory;
    /**
     * @var \ShipperHQ\Shipper\Helper\LogAssist
     */
    private $shipperLogger;
    /**
     * @var Request\Shipping\SelectedOptionsFactory
     */
    private $selectedOptionsFactory;
    /*
     * @var \ShipperHQ\WS\Rate\Request\Checkout\CartFactory
     */
    private  $cartFactory;
    /*
     * @var \ShipperHQ\WS\Rate\Request\Checkout\Item
     */
    private $itemFactory;
    /**
     * @var WS\Shared\AddressFactory
     */
    private $addressFactory;
    /**
     * @var Request\InfoRequestFactory
     */
    private $infoRequestFactory;
    /**
     * @var WS\Shared\CredentialsFactory
     */
    private $credentialsFactory;
    /**
     * @var WS\Shared\SiteDetailsFactory
     */
    private $siteDetailsFactory;
    /**
     * @var Request\CustomerDetailsFactory
     */
    private $customerDetailsFactory;
    /**
     * @var Request\ShipDetailsFactory
     */
    private $shipDetailsFactory;
    /**
     * @var StockHandler
     */
    protected $stockHandler;

    /**
     * @var \Magento\Checkout\Model\Session
     */
    protected $checkoutSession;

    /**
     * @var  \ShipperHQ\WS\Rate\Request\Checkout\PhysicalBuildingDetailFactory
     */
    protected $physicalBuildingDetailFactory;
    /**
     * @var  \ShipperHQ\WS\Rate\Request\Checkout\StockDetailFactory
     */
    protected $stockDetailFactory;

    function __construct(\ShipperHQ\Shipper\Helper\Data $shipperDataHelper,
                         \Magento\Customer\Model\GroupFactory $groupFactory,
                         \ShipperHQ\WS\Rate\Request\RateRequestFactory $rateRequestFactory,
                         \ShipperHQ\WS\Rate\Request\InfoRequestFactory $infoRequestFactory,
                         \ShipperHQ\WS\Shared\AddressFactory $addressFactory,
                         \ShipperHQ\WS\Shared\CredentialsFactory $credentialsFactory,
                         \ShipperHQ\WS\Shared\SiteDetailsFactory $siteDetailsFactory,
                         \ShipperHQ\WS\Rate\Request\CustomerDetailsFactory $customerDetailsFactory,
                         \ShipperHQ\WS\Rate\Request\ShipDetailsFactory $shipDetailsFactory,
                         \ShipperHQ\WS\Rate\Request\Shipping\SelectedOptionsFactory $selectedOptionsFactory,
                         \ShipperHQ\WS\Rate\Request\Checkout\CartFactory $cartFactory,
                         \ShipperHQ\WS\Rate\Request\Checkout\ItemFactory $itemFactory,
                         \Magento\Tax\Model\Calculation $taxCalculation,
                         \ShipperHQ\Shipper\Helper\LogAssist $shipperLogger,
                         \Magento\Catalog\Helper\Product\Configuration $productConfiguration,
                         \Magento\Framework\App\ProductMetadata $productMetadata,
                         \Magento\Backend\Block\Template\Context $context,
                         StockHandler $stockHandler,
                         \Magento\Checkout\Model\Session $checkoutSession,
                         \ShipperHQ\WS\Rate\Request\Checkout\PhysicalBuildingDetailFactory $physicalBuildingDetailFactory,
                         \ShipperHQ\WS\Rate\Request\Checkout\StockDetailFactory $stockDetailFactory
    )
    {

        $this->shipperDataHelper = $shipperDataHelper;
        $this->storeManager = $context->getStoreManager();
        self::$prodAttributes = $this->shipperDataHelper->getProductAttributes();
        $this->groupFactory = $groupFactory;
        $this->productMetadata = $productMetadata;
        $this->taxCalculation = $taxCalculation;
        $this->productConfiguration = $productConfiguration;
        $this->rateRequestFactory = $rateRequestFactory;
        $this->shipperLogger = $shipperLogger;
        $this->selectedOptionsFactory = $selectedOptionsFactory;
        $this->cartFactory = $cartFactory;
        $this->itemFactory = $itemFactory;
        $this->addressFactory = $addressFactory;
        $this->infoRequestFactory = $infoRequestFactory;
        $this->credentialsFactory = $credentialsFactory;
        $this->siteDetailsFactory = $siteDetailsFactory;
        $this->customerDetailsFactory = $customerDetailsFactory;
        $this->shipDetailsFactory = $shipDetailsFactory;
        $this->stockHandler = $stockHandler;
        $this->physicalBuildingDetailFactory = $physicalBuildingDetailFactory;
        $this->checkoutSession = $checkoutSession;
        $this->stockDetailFactory = $stockDetailFactory;
    }

    /*Other function continue*/
}

Can anyone please help me on this.

2

You are using private variable of your extended class. If you can this using construct function.

Firstly add below variable.

/*
* @var \ShipperHQ\WS\Rate\Request\Checkout\CartFactory
*/
private  $cartFactory;

Then add __construct fucntion

function __construct(\ShipperHQ\WS\Rate\Request\Checkout\CartFactory $cartFactory)
{
    $this->cartFactory = $cartFactory;
}
0
0

Since you are extending the original class, you can only access its public or protected variables. $cartFactory in the original class is private so you cant access it directly with

$this->cartFactory

The question here really is, if a preference is a good choice. If there is any other way around it shoudn't be used. It seems like a after plugin in your case would be a way better choice http://devdocs.magento.com/guides/v2.0/extension-dev-guide/plugins.html

0
0

Try to use the below override model

class ShipperMapper extends \ShipperHQ\Shipper\Model\Carrier\Processor\ShipperMapper
{

    private $cartFactory;

    public function __construct(\ShipperHQ\Shipper\Helper\Data $shipperDataHelper,
                             \Magento\Customer\Model\GroupFactory $groupFactory,
                             \ShipperHQ\WS\Rate\Request\RateRequestFactory $rateRequestFactory,
                             \ShipperHQ\WS\Rate\Request\InfoRequestFactory $infoRequestFactory,
                             \ShipperHQ\WS\Shared\AddressFactory $addressFactory,
                             \ShipperHQ\WS\Shared\CredentialsFactory $credentialsFactory,
                             \ShipperHQ\WS\Shared\SiteDetailsFactory $siteDetailsFactory,
                             \ShipperHQ\WS\Rate\Request\CustomerDetailsFactory $customerDetailsFactory,
                             \ShipperHQ\WS\Rate\Request\ShipDetailsFactory $shipDetailsFactory,
                             \ShipperHQ\WS\Rate\Request\Shipping\SelectedOptionsFactory $selectedOptionsFactory,
                             \ShipperHQ\WS\Rate\Request\Checkout\CartFactory $cartFactory,
                             \ShipperHQ\WS\Rate\Request\Checkout\ItemFactory $itemFactory,
                             \Magento\Tax\Model\Calculation $taxCalculation,
                             \ShipperHQ\Shipper\Helper\LogAssist $shipperLogger,
                             \Magento\Catalog\Helper\Product\Configuration $productConfiguration,
                             \Magento\Framework\App\ProductMetadata $productMetadata,
                             \Magento\Backend\Block\Template\Context $context,
                             StockHandler $stockHandler,
                             \Magento\Checkout\Model\Session $checkoutSession,
                             \ShipperHQ\WS\Rate\Request\Checkout\PhysicalBuildingDetailFactory $physicalBuildingDetailFactory,
                             \ShipperHQ\WS\Rate\Request\Checkout\StockDetailFactory $stockDetailFactory
    )
    {
        parent::__construct(
            $shipperDataHelper,$groupFactory,$rateRequestFactory,$infoRequestFactory,$addressFactory,$credentialsFactory,
            $siteDetailsFactory,$customerDetailsFactory,$shipDetailsFactory,$selectedOptionsFactory,$cartFactory,$itemFactory,
            $taxCalculation,$shipperLogger,$productConfiguration,$productMetadata,$context,$stockHandler,$checkoutSession,
            $physicalBuildingDetailFactory,$stockDetailFactory
        );
         $this->cartFactory = $cartFactory;
    }

    // Custom code here

}  
2
  • Thanks for Answer. I have try but it's throw error. Jul 12 '17 at 8:39
  • what error it is throwing? I have missed comma in the constructor, updated my answer
    – lavanya
    Jul 12 '17 at 8:56
0

@Vikas Verma answer is right but magento throw error so i have pass parent construct on my construct function. Here is full code. Maybe it's helpful to other people.

<?php
namespace Test\Test\Model\Carrier\Processor;

class ShipperMapper extends \ShipperHQ\Shipper\Model\Carrier\Processor\ShipperMapper
{
    /*
    * @var \ShipperHQ\WS\Rate\Request\Checkout\CartFactory
    */
    private $cartFactory;

    public function __construct(\ShipperHQ\Shipper\Helper\Data $shipperDataHelper,
                                \Magento\Customer\Model\GroupFactory $groupFactory,
                                \ShipperHQ\WS\Rate\Request\RateRequestFactory $rateRequestFactory,
                                \ShipperHQ\WS\Rate\Request\InfoRequestFactory $infoRequestFactory,
                                \ShipperHQ\WS\Shared\AddressFactory $addressFactory,
                                \ShipperHQ\WS\Shared\CredentialsFactory $credentialsFactory,
                                \ShipperHQ\WS\Shared\SiteDetailsFactory $siteDetailsFactory,
                                \ShipperHQ\WS\Rate\Request\CustomerDetailsFactory $customerDetailsFactory,
                                \ShipperHQ\WS\Rate\Request\ShipDetailsFactory $shipDetailsFactory,
                                \ShipperHQ\WS\Rate\Request\Shipping\SelectedOptionsFactory $selectedOptionsFactory,
                                \ShipperHQ\WS\Rate\Request\Checkout\CartFactory $cartFactory,
                                \ShipperHQ\WS\Rate\Request\Checkout\ItemFactory $itemFactory,
                                \Magento\Tax\Model\Calculation $taxCalculation,
                                \ShipperHQ\Shipper\Helper\LogAssist $shipperLogger,
                                \Magento\Catalog\Helper\Product\Configuration $productConfiguration,
                                \Magento\Framework\App\ProductMetadata $productMetadata,
                                \Magento\Backend\Block\Template\Context $context,
                                \ShipperHQ\Shipper\Model\Carrier\Processor\StockHandler $stockHandler,
                                \Magento\Checkout\Model\Session $checkoutSession,
                                \ShipperHQ\WS\Rate\Request\Checkout\PhysicalBuildingDetailFactory $physicalBuildingDetailFactory,
                                \ShipperHQ\WS\Rate\Request\Checkout\StockDetailFactory $stockDetailFactory
    )
    {
        $this->cartFactory = $cartFactory;

        parent::__construct($shipperDataHelper,
            $groupFactory,
            $rateRequestFactory,
            $infoRequestFactory,
            $addressFactory,
            $credentialsFactory,
            $siteDetailsFactory,
            $customerDetailsFactory,
            $shipDetailsFactory,
            $selectedOptionsFactory,
            $cartFactory,
            $itemFactory,
            $taxCalculation,
            $shipperLogger,
            $productConfiguration,
            $productMetadata,
            $context,
            $stockHandler,
            $checkoutSession,
            $physicalBuildingDetailFactory,
            $stockDetailFactory);
    }

    /**
     * Format cart for from shipper for Magento
     *
     * @param $request
     * @return array
     */
    public function getCartDetails($request)
    {
        $cartDetails = $this->cartFactory->create([
                'declaredValue' => $request->getPackageValue(),
                'freeShipping'  => (bool)$request->getFreeShipping(),
                'items'         => $this->getFormattedItems($request, $request->getAllItems())
        ]);

        return $cartDetails;
    }
} 

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.