سؤال

I've created a model override for Magento\Catalog\Model\Product. My override functions for getName() and getPrice() are working. But getTierPrices() and getTierPrice() in the same class are not being invoked. When I open a category page or a product details page, the name and price are showing the hard-coded values in my overrides, but the tier prices show the standard Magento values. I've even throw an exception on the first line of getTierPrices() to check if it's invoked but the exception isn't thrown.

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="MyCompany\Pricing\Catalog\Model\Product" />
</config>

Product.php:

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

class Product extends \Magento\Catalog\Model\Product
{
    public function getName()
    {
        // Do something here
        //return parent::getName();
        return 'Name override';
    }

    public function getPrice()
    {
        // Do something here
        return 100;
    }

    public function getTierPrices()
    {
        throw new Exception("This isn't invoked.");
        $tierPrices = [
            [ 'customer_group_id' => '0',
                'price_qty' => 2,
                'price' => 12,
                'website' => 'all'
            ]
        ];
        return $tierPrices;
    }

    public function getTierPrice($qty = null)
    {
        return 10;
    }
}
هل كانت مفيدة؟

المحلول

The solution turned out to be using a different model. It doesn't seem logical to me if the product has a 'getTierPrices' function, but the frontend actually calls the 'getTierPriceList' on the \Magento\Catalog\Pricing\Price\TierPrice class. You do, however, need to use 'getTierPrice' on the Product class to control what happens when you add a product to cart and/or update a cart quantity.

نصائح أخرى

I think the best practice would be use Magento 2 plugin to apply your changes.

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">               
    <type name="Magento\Catalog\Api\Data\ProductInterface">
        <plugin name="company-product" type="MyCompany\Pricing\Catalog\ProductPlugin" sortOrder="1" />
    </type>
</config>

app/code/MyCompany/Pricing/Catalog/ProductPlugin.php

<?php
namespace MyCompany\Pricing\Catalog;

use Magento\Catalog\Api\Data\ProductInterface;

class ProductPlugin {

    public function afterGetName(
        ProductInterface $subject,
        $result
    ) {
        return 'Name override' . $result;
    }

    public function afterGetPrice(
        ProductInterface $subject,
        $result
    ) {
        return $result;
    }

    public function afterGetTierPrices(
        ProductInterface $subject,
        $result
    ) {
        return $result;
    }

    public function afterGetTierPrice(
        ProductInterface $subject,
        $result
    ) {
        return $result;
    }
}

You can find more information here http://devdocs.magento.com/guides/v2.1/extension-dev-guide/plugins.html

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top