0

I'm extending \Magento\Catalog\Pricing\Price\TierPrice to return a custom list of tier prices for products. But I can't seem to figure out how to get the product id that is calling the getTierPriceList method. Any suggestions?

My di.xml:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Catalog\Pricing\Price\TierPrice" type="MyCompany\Pricing\Model\Plugin\TierPrice" />
</config>

TierPrice class:

<?php
namespace MyCompany\Pricing\Model\Plugin;

class TierPrice extends \Magento\Catalog\Pricing\Price\TierPrice
{
    public function getTierPriceList()
    {
        //custom code goes here, return original for now.
        return parent::getTierPriceList();
    }
}

1 Answer 1

1

Because you are extending \Magento\Catalog\Pricing\Price\TierPrice you can access the protected property $product with:

$productId = $this->getProduct()->getId();

The $product property is set in the constructor of \Magento\Framework\Pricing\Price\AbstractPrice which \Magento\Catalog\Pricing\Price\TierPrice extends.

1
  • Awesome, thanks. I didn't realize you could use $this in this context. I assumed it would be the extended class.
    – Erik
    Jun 12, 2017 at 12:25

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.