2

I want to override a model class of custom module.

Want to override:-I95DevConnect\Base\Model\Product\product

By:-I95Dev\Base\Model\Rewrite\Product\product

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="I95DevConnect\Base\Model\Product\Product" 
             type="I95Dev\Base\Model\Rewrite\Product\Product" />
</config>

I95DevConnect\Base\Model\Product\product.php

<?php
    namespace I95DevConnect\Base\Model\Product;

    use I95DevConnect\Base\Helper\Data;
    use Magento\Tax\Model\ClassModelFactory as TaxClassModelFactory;

    class Product
    {

        /**
         * Custom logs
         */
        const MAGLOGNAME = 'MagentoToERP';
        const ERPLOGNAME = 'ERPToMagento';
        const I95EXC = 'i95devApiException';

        protected $_logger;

        /**
         * Tax class model factory
         *
         * @var TaxClassModelFactory
         */
        private $_taxClassModelFactory;

        /**
         * @var \I95DevConnect\MessageQueue\Api\Data\ProductInterface
         */
        protected $_productInterface;

        /**
         * @var StockRegistryProviderInterface
         */
        protected $_stockRegistryProvider;
        /**
         *
         * @param \Psr\Log\LoggerInterface                              $logger
         * @param TaxClassModelFactory                                  $taxClassModelFactory
         * @param \I95DevConnect\MessageQueue\Api\Data\ProductInterface $productInterface
         * @param \Magento\CatalogInventory\Model\StockRegistryProvider $stockRegistryProvider
         */
        public function __construct(
            \Psr\Log\LoggerInterface $logger,
            \Magento\Tax\Model\ClassModelFactory $taxClassModelFactory,
            \I95DevConnect\MessageQueue\Api\Data\ProductInterface $productInterface,
            \Magento\CatalogInventory\Model\StockRegistryProvider $stockRegistryProvider
        ) {
            $this->_logger = $logger;
            $this->_taxClassModelFactory = $taxClassModelFactory;
            $this->_productInterface = $productInterface;
            $this->_stockRegistryProvider = $stockRegistryProvider;
        }

        /**
         * Sets Product information
         *
         * @param  Mage_Catalog_Model_Product $product
         * @return array
         */
        public function setProduct($product)
        {
            try {
                $weight = 0;
                $status = $product->getStatus();
                $productStatus = $product->getVisibility();
                $websiteId = 1;
                $productId = $product->getId();
                $stockData = $this->_stockRegistryProvider->getStockItem($productId, $websiteId);

                if ($productStatus == 1) {
                    $productVisibility = 0;
                } else {
                    $productVisibility = 1;
                }
                $productId = $product->getId();
                $taxClassModel = $this->_taxClassModelFactory->create()->load($product->getTaxClassId());
                $taxClass = $taxClassModel->getClassName();
                $taxClassName = (isset($taxClass) ? $taxClass : 'None' );
                $specialPrice = $product->getSpecialPrice();
                $productSpecialPrice = (isset($specialPrice) ? $specialPrice : '0' );
                $this->_productInterface->setSku($product->getsku());
                $this->_productInterface->setName($product->getName());
                $this->_productInterface->setDescription(strip_tags($product->getDescription()));
                $this->_productInterface->setShortDescription(strip_tags($product->getShortDescription()));
                $this->_productInterface->setQty($stockData->getQty());
                $cost = $product->getCost();
                $this->_productInterface->setCost((isset($cost) ? $cost : 0));
                $this->_productInterface->setUpdatedAt($product->getUpdatedAt());
                $this->_productInterface->setTypeId($product->getTypeId());
                $this->_productInterface->setSpecialPrice($productSpecialPrice);
                $this->_productInterface->setPrice($product->getPrice());
                $this->_productInterface->setSpecialFromDate($product->getSpecialFromDate());
                $this->_productInterface->setSpecialToDate($product->getSpecialToDate());
                $this->_productInterface->setUnitOfMeasure('each');
                $this->_productInterface->setQuantityOrdered(0);
                if ($product->getWeight()) {
                    $weight = $product->getWeight();
                }
                $this->_productInterface->setWeight($weight);
                $this->_productInterface->setTaxClassId($taxClassName);
                $this->_productInterface->setCreatedAt($product->getcreatedAt());
                $this->_productInterface->setStatus($status);
                $this->_productInterface->setDiscountReasonCodeId(null);
                $this->_productInterface->setDefaultDiscountReasonCode(null);
                $this->_productInterface->setDiscountAmount(0);
                $this->_productInterface->setIsTierPriceApplied(true);
                $this->_productInterface->setVoucherNumber(null);
                $this->_productInterface->setGiftCardAmount(0);
                $this->_productInterface->setStoreId($stockData->getStoreId());
                $this->_productInterface->setBackorders($stockData->getBackorders());
                $this->_productInterface->setSourceItemId($productId);
            } catch (\Exception $ex) {
                $this->_logger->critical($ex->getMessage());
            }
            return $this->_productInterface;
        }
    }

I95Dev\Base\Model\Rewrite\Product\product.php

<?php
namespace I95Dev\Base\Model\Rewrite\Product;

use I95DevConnect\Base\Helper\Data;
use Magento\Tax\Model\ClassModelFactory as TaxClassModelFactory;

    class Product extends \I95DevConnect\Base\Model\Product\Product
{

    /**
     * Custom logs
     */
    const MAGLOGNAME = 'MagentoToERP';
    const ERPLOGNAME = 'ERPToMagento';
    const I95EXC = 'i95devApiException';

    protected $_logger;

    /**
     * Tax class model factory
     *
     * @var TaxClassModelFactory
     */
    private $_taxClassModelFactory;

    /**
     * @var \I95DevConnect\MessageQueue\Api\Data\ProductInterface
     */
    protected $_productInterface;

    /**
     * @var StockRegistryProviderInterface
     */
    protected $_stockRegistryProvider;
    /**
     *
     * @param \Psr\Log\LoggerInterface                              $logger
     * @param TaxClassModelFactory                                  $taxClassModelFactory
     * @param \I95DevConnect\MessageQueue\Api\Data\ProductInterface $productInterface
     * @param \Magento\CatalogInventory\Model\StockRegistryProvider $stockRegistryProvider
     */
    public function __construct(
        \Psr\Log\LoggerInterface $logger,
        \Magento\Tax\Model\ClassModelFactory $taxClassModelFactory,
        \I95DevConnect\MessageQueue\Api\Data\ProductInterface $productInterface,
        \Magento\CatalogInventory\Model\StockRegistryProvider $stockRegistryProvider
    ) {
        $this->_logger = $logger;
        $this->_taxClassModelFactory = $taxClassModelFactory;
        $this->_productInterface = $productInterface;
        $this->_stockRegistryProvider = $stockRegistryProvider;
    }

    /**
     * Sets Product information
     *
     * @param  Mage_Catalog_Model_Product $product
     * @return array
     */
    public function setProduct($product)
    {
        try {
            $weight = 0;
            $status = $product->getStatus();
            $productStatus = $product->getVisibility();
            $websiteId = 1;
            $AdditionalAtrr=0;
            $productId = $product->getId();
            $stockData = $this->_stockRegistryProvider->getStockItem($productId, $websiteId);

            if ($productStatus == 1) {
                $productVisibility = 0;
            } else {
                $productVisibility = 1;
            }
            $productId = $product->getId();
            $taxClassModel = $this->_taxClassModelFactory->create()->load($product->getTaxClassId());
            $taxClass = $taxClassModel->getClassName();
            $taxClassName = (isset($taxClass) ? $taxClass : 'None' );
            $specialPrice = $product->getSpecialPrice();
            $productSpecialPrice = (isset($specialPrice) ? $specialPrice : '0' );
            //$this->_productInterface->setSku($product->getsku());
            $this->_productInterface->setName($product->getName());
            $this->_productInterface->setDescription(strip_tags($product->getDescription()));
            $this->_productInterface->setShortDescription(strip_tags($product->getShortDescription()));
            $this->_productInterface->setQty($stockData->getQty());
            $cost = $product->getCost();
            $this->_productInterface->setCost((isset($cost) ? $cost : 0));
            $this->_productInterface->setUpdatedAt($product->getUpdatedAt());
            $this->_productInterface->setTypeId($product->getTypeId());
            $this->_productInterface->setSpecialPrice($productSpecialPrice);
            $this->_productInterface->setPrice($product->getPrice());
            $this->_productInterface->setSpecialFromDate($product->getSpecialFromDate());
            $this->_productInterface->setSpecialToDate($product->getSpecialToDate());
            $this->_productInterface->setUnitOfMeasure('each');
            $this->_productInterface->setQuantityOrdered(0);
            if ($product->getWeight()) {
                $weight = $product->getWeight();
            }

            $this->_productInterface->setWeight($weight);
//             if ($product->getTestDemo()) {
//                $AdditionalAtrr = $product->getTestDemo();
//            }
//            $this->_productInterface->setAdditionalAtrr($AdditionalAtrr);
            $this->_productInterface->setTaxClassId($taxClassName);
            $this->_productInterface->setCreatedAt($product->getcreatedAt());
            $this->_productInterface->setStatus($status);
            $this->_productInterface->setDiscountReasonCodeId(null);
            $this->_productInterface->setDefaultDiscountReasonCode(null);
            $this->_productInterface->setDiscountAmount(0);
            $this->_productInterface->setIsTierPriceApplied(true);
            $this->_productInterface->setVoucherNumber(null);
            $this->_productInterface->setGiftCardAmount(0);
            $this->_productInterface->setStoreId($stockData->getStoreId());
            $this->_productInterface->setBackorders($stockData->getBackorders());
            $this->_productInterface->setSourceItemId($productId);
        } catch (\Exception $ex) {
              $this->_logger->critical($ex->getMessage());
        }
        return $this->_productInterface;
    }
}

Can any one help me on this problem ? Any references or suggestions are highly appreciated. Thanks in advance.

1 Answer 1

2

We can override classes using the Preferences in Magento2. Use the following code in your di.xml

<preference for="I95DevConnect\Base\Model\Product\Product" 
         type="I95Dev\Base\Model\Rewrite\Product\Product" />

Please mention the dependency in your module.xml so that it will load after the base module have been loaded. Please mention in module.xml as following:

<module name="I95Dev_Base" setup_version="1.13.2">
    <sequence>
        <module name="I95DevConnect_Base" />
    </sequence>
</module>

Hope this will help you.

3
  • thanks for answer but i have made changes it is not working. Apr 21, 2017 at 8:47
  • Please clear the var/generation folder and check if the same class is overridden by any other class. Apr 21, 2017 at 8:49
  • I have updated the question. Please check once. Apr 21, 2017 at 8:56

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.