0

I have a third-party module with a file I want to override so I don't loss my changes when upgrading the third-party extension.

Vendor\Module\Plugin\Quote\UpdateItem.php

namespace Vendor\Module\Plugin\Quote;

use Vendor\Module\Model\Product\Type\GiftCard;

class UpdateItem {

    /**
     * @var \Magento\Catalog\Model\ProductRepository
     */
    private $productRepository;

    public function __construct(\Magento\Catalog\Model\ProductRepository $productRepository)
    {
        $this->productRepository = $productRepository;
    }

    /**
     * save
     *
     * @param \Magento\Quote\Model\Quote $subject
     * @param callable $proceed
     * @param int $itemId
     * @param \Magento\Framework\DataObject $buyRequest
     * @param null|array|\Magento\Framework\DataObject $params
     * @return \Magento\Quote\Model\Quote\Item
     */
    public function aroundUpdateItem($subject, callable $proceed, $item, $buyRequest, $params = null)
    {
        // some code

        return $proceed($itemId, $buyRequest, $params = null);
    }
}

Vendor\Module\etc\di.xml

<type name="Magento\Quote\Model\Quote">
    <plugin name="Vendor_Module::UpdateItem" type="Vendor\Module\Plugin\Quote\UpdateItem"/>
</type>

I tried to add this file to my theme but it doesn't override the third-party file:

\app\design\frontend\Mytheme\custom\Vendor_Module\Plugin\Quote\UpdateItem.php

UPDATE

Vendor\Module and Vendor_Module is the third-party module name. So above code is the code I want to override.

UPDATE 2

The accepted answer got me on the right track. I did the following:

Myvendor\Mymodule and Myvendor_Mymodule is a custom module I created.

Myvendor\Mymodule\etc\di.xml

<type name="Magento\Quote\Model\Quote">
    <plugin name="Vendor_Module::UpdateItem" type="Vendor\Module\Plugin\Quote\UpdateItem" disabled="true" />
</type>
<type name="Magento\Quote\Model\Quote">
    <plugin name="Myvendor_Mymodule::UpdateItem" type="Myvendor\Mymodule\Plugin\Quote\UpdateItem"/>
</type>

Myvendor\Mymodule\Plugin\Quote\UpdateItem.php

namespace Myvendor\Mymodule\Plugin\Quote;

use Vendor\Module\Model\Product\Type\GiftCard;

class UpdateItem {

    /**
     * @var \Magento\Catalog\Model\ProductRepository
     */
    private $productRepository;

    public function __construct(\Magento\Catalog\Model\ProductRepository $productRepository)
    {
        $this->productRepository = $productRepository;
    }

    /**
     * save
     *
     * @param \Magento\Quote\Model\Quote $subject
     * @param callable $proceed
     * @param int $itemId
     * @param \Magento\Framework\DataObject $buyRequest
     * @param null|array|\Magento\Framework\DataObject $params
     * @return \Magento\Quote\Model\Quote\Item
     */
    public function aroundUpdateItem($subject, callable $proceed, $item, $buyRequest, $params = null)
    {
        // some code

        return $proceed($itemId, $buyRequest, $params = null);
    }
}

1 Answer 1

0

Your custom Plugin class file needs to be located at -

$MAGENTO_HOME\app\code\Vendor\Module\Plugin\Quote\UpdateItem.php

Note: I'm assuming Vendor\Module is your custom-module's namespace and not the 3rd party vendors namespace.

Additionally, you could try using the sortOrder attribute to ensure your custom Plugin executes before/after (as desired, per your specific requirements) the Vendor's code.

Good Luck! :)

2
  • Vendor\Module is the third-party module name
    – Cudos
    Dec 6, 2017 at 7:50
  • @Cudos - I would highly recommend against making code changes to the Vendor's 3rd party module files as any module updates made by the 3rd party vendor could, essentially, wipe out your custom changes. Dec 6, 2017 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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