0

I'm implementing product price calculation based on a formula that uses customizible options in Magento 2 (ver. 2.3.1). I added some code to _applyOptionsPrice() to "Magento/Catalog/Model/Product/Type/Price.php and it works like a charm. Now I want to take my code out of magento code, and implement it as an custom module, but as much as I try I can not get it to work as it's suppose to do.

Here is my code:

in app/code/ABPPRK/ModulePrice/registration.php:

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'ABPPRK_ModulePrice',
__DIR__
);

in app/code/ABPPRK/ModulePrice/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\Type\Price" type="ABPPRK\ModulePrice\Model\Price" />
</config>

module.xml:


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="ABPPRK_ModulePrice" setup_version="1.0.1"></module>
</config>


And finally app/code/ABPPRK/ModulePrice/Model/Price.php:


<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace ABPPRK\ModulePrice\Model;

class Price extends \Magento\Catalog\Model\Product\Type\Price 
{

    public function getFinalPrice($qty, $product)
    {

        return 0;
    }

}


Of course the return of the method to override (getFinalPrice) should not be that one. This is just a simple implementation to test with xdebug that does not work, as neither the real code.

There are no errors during "bin/magento setup:di:compile". And I can tell that command is considering my module because at some past point it gave the error:

"Warning: Declaration of ABPPRK\ModulePrice\Model\Price::getFinalPrice() should be compatible with Magento\Catalog\Model\Product\Type\Price::getFinalPrice($qty, $product)"

What is missing here? I've followed all the tutorials I've found about overriding class methods but Iven't found anyone specific for this method. :-(

Thanks in advance, Armando.

0

I think you need to add sequence in module.xml , may be you need to load catalog and sales modules before your's

3
0

You need to use

public function getFinalPrice(Magento\Catalog\Model\Product\Type\Price $price, $qty, $product)

as your function signature.

The first argument when injecting functions in magento is always the subject class, so you can call the subject classes functions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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