3

enter image description here

I have create custom module and render product grid with custom column "Store Price" and that value is saved in custom table with product id. Now I want to render store price in that coulmn. Note: that custom column is rendered from "render" option with Block path in addcolumns.

Prepare columns:

    protected function _prepareColumns()
    {
        $this->addColumn(
            'entity_id',
            [
                'header' => __('Product Id'),
                'sortable' => true,
                'index' => 'entity_id',
                'header_css_class' => 'col-id',
                'column_css_class' => 'col-id'
            ]
        );
        $this->addColumn(
            'name',
            [
                'header' => __('Product Name'),
                'index' => 'name'
            ]
        );
        $this->addColumn(
            'sku',
            [
                'header' => __('Sku'),
                'index' => 'sku'
            ]
        );

        $this->addColumn(
            'price',
            [
                'header' => __('Price'),
                'index' => 'price'
            ]
        );

        $this->addColumn(
            'store_price',
            [
                'header' => __('Store Price'),
                'index' => 'entity_id',
                'renderer'  => '\Vendor\Modulename\Block\Adminhtml\Template\Renderer\StorePrice',
            ]
        );

        $this->addColumn(
            'product_id',
            [
                'header' => __('Select Product'),
                'sortable' => false,
                'type' => 'checkbox',
                'field_name' => 'product_id[]',
                'index' => 'entity_id',
                'header_css_class' => 'col-id',
                'column_css_class' => 'col-id'
            ]
        );



        return parent::_prepareColumns();

Render File:

namespace Vendor\Modulename\Block\Adminhtml\Template\Renderer;
use Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer;
use Magento\Framework\DataObject;
use Magento\Store\Model\StoreManagerInterface;

class StorePrice extends AbstractRenderer
{
 private $_storeManager;
 /**
  * @param \Magento\Backend\Block\Context $context
  * @param array $data
  */
 public function __construct(\Magento\Backend\Block\Context $context,   StoreManagerInterface $storemanager, array $data = [])
{
    $this->_storeManager = $storemanager;
    parent::__construct($context, $data);
    $this->_authorization = $context->getAuthorization();
}
/**
 * Renders grid column
 *
 * @param Object $row
 * @return  string
 */
public function render(DataObject $row)
{

    return '<input type="text" maxlength="10" name="product_price[]">';
}
}

Thanks in advanced

5

Add renderer class in grid block xml as shown below:

<block class="Magento\Backend\Block\Widget\Grid\Column" as="store_price">
  <arguments>
    <argument name="header" xsi:type="string" translate="true">Store Price</argument>
    <argument name="index" xsi:type="string">product_id</argument>
    <argument name="renderer" xsi:type="string">YourPackage\YourModule\Block\Adminhtml\YourModule\Grid\Renderer\StorePrice</argument>
  </arguments>
</block>

Then create a renderer class in YourPackage\YourModule\Block\Adminhtml\YourModule\Grid\Renderer as shown below:

namespace YourPackage\YourModule\Block\Adminhtml\YourModule\Grid\Renderer;

use YourPackage\YourModule\Model\StorePriceFactory;

class StorePrice extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer
{
   protected $_modelStorePriceFactory;

   public function __construct(StorePriceFactory $modelStorePriceFactory)
   {
     $this->_modelStorePriceFactory = $modelStorePriceFactory;
   }


public function render(\Magento\Framework\DataObject $row)
{
   $value = parent::render($row);
   $modelStorePriceFactory = $this->_modelStorePriceFactory->create();
   $storeCollection = $modelStorePriceFactory->getCollection()->addFieldToFilter('product_id',$value);
   $storeData=$storeCollection->getData();
   if(count($storeData)>0){
       return $storeData[0]['store_price'];
   }
   else{
       return false;
   }
  }
}

**please use variable, model as per you define.

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.