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.