This commit is contained in:
shopkeeperdev
2025-10-21 14:55:37 -04:00
parent e9bcd8551f
commit 61f40a0d7a
5 changed files with 163 additions and 0 deletions

View File

@@ -0,0 +1,102 @@
<?php
namespace Shopkeeper\OrderItemsCustom\Plugin\Order\View\Items\Renderer;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
class DefaultRenderer
{
protected $productRepository;
public function __construct(
ProductRepositoryInterface $productRepository
) {
$this->productRepository = $productRepository;
}
/**
* Modify columns to remove tax columns and add cost column
*
* @param \Magento\Sales\Block\Adminhtml\Order\View\Items\Renderer\DefaultRenderer $subject
* @param array $result
* @return array
*/
public function afterGetColumns($subject, $result)
{
// Remove tax columns
unset($result['tax-amount']);
unset($result['tax-percent']);
// Add cost column after price
$newColumns = [];
foreach ($result as $key => $value) {
$newColumns[$key] = $value;
if ($key === 'price') {
$newColumns['cost'] = 'col-cost';
}
}
return $newColumns;
}
/**
* Add HTML rendering for cost column
*
* @param \Magento\Sales\Block\Adminhtml\Order\View\Items\Renderer\DefaultRenderer $subject
* @param string $result
* @param \Magento\Sales\Model\Order\Item $item
* @param string $column
* @return string
*/
public function afterGetColumnHtml($subject, $result, $item, $column)
{
if ($column === 'cost') {
try {
$product = $this->productRepository->getById($item->getProductId());
$cost = $product->getCost();
if ($cost) {
return $subject->getOrder()->formatPrice($cost);
} else {
return __('N/A');
}
} catch (NoSuchEntityException $e) {
return __('N/A');
}
}
// Make product name and SKU clickable
if ($column === 'product') {
$productUrl = $subject->getUrl('catalog/product/edit', ['id' => $item->getProductId()]);
// Replace product name with linked version
$productName = $item->getName();
$linkedName = '<a href="' . $productUrl . '" target="_blank" title="' . __('Edit Product') . '">'
. $subject->escapeHtml($productName) . '</a>';
$result = str_replace($subject->escapeHtml($productName), $linkedName, $result);
// Replace SKU with linked version
$sku = $item->getSku();
$linkedSku = '<a href="' . $productUrl . '" target="_blank" title="' . __('Edit Product') . '">'
. $subject->escapeHtml($sku) . '</a>';
$result = str_replace($subject->escapeHtml($sku), $linkedSku, $result);
}
return $result;
}
/**
* Add column header for cost
*
* @param \Magento\Sales\Block\Adminhtml\Order\View\Items\Renderer\DefaultRenderer $subject
* @param string $result
* @param string $columnName
* @return string
*/
public function afterGetHeaderText($subject, $result, $columnName)
{
if ($columnName === 'cost') {
return __('Cost');
}
return $result;
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace Shopkeeper\OrderItemsCustom\Plugin\Order\View;
class ItemsColumns
{
/**
* Modify the columns array to remove tax columns and add cost column
*
* @param \Magento\Sales\Block\Adminhtml\Order\View\Items $subject
* @param array $result
* @return array
*/
public function afterGetColumns($subject, $result)
{
// Remove tax columns
unset($result['tax-amount']);
unset($result['tax-percent']);
// Add cost column after price
$newColumns = [];
foreach ($result as $key => $value) {
$newColumns[$key] = $value;
if ($key === 'price') {
$newColumns['cost'] = 'Cost';
}
}
return $newColumns;
}
}

14
etc/adminhtml/di.xml Normal file
View File

@@ -0,0 +1,14 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Sales\Block\Adminhtml\Order\View\Items\Renderer\DefaultRenderer">
<plugin name="shopkeeper_order_items_custom"
type="Shopkeeper\OrderItemsCustom\Plugin\Order\View\Items\Renderer\DefaultRenderer"
sortOrder="10"/>
</type>
<type name="Magento\Sales\Block\Adminhtml\Order\View\Items">
<plugin name="shopkeeper_order_items_columns"
type="Shopkeeper\OrderItemsCustom\Plugin\Order\View\ItemsColumns"
sortOrder="10"/>
</type>
</config>

9
etc/module.xml Normal file
View File

@@ -0,0 +1,9 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Shopkeeper_OrderItemsCustom" setup_version="1.0.0">
<sequence>
<module name="Magento_Sales"/>
</sequence>
</module>
</config>

8
registration.php Normal file
View File

@@ -0,0 +1,8 @@
<?php
use Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(
ComponentRegistrar::MODULE,
'Shopkeeper_OrderItemsCustom',
__DIR__
);