I want to show Product Custom Attribute in edit page of Invoice, Shipments and Credit Memos below SKU in admin in Magento2.2.0? Refer my Screenshot.
Any help would be appreciated.
-
Can you check my answer?Sukumar Gorai– Sukumar Gorai2018年11月28日 07:24:42 +00:00Commented Nov 28, 2018 at 7:24
2 Answers 2
Create a module with name STech_Salesattribute by following below steps:
Step 1: Create registration.php under
app/code/STech/Salesattribute/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'STech_Salesattribute',
__DIR__
);
Step 2: Create module.xml under
app/code/STech/Salesattribute/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="STech_Salesattribute" setup_version="0.0.1">
<sequence>
<module name="Magento_Sales"/>
<module name="Magento_Quote"/>
<module name="Magento_Checkout"/>
</sequence>
</module>
</config>
Step 3: Create adminhtml_order_shipment_new.xml under
app/code/STech/Salesattribute/view/adminhtml/layout/adminhtml_order_shipment_new.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="column_name" template="STech_Salesattribute::items/column/name.phtml"/>
</body>
</page>
Step 4: Create sales_order_creditmemo_new.xml under
app/code/STech/Salesattribute/view/adminhtml/layout/sales_order_creditmemo_new.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="column_name" template="STech_Salesattribute::items/column/name.phtml"/>
</body>
</page>
Step 5: Create sales_order_invoice_new.xml under
app/code/STech/Salesattribute/view/adminhtml/layout/sales_order_invoice_new.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="column_name" template="STech_Salesattribute::items/column/name.phtml"/>
</body>
</page>
Step 6: Create name.phtml under
app/code/STech/Salesattribute/view/adminhtml/templates/items/column/name.phtml
<?php if ($_item = $block->getItem()): ?>
<div id="order_item_<?= /* @escapeNotVerified */ $_item->getId() ?>_title"
class="product-title">
<?= $block->escapeHtml($_item->getName()) ?>
</div>
<div class="product-sku-block">
<span><?= /* @escapeNotVerified */ __('SKU') ?>:</span> <?= implode('<br />', $this->helper('Magento\Catalog\Helper\Data')->splitSku($block->escapeHtml($block->getSku()))) ?>
</div>
<?php if ($block->getOrderOptions()): ?>
<dl class="item-options">
<?php foreach ($block->getOrderOptions() as $_option): ?>
<dt><?= /* @escapeNotVerified */ $_option['label'] ?>:</dt>
<dd>
<?php if (isset($_option['custom_view']) && $_option['custom_view']): ?>
<?= /* @escapeNotVerified */ $block->getCustomizedOptionValue($_option) ?>
<?php else: ?>
<?php $_option = $block->getFormattedOption($_option['value']); ?>
<?= /* @escapeNotVerified */ $_option['value'] ?><?php if (isset($_option['remainder']) && $_option['remainder']): ?><span id="<?= /* @escapeNotVerified */ $_dots = 'dots' . uniqid() ?>"> ...</span><span id="<?= /* @escapeNotVerified */ $_id = 'id' . uniqid() ?>"><?= /* @escapeNotVerified */ $_option['remainder'] ?></span>
<script>
require(['prototype'], function() {
$('<?= /* @escapeNotVerified */ $_id ?>').hide();
$('<?= /* @escapeNotVerified */ $_id ?>').up().observe('mouseover', function(){$('<?= /* @escapeNotVerified */ $_id ?>').show();});
$('<?= /* @escapeNotVerified */ $_id ?>').up().observe('mouseover', function(){$('<?= /* @escapeNotVerified */ $_dots ?>').hide();});
$('<?= /* @escapeNotVerified */ $_id ?>').up().observe('mouseout', function(){$('<?= /* @escapeNotVerified */ $_id ?>').hide();});
$('<?= /* @escapeNotVerified */ $_id ?>').up().observe('mouseout', function(){$('<?= /* @escapeNotVerified */ $_dots ?>').show();});
});
</script>
<?php endif; ?>
<?php endif; ?>
</dd>
<?php endforeach; ?>
</dl>
<?php endif; ?>
<?php //Your code starts to show custom attribute value ?>
<dl class="item-options">
<dt><?= __('Custom Label') ?>:</dt>
<dd><?= $_item->getProduct()->getCustom() ?></dd>
</dl>
<?php //Your code ends to show custom attribute value ?>
<?= $block->escapeHtml($_item->getDescription()) ?>
<?php endif; ?>
Thats it!. Now run setup upgrade and other required commands and check. Don't forget to replace your attribute code in place of "custom".
-
I have one more doubt, your code is perfecting working for simple product. But for Bundle product the Custom attribute is not showing and for Configurable product the Custom attribute is showing only for the main product. Do you know any idea regarding this.Amy– Amy2018年11月28日 10:49:14 +00:00Commented Nov 28, 2018 at 10:49
-
You need to check the product ID, if its bundle then you need to put condition and load the product accordinglySukumar Gorai– Sukumar Gorai2018年11月28日 11:11:06 +00:00Commented Nov 28, 2018 at 11:11
-
how to override invoice pdf in magento 2Manu– Manu2020年11月21日 12:59:50 +00:00Commented Nov 21, 2020 at 12:59
Here is the example of override invoice file in a custom module
Create sales_order_invoice_new, sales_order_invoice_updateqty, sales_order_invoice_view
app/code/Vendor/Module/view/adminhtml/layout/sales_order_invoice_view.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="order_items.default">
<action method="setTemplate">
<argument name="template" xsi:type="string">Vendor_Module::order/invoice/create/items/renderer/default.phtml</argument>
</action>
</referenceBlock>
</body>
</page>
Now override
vendor/magento/module-sales/view/adminhtml/templates/order/invoice/create/items/renderer/default.phtml
to
app/code/Vendor/Module/view//adminhtml/templates/order/invoice/create/items/renderer/default.phtml
Here in default.phtml you need to load full product object by product id $_item->getProductId() and get custom attribute value
<?php $_item = $block->getItem() ?>
<?php $block->setPriceDataObject($_item)?>
<td class="col-product"><?= $block->getColumnHtml($_item, 'name') ?></td>
<?php
// Load product by $_item->getProductId() and get custom attribute value here */
?>
<td class="col-price">
<?= $block->getColumnHtml($_item, 'price') ?>
</td>
For creditmemo override
vendor/magento/module-sales/view/adminhtml/templates/order/creditmemo/create/items/renderer/default.phtml
For shipment override
vendor/magento/module-sales/view/adminhtml/templates/items/column/name.phtml