7

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?

1

1 Answer 1

18

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

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