Вопрос

I am trying to override a resource model class Magento\Catalog\Model\ResourceModel\Product\Attribute\Backend\TierPrice to select new columns that I have created in the catalog_product_entity_tier_price table. However, the new columns are not selected and the model is not getting overridden.

namespace [vendor][module]\Model\ResourceModel\Product\Attribute\Backend;

class TierPrice extends \Magento\Catalog\Model\ResourceModel\Product\Attribute\Backend\TierPrice
{

    /**
     * Add qty column
     *
     * @param array $columns
     * @return array
     */
    protected function _loadPriceDataColumns($columns)
    {
        $columns = parent::_loadPriceDataColumns($columns);
        //$columns['price_qty'] = 'qty';
        //new columns
        $columns['created_by']='created_by';
        $columns['price_effective_from']='price_effective_from';
        $columns['price_effective_to']='price_effective_to';
        $columns['deletion_flag']='deletion_flag';
        return $columns;

    }
}

di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLcation="urn:magento:framework:ObjectManager/etc/config.xsd">
   <preference for="Magento\Catalog\Model\ResourceModel\Product\Attribute\Backend\TierPrice" type="[vendor]\[module]\Model\ResourceModel\Product\Attribute\Backend\TierPrice" /> 
</config>

script.php

 $p=$productRepo->get('s1');
    echo $p->getName();
    $tier=$p->getTierPrices();
    foreach($tier as $t)
    {
     print_r($t->getData());
    }

returns error Notice: Undefined index: created_by

How to override this class to add additional columns of the tier price table.

Note I have implemented interface for the new columns. So if I directly edit the core resource model, it works.

Это было полезно?

Решение

It appears to me that the only problem with your code is capitalization.

The class you are writing preference for is actually: ...\Backend\Tierprice not ...\Backend\TierPrice (note the 'P')

I have just tested a preference with correct capitalization and works fine for me.

Другие советы

di.xml can live in many different scopes. You need to make sure that it matches the scope of the class that you are overriding. If it does not work, as a first step I would recommend placing it in the root of etc directory in your module. This will make it available in all scopes (or as Magento calls them 'areas'). You need to make sure it is not in frontend or adminhtml to make sure it applies everywhere.

Here you can find more info on what types of scopes exists. http://devdocs.magento.com/guides/v2.0/architecture/archi_perspectives/components/modules/mod_and_areas.html

Лицензировано под: CC-BY-SA с атрибуция
Не связан с magento.stackexchange
scroll top