Changing directory structure for Composer compatibility
This commit is contained in:
104
Plugin/Amasty/FilterPlugin.php
Normal file
104
Plugin/Amasty/FilterPlugin.php
Normal file
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
namespace Shopkeeper\PDFingPO\Plugin\Amasty;
|
||||
|
||||
use Magento\Sales\Model\Order;
|
||||
use Magento\Catalog\Api\ProductRepositoryInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class FilterPlugin
|
||||
{
|
||||
protected $productRepository;
|
||||
protected $logger;
|
||||
protected $costVariablesAdded = false;
|
||||
|
||||
public function __construct(
|
||||
ProductRepositoryInterface $productRepository,
|
||||
LoggerInterface $logger
|
||||
) {
|
||||
$this->productRepository = $productRepository;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add variables before filtering
|
||||
*/
|
||||
public function beforeFilter($subject, $value)
|
||||
{
|
||||
if (!$this->costVariablesAdded) {
|
||||
// Get existing variables
|
||||
$variables = [];
|
||||
if (method_exists($subject, 'getVariables')) {
|
||||
$variables = $subject->getVariables();
|
||||
} else {
|
||||
// Fallback to reflection
|
||||
try {
|
||||
$reflection = new \ReflectionObject($subject);
|
||||
$propNames = ['_templateVars', 'templateVars', 'variables']; // Common property names
|
||||
foreach ($propNames as $propName) {
|
||||
if ($reflection->hasProperty($propName)) {
|
||||
$prop = $reflection->getProperty($propName);
|
||||
$prop->setAccessible(true);
|
||||
$variables = $prop->getValue($subject);
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->info('FilterPlugin: Could not access variables: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($variables['order']) && $variables['order'] instanceof Order) {
|
||||
$this->logger->info('FilterPlugin: Adding cost variables for order ' . $variables['order']->getIncrementId());
|
||||
|
||||
$order = $variables['order'];
|
||||
$itemsHtml = '';
|
||||
$totalCost = 0;
|
||||
|
||||
foreach ($order->getAllVisibleItems() as $item) {
|
||||
try {
|
||||
$product = $this->productRepository->getById($item->getProductId());
|
||||
$cost = (float)($product->getCost() ?: $product->getData('cost') ?: 0);
|
||||
} catch (\Exception $e) {
|
||||
$cost = 0;
|
||||
}
|
||||
|
||||
$qty = (float) $item->getQtyOrdered();
|
||||
$rowTotal = $cost * $qty;
|
||||
$totalCost += $rowTotal;
|
||||
|
||||
$itemsHtml .= '<tr>';
|
||||
$itemsHtml .= '<td style="border: 1px solid #000; padding: 5px;">' . htmlspecialchars($item->getName()) . '</td>';
|
||||
$itemsHtml .= '<td style="border: 1px solid #000; padding: 5px;">' . htmlspecialchars($item->getSku()) . '</td>';
|
||||
$itemsHtml .= '<td style="border: 1px solid #000; padding: 5px; text-align: center;">' . (int)$qty . '</td>';
|
||||
$itemsHtml .= '<td style="border: 1px solid #000; padding: 5px; text-align: right;">' . $this->formatPrice($cost, $order) . '</td>';
|
||||
$itemsHtml .= '<td style="border: 1px solid #000; padding: 5px; text-align: right;">' . $this->formatPrice($rowTotal, $order) . '</td>';
|
||||
$itemsHtml .= '</tr>';
|
||||
}
|
||||
|
||||
$shippingCost = $order->getShippingAmount() ?: 0;
|
||||
|
||||
// Add new variables to the array
|
||||
$variables['items_html'] = $itemsHtml;
|
||||
$variables['order_subtotal_cost'] = $this->formatPrice($totalCost, $order);
|
||||
$variables['order_total_cost'] = $this->formatPrice($totalCost + $shippingCost, $order);
|
||||
$variables['shipping_cost'] = $this->formatPrice($shippingCost, $order);
|
||||
$variables['raw_subtotal_cost'] = $totalCost;
|
||||
$variables['raw_total_cost'] = $totalCost + $shippingCost;
|
||||
$variables['raw_shipping_cost'] = $shippingCost;
|
||||
|
||||
// Set the updated variables array
|
||||
$subject->setVariables($variables);
|
||||
|
||||
$this->costVariablesAdded = true;
|
||||
$this->logger->info('FilterPlugin: Cost variables added successfully');
|
||||
}
|
||||
}
|
||||
|
||||
return [$value];
|
||||
}
|
||||
|
||||
protected function formatPrice($price, $order)
|
||||
{
|
||||
return $order->getOrderCurrency()->formatPrecision($price, 2, [], false);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user