2

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.

Prince Patel
23.1k10 gold badges102 silver badges124 bronze badges
asked Aug 12, 2017 at 2:49
2
  • Can you show me the full tree for your module? Also, did you mean to put [vendor]\[module] in your di.xml? Commented Aug 16, 2017 at 19:36
  • @nick.graziano [vendor] is replaced with actual namespace in my environment Commented Aug 18, 2017 at 8:20

2 Answers 2

2
+50

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.

answered Aug 19, 2017 at 17:07
1
  • 1
    Ahhh... the sweet smell of case sensitivity. So many hours lost on this one... Commented Aug 21, 2017 at 20:18
0

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

answered Aug 18, 2017 at 17:50
1
  • di.xml exists directly under etc/ Commented Aug 18, 2017 at 17:53

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.