0

We have two questions about the catalog grid in Magento 2:

  • By default there is a qty column, but this value has a dot and 4 zero's behind it. How can we show just the rounded value?
  • We've added the 'Cost Price' column, but this is not shown as a price. How can we format this column as price.

See: https://www.dropbox.com/s/fy5sfgxerg7ugp9/Schermafbeelding%202017-12-05%20om%2010.31.29.png?dl=0

0

I have created module for this.

Fellow below steps:

Step:1 Create registration.php file at location

app\code\AR\CustomCatalog\

and add below code

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'AR_CustomCatalog',
    __DIR__
);

Setp:2 Create module.xml file at location

app\code\AR\CustomCatalog\etc\

and add below code

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2015 Ccc. All rights reserved.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="AR_CustomCatalog" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Catalog"/>
            <module name="Magento_CatalogInventory"/>
        </sequence>
   </module>
</config>

Step:3 Create product_listing.xml file at location

app\code\AR\CustomCatalog\view\adminhtml\ui_component

and add below code in file

<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <columns name="product_columns" class="Magento\Catalog\Ui\Component\Listing\Columns">
        <column name="qty" class="AR\CustomCatalog\Ui\Component\Listing\Columns\Quantity">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">textRange</item>
                    <item name="add_field" xsi:type="boolean">true</item>
                    <item name="label" xsi:type="string" translate="true">Quantity</item>
                    <item name="sortOrder" xsi:type="number">75</item>
                </item>
            </argument>
        </column>
        <column name="cost" class="Magento\Catalog\Ui\Component\Listing\Columns\Price">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">textRange</item>
                    <item name="add_field" xsi:type="boolean">true</item>
                    <item name="label" xsi:type="string" translate="true">Cost</item>
                    <item name="sortOrder" xsi:type="number">80</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

Step:4 Create Quantity.php file at location

app\code\AR\CustomCatalog\Ui\Component\Listing\Columns

and add below code in file

<?php

namespace AR\CustomCatalog\Ui\Component\Listing\Columns;

use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Framework\View\Element\UiComponent\ContextInterface;

class Quantity extends \Magento\Ui\Component\Listing\Columns\Column
{
    /**
     * Column name
     */
    const NAME = 'column.qty';

    /**
     * Prepare Data Source
     *
     * @param array $dataSource
     * @return array
     */
    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            $fieldName = $this->getData('name');
            foreach ($dataSource['data']['items'] as & $item) {
                if (isset($item[$fieldName])) {
                    $item[$fieldName] = round($item[$fieldName] ,0);
                }
            }
        }
        return $dataSource;
    }
}

setp:5 run command bin/magento setup:upgrade and check

| improve this answer | |

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.