103 lines
3.3 KiB
PHP
103 lines
3.3 KiB
PHP
|
|
<?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;
|
||
|
|
}
|
||
|
|
}
|