1

We wish to add the extra column Cost to the Items Ordered grid when creating an order in Magento 2. See image below.

enter image description here

Hopefully someone can help us out! I already tried creating a custom module overwriting the Magento/Sales/Block/Adminhtml/Order/Create/Items/Grid.php file but haven't got it working as of yet.

What we want as outcome is the following:

enter image description here

At least the Cost column would be required, but eventually a calculated Margin would be great!

Edit #1:

Following @Nayem I have overwritten magento/sales/view/adminhtml/templates/order/create/items/grid.phtml and the columns are created but I don't get a value as output. My file is now looking like this:

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// phpcs:disable Magento2.Templates.ThisInTemplate
?>
<?php
/**
 * @var $block \Magento\Sales\Block\Adminhtml\Order\Create\Items\Grid
 */
?>

<?php $_items = $block->getItems() ?>
<?php if (empty($_items)) : ?>
    <div id="order-items_grid">
        <div class="admin__table-wrapper">
            <table class="data-table admin__table-primary order-tables">
                <thead>
                    <tr class="headings">
                        <th class="col-product"><span><?= $block->escapeHtml(__('Product')) ?></span></th>
                        <th class="col-price"><span><?= $block->escapeHtml(__('Price')) ?></span></th>
                        <th class="col-cost"><span><?= $block->escapeHtml(__('Cost')) ?></span></th>
                        <th class="col-margin"><span><?= $block->escapeHtml(__('Margin')) ?></span></th>
                        <th class="col-qty"><span><?= $block->escapeHtml(__('Qty')) ?></span></th>
                        <th class="col-subtotal"><span><?= $block->escapeHtml(__('Subtotal')) ?></span></th>
                        <th class="col-discount"><span><?= $block->escapeHtml(__('Discount')) ?></span></th>
                        <th class="col-row-total"><span><?= $block->escapeHtml(__('Row Subtotal')) ?></span></th>
                        <th class="col-action"><span><?= $block->escapeHtml(__('Action')) ?></span></th>
                    </tr>
                </thead>
                <tbody>
                    <tr class="even">
                        <td class="empty-text" colspan="100"><?= $block->escapeHtml(__('No ordered items')) ?></td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
<?php else : ?>
    <div class="admin__table-wrapper" id="order-items_grid">
    <?php if (count($_items) > 10) : ?>
        <div class="actions update actions-update">
            <?= $block->getButtonHtml(__('Update Items and Quantities'), 'order.itemsUpdate()', 'action-secondary') ?>
        </div>
    <?php endif; ?>
        <table class="data-table admin__table-primary order-tables">
            <thead>
                <tr class="headings">
                    <th class="col-product"><span><?= $block->escapeHtml(__('Product')) ?></span></th>
                    <th class="col-price"><span><?= $block->escapeHtml(__('Price')) ?></span></th>
                    <th class="col-cost"><span><?= $block->escapeHtml(__('Cost')) ?></span></th>
                    <th class="col-margin"><span><?= $block->escapeHtml(__('Margin')) ?></span></th>
                    <th class="col-qty"><span><?= $block->escapeHtml(__('Qty')) ?></span></th>
                    <th class="col-subtotal"><span><?= $block->escapeHtml(__('Subtotal')) ?></span></th>
                    <th class="col-discount"><span><?= $block->escapeHtml(__('Discount')) ?></span></th>
                    <th class="col-row-total"><span><?= $block->escapeHtml(__('Row Subtotal')) ?></span></th>
                    <th class="col-action"><span><?= $block->escapeHtml(__('Action')) ?></span></th>
                </tr>
            </thead>
            <tfoot>
                <tr>
                    <td class="col-total"><?= $block->escapeHtml(__('Total %1 product(s)', count($_items))) ?></td>
                    <td colspan="2" class="col-subtotal"><?= $block->escapeHtml(__('Subtotal:')) ?></td>
                    <td class="col-price"><strong><?= /* @noEscape */ $block->formatPrice($block->getSubtotal()) ?></strong></td>
                    <td class="col-price"><strong><?= /* @noEscape */ $block->formatPrice($block->getDiscountAmount()) ?></strong></td>
                    <td class="col-price"><strong><?= /* @noEscape */ $block->formatPrice($block->getSubtotalWithDiscount()); ?></strong></td>
                    <td colspan="2">&nbsp;</td>
                </tr>
            </tfoot>
                <?php $i = 0 ?>
                <?php foreach ($_items as $_item) : $i++ ?>
                <tbody class="<?= /* @noEscape */ ($i%2) ? 'even' : 'odd' ?>">
                    <tr>
                        <td class="col-product">
                            <span id="order_item_<?= (int) $_item->getId() ?>_title"><?= $block->escapeHtml($_item->getName()) ?></span>
                            <div class="product-sku-block">
                                <span><?= $block->escapeHtml(__('SKU')) ?>:</span>
                                <?= /* @noEscape */ implode('<br />', $this->helper(\Magento\Catalog\Helper\Data::class)->splitSku($block->escapeHtml($_item->getSku()))) ?>
                            </div>
                            <div class="product-configure-block">
                                <?= $block->getConfigureButtonHtml($_item) ?>
                            </div>
                        </td>
                        <td class="col-price">

                            <?= $block->getItemUnitPriceHtml($_item) ?>

                            <?php $_isCustomPrice = $block->usedCustomPriceForItem($_item) ?>
                            <?php if ($_tier = $block->getTierHtml($_item)) : ?>
                            <div id="item_tier_block_<?= (int) $_item->getId() ?>"<?php if ($_isCustomPrice) : ?> style="display:none"<?php endif; ?>>
                                <a href="#" onclick="$('item_tier_<?= (int) $_item->getId() ?>').toggle();return false;"><?= $block->escapeHtml(__('Tier Pricing')) ?></a>
                                <div style="display:none" id="item_tier_<?= (int) $_item->getId() ?>"><?= /* @noEscape */ $_tier ?></div>
                            </div>
                            <?php endif; ?>
                            <?php if ($block->canApplyCustomPrice($_item)) : ?>
                                <div class="custom-price-block">
                                    <input type="checkbox"
                                           class="admin__control-checkbox"
                                           id="item_use_custom_price_<?= (int) $_item->getId() ?>"
                                           <?php if ($_isCustomPrice) : ?> checked="checked"<?php endif; ?>
                                           onclick="order.toggleCustomPrice(this, 'item_custom_price_<?= (int) $_item->getId() ?>', 'item_tier_block_<?= (int) $_item->getId() ?>');"/>
                                    <label
                                        class="normal admin__field-label"
                                        for="item_use_custom_price_<?= (int) $_item->getId() ?>">
                                        <span><?= $block->escapeHtml(__('Custom Price')) ?>*</span></label>
                                </div>
                            <?php endif; ?>
                            <input id="item_custom_price_<?= (int) $_item->getId() ?>"
                                name="item[<?= (int) $_item->getId() ?>][custom_price]"
                                value="<?= /* @noEscape */ sprintf("%.2f", $block->getOriginalEditablePrice($_item)) ?>"
                                <?php if (!$_isCustomPrice) : ?>
                                style="display:none"
                                disabled="disabled"
                                <?php endif; ?>
                                class="input-text item-price admin__control-text"/>
                        </td>
                        <td class="col-cost col-price">
                            <?= $block->escapeHtml($_item->getCost()) ?>
                        </td>
                        <td class="col-margin">
                            <?= $block->escapeHtml($_item->getCost()) ?>
                        </td>
                        <td class="col-qty">
                            <input name="item[<?= (int) $_item->getId() ?>][qty]"
                                   class="input-text item-qty admin__control-text"
                                   value="<?= (int) $_item->getQty() ?>"
                                   maxlength="12" />
                        </td>
                        <td class="col-subtotal col-price">
                            <?= $block->getItemRowTotalHtml($_item) ?>
                        </td>
                        <td class="col-discount col-price">
                            <?= /* @noEscape */ $block->formatPrice(-$_item->getTotalDiscountAmount()) ?>
                            <div class="discount-price-block">
                                <input id="item_use_discount_<?= (int) $_item->getId() ?>"
                                       class="admin__control-checkbox"
                                       name="item[<?= (int) $_item->getId() ?>][use_discount]"
                                       <?php if (!$_item->getNoDiscount()) : ?>checked="checked"<?php endif; ?>
                                       value="1"
                                       type="checkbox" />
                                <label
                                    for="item_use_discount_<?= (int) $_item->getId() ?>"
                                    class="normal admin__field-label">
                                    <span><?= $block->escapeHtml(__('Apply')) ?></span></label>
                            </div>

                        </td>
                        <td class="col-price col-row-subtotal">
                            <?= $block->getItemRowTotalWithDiscountHtml($_item) ?>
                        </td>
                        <td class="col-actions last">
                            <select class="admin__control-select" name="item[<?= (int) $_item->getId() ?>][action]">
                                <option value=""><?= $block->escapeHtml(__('Please select')) ?></option>
                                <option value="remove"><?= $block->escapeHtml(__('Remove')) ?></option>
                                <?php if ($block->getCustomerId() && $block->getMoveToCustomerStorage()) : ?>
                                    <option value="cart"><?= $block->escapeHtml(__('Move to Shopping Cart')) ?></option>
                                    <?php if ($block->isMoveToWishlistAllowed($_item)) : ?>
                                        <?php $wishlists = $block->getCustomerWishlists();?>
                                        <?php if (count($wishlists) <= 1) : ?>
                                            <option value="wishlist"><?= $block->escapeHtml(__('Move to Wish List')) ?></option>
                                        <?php else : ?>
                                            <optgroup label="<?= $block->escapeHtml(__('Move to Wish List')) ?>">
                                                <?php foreach ($wishlists as $wishlist) :?>
                                                    <option value="wishlist_<?= (int) $wishlist->getId() ?>"><?= $block->escapeHtml($wishlist->getName()) ?></option>
                                                <?php endforeach;?>
                                            </optgroup>
                                        <?php endif; ?>
                                    <?php endif; ?>
                                <?php endif; ?>
                            </select>
                        </td>
                    </tr>

                    <?php $hasMessageError = false; ?>
                    <?php foreach ($_item->getMessage(false) as $messageError) : ?>
                        <?php if (!empty($messageError)) :
                            $hasMessageError = true;
                        endif; ?>
                    <?php endforeach; ?>

                    <?php if ($hasMessageError) : ?>
                        <tr class="row-messages-error">
                            <td colspan="100"> <!-- ToDo UI: remove the 100 -->
                                <?php foreach ($_item->getMessage(false) as $message) :
                                    if (empty($message)) {
                                        continue;
                                    }
                                    ?>
                                    <div class="message <?php if ($_item->getHasError()) : ?>message-error<?php else : ?>message-notice<?php endif; ?>">
                                        <?= $block->escapeHtml($message) ?>
                                    </div>
                                <?php endforeach; ?>
                            </td>
                        </tr>
                    <?php endif;?>

                    <?= $block->getItemExtraInfo($_item)->toHtml() ?>
                </tbody>
                <?php endforeach; ?>
        </table>
        <p><small><?= $block->escapeHtml($block->getInclExclTaxMessage()) ?></small></p>
    </div>

    <div class="order-discounts">
        <?= $block->getButtonHtml(__('Update Items and Quantities'), 'order.itemsUpdate()', 'action-secondary') ?>
        <div id="order-coupons" class="order-coupons"><?= $block->getChildHtml() ?></div>
    </div>

    <script>
        require([
            'Magento_Sales/order/create/form'
        ], function(){
            order.itemsOnchangeBind()
        });
    </script>
    <?php if ($block->isGiftMessagesAvailable()) : ?>
        <script>
        require([
            "prototype",
            "Magento_Sales/order/giftoptions_tooltip"
        ], function(){

        //<![CDATA[
            /**
             * Retrieve gift options tooltip content
             */
            function getGiftOptionsTooltipContent(itemId) {
                var contentLines = [];
                var headerLine = null;
                var contentLine = null;

                $$('#gift_options_data_' + itemId + ' .gift-options-tooltip-content').each(function (element) {
                    if (element.down(0)) {
                        headerLine = element.down(0).innerHTML;
                        contentLine = element.down(0).next().innerHTML;
                        if (contentLine.length > 30) {
                            contentLine = contentLine.slice(0,30) + '...';
                        }
                        contentLines.push(headerLine + ' ' + contentLine);
                    }
                });
                return contentLines.join('<br/>');
            }

            giftOptionsTooltip.setTooltipContentLoaderFunction(getGiftOptionsTooltipContent);

            window.getGiftOptionsTooltipContent = getGiftOptionsTooltipContent;

        //]]>

        });
        </script>
    <?php endif; ?>
<?php endif; ?>

Edit #2:

Got it working by making two functions in Block/Adminhtml/Order/Create/Items/Grid.php returning cost and margin and using these functions in the template!

2

Override vendor\magento\module-sales\view\adminhtml\layout\sales_order_view.xml file by your custom module. The path will be something like Vendor_name\Module_name\view\adminhtml\layout\sales_order_view.xml

See the below example -

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<referenceBlock name="order_items">
    <arguments>
        <argument name="columns" xsi:type="array">
            <item name="product" xsi:type="string" translate="true">Product</item>
            <item name="ordered-qty" xsi:type="string" translate="true">Quantity</item>
            <item name="status" xsi:type="string" translate="true">Item Status</item>
            <item name="merchant" xsi:type="string" translate="true">Merchant</item>
            <item name="school" xsi:type="string" translate="true">School</item>
            <item name="fundraiser" xsi:type="string" translate="true">Fundraiser</item>
            <!-- <item name="price-original" xsi:type="string" translate="true">Original Price</item> -->
            <!-- <item name="price" xsi:type="string" translate="true">Gift Card</item> -->
            <item name="giftcard" xsi:type="string" translate="true">Gift Card</item>
            <item name="fee" xsi:type="string" translate="true">Service Fee</item>
            <!-- <item name="subtotal" xsi:type="string" translate="true">Order Amount</item> -->
            <item name="fundraised" xsi:type="string" translate="true">Funds Raised</item>
            <!-- <item name="tax-amount" xsi:type="string" translate="true">Tax Amount</item>
            <item name="tax-percent" xsi:type="string" translate="true">Tax Percent</item>
            <item name="discont" xsi:type="string" translate="true">Discount Amount</item>
            <item name="total" xsi:type="string" translate="true">Row Total</item> -->
        </argument>
    </arguments>
    <block class="Magento\Sales\Block\Adminhtml\Order\View\Items\Renderer\DefaultRenderer" as="default" template="order/view/items/renderer/default.phtml">
    <arguments>
        <argument name="columns" xsi:type="array">
            <item name="product" xsi:type="string" translate="false">col-product</item>
            <item name="qty" xsi:type="string" translate="false">col-ordered-qty</item>
            <item name="status" xsi:type="string" translate="false">col-status</item>
            <item name="merchant" xsi:type="string" translate="false">col-merchant</item>
            <item name="school" xsi:type="string" translate="false">col-school</item>
            <item name="fundraiser" xsi:type="string" translate="false">col-fundraiser</item>
            <!-- <item name="price-original" xsi:type="string" translate="false">col-price-original</item> -->
            <!-- <item name="price" xsi:type="string" translate="false">col-price</item> -->
            <item name="giftcard" xsi:type="string" translate="false">col-giftcard</item>
            <item name="fee" xsi:type="string" translate="false">col-fee</item>
            <!-- <item name="subtotal" xsi:type="string" translate="false">col-subtotal</item> -->
            <item name="fundraised" xsi:type="string" translate="false">col-fundraised</item>
            <!-- <item name="tax-amount" xsi:type="string" translate="false">col-tax-amount</item>
            <item name="tax-percent" xsi:type="string" translate="false">col-tax-percent</item>
            <item name="discont" xsi:type="string" translate="false">col-discont</item>
            <item name="total" xsi:type="string" translate="false">col-total</item> -->
        </argument>
    </arguments>
    </block>
    <block class="Magento\Sales\Block\Adminhtml\Items\Column\Qty" name="column_qty" template="items/column/qty.phtml" group="column"/>
    <block class="Magento\Sales\Block\Adminhtml\Items\Column\Name" name="column_name" template="items/column/name.phtml" group="column"/>
    <block class="Magento\Framework\View\Element\Text\ListText" name="order_item_extra_info"/>
</referenceBlock>

| improve this answer | |
  • Mm seems not to work for me.. No columns are added after editing the file. – JorgB Dec 12 '19 at 8:06
  • Clear cache, run setup:upgrade and setup:di:compile – Nayem Dec 12 '19 at 8:16
  • Magento is running in developer mode and I have done a setup:upgrade. I can see the module is active but I dont see any change for the item grid. I just copied your example. I will edit my post to let you know what I did. – JorgB Dec 12 '19 at 8:36
  • Mm can't edit my comment, but I have updated my startpost @Nayem – JorgB Dec 12 '19 at 8:52
  • Ah wait this does change the order view item grid, but I want to edit the order create view item grid! – JorgB Dec 12 '19 at 8:55

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.