5

I would like to override (call the below model file from my custom module called Demo_Customprice

/vendor/magento/module-catalog/Model/Product/Option/Value.php

How could we achieve this?

13

in you etc/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\Catalog\Model\Product\Option\Value" type="Demo\Customprice\Model\Rewrite\Catalog\Product\Option\Value" />

</config>

in your Model file

namespace Demo\Customprice\Model\Rewrite\Catalog\Product\Option;

    class Value extends \Magento\Catalog\Model\Product\Option\Value
    {
        public function __construct()
        {
            echo "Model Rewrite Working"; die();

        }

    }

Reference

If use any constant value from interface.

namespace Demo\Customprice\Model\Rewrite\Catalog\Product\Option;

     use Demo\Customprice\Api\Data\AccountInterface;

        class Value extends \Magento\Catalog\Model\Product\Option\Value
        {
            public function __construct()
            {
                echo "Model Rewrite Working"; die();

            }

        }

    /**
     * {@inheritdoc}
     */
    public function getCustomerId() {
        return $this->_getData(AccountInterface::CUSTOMER_ID);
    }

    /**
     * {@inheritdoc}
     */
    public function setCustomerId($customerId) {
        $this->setData(AccountInterface::CUSTOMER_ID, $customerId);

        return $this;
    }
| improve this answer | |
  • Hi @Satti thanks for the reply but based on that i am getting errors prntscr.com/e90sdy whether i have to do anything more? – Nagaraju K Feb 15 '17 at 12:20
  • @NAGARAJUK after seen this error there we problem in you xml kindly check all nodes are closed properly? – Qaisar Satti Feb 15 '17 at 12:23
  • Sure. Let me check and confirm you thanks – Nagaraju K Feb 15 '17 at 12:25
  • thanks now its loading fine but how can i override the public function getPrice($flag = false) function. – Nagaraju K Feb 15 '17 at 12:56
  • @NAGARAJUK simply add public function getPrice($flag = false) { echo "Model Rewrite Working"; die(); } – Qaisar Satti Feb 15 '17 at 12:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.