0

currently i can't find an answer and hope someone can guide and help me to solve my problem.

In the shop i support the qty of a product are packages of items. We have added a field to the product in which one can save the amount of items inside one package.

My problem is that i have to add a column to the product list containing the total items

eg: qty * items

Would nice if someone has an idea, does't have to be a complete solution, but would be nice if it is :)

Thanks for reading

1 Answer 1

0

Found a Solution:

In: COMPANY/MODULE/view/adminhtml/ui_component/product_listing.xml

<?xml version="1.0" encoding="UTF-8"?>
<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="gn_amount" class="COMPANY\MODULE\Ui\Component\Listing\Columns\Amount">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Menge Stück</item>
                    <item name="add_field" xsi:type="boolean">true</item>
                    <item name="sortOrder" xsi:type="number">77</item>
                    <item name="sortable" xsi:type="boolean">false</item>
                    <item name="filter" xsi:type="string">false</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

The Example below uses a custom attribute, so take care while copying And than In: COMPANY/MODULE/Ui/Component/Listing/Columns/Amount.php

<?php
namespace COMPANY\MODULE\Ui\Component\Listing\Columns;
class Amount extends \Magento\Ui\Component\Listing\Columns\Column {

    const NAME = 'column.amount';

    public function prepareDataSource(array $dataSource) {
        if (isset($dataSource['data']['items'])) {
            $fieldName = $this->getData('name');
            foreach ($dataSource['data']['items'] as & $item) {
                $packaging_quantity = 0;
                $qty = 1;
                if (isset($item['packaging_quantity'])) {
                    $packaging_quantity = (int) $item['packaging_quantity'];
                }
                if (isset($item['qty'])) {
                    $qty = (int) $item['qty'];
                }
                $item['amount'] = $packaging_quantity * $qty;
            }
        }

        return $dataSource;
    }
}
?>

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.