Changing directory structure for Composer compatibility
This commit is contained in:
0
Plugin/Amasty/.gitkeep
Normal file
0
Plugin/Amasty/.gitkeep
Normal file
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);
|
||||
}
|
||||
}
|
||||
170
Plugin/Amasty/TemplatePlugin.php
Normal file
170
Plugin/Amasty/TemplatePlugin.php
Normal file
@@ -0,0 +1,170 @@
|
||||
<?php
|
||||
namespace Shopkeeper\PDFingPO\Plugin\Amasty;
|
||||
|
||||
use Magento\Sales\Model\Order;
|
||||
use Magento\Catalog\Api\ProductRepositoryInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class TemplatePlugin
|
||||
{
|
||||
protected $productRepository;
|
||||
protected $logger;
|
||||
protected $isProcessing = false;
|
||||
|
||||
public function __construct(
|
||||
ProductRepositoryInterface $productRepository,
|
||||
LoggerInterface $logger
|
||||
) {
|
||||
$this->productRepository = $productRepository;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add cost variables before processing template
|
||||
*/
|
||||
public function beforeProcessTemplate($subject, array $variables = [])
|
||||
{
|
||||
if (!$this->isProcessing) {
|
||||
$this->logger->info('TemplatePlugin: beforeProcessTemplate called');
|
||||
|
||||
// Check if we have an order in variables
|
||||
if (isset($variables['order']) && $variables['order'] instanceof Order) {
|
||||
$this->logger->info('TemplatePlugin: Found order ' . $variables['order']->getIncrementId());
|
||||
$variables = $this->addCostVariables($variables, $variables['order']);
|
||||
}
|
||||
}
|
||||
|
||||
return [$variables];
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook into setVars to add our variables
|
||||
*/
|
||||
public function afterSetVars($subject, $result, $variables)
|
||||
{
|
||||
// Only process once to avoid infinite loop
|
||||
if (!$this->isProcessing && is_array($variables) && isset($variables['order']) && $variables['order'] instanceof Order) {
|
||||
$this->isProcessing = true;
|
||||
|
||||
$this->logger->info('TemplatePlugin: afterSetVars called - adding cost variables');
|
||||
|
||||
// Add cost variables
|
||||
$enhancedVariables = $this->addCostVariables($variables, $variables['order']);
|
||||
|
||||
// Set variables back
|
||||
$subject->setVars($enhancedVariables);
|
||||
|
||||
$this->isProcessing = false;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add cost variables to the variables array
|
||||
*/
|
||||
protected function addCostVariables($variables, $order)
|
||||
{
|
||||
// Check if we already added these variables
|
||||
if (isset($variables['items_html'])) {
|
||||
$this->logger->info('Cost variables already exist, skipping');
|
||||
return $variables;
|
||||
}
|
||||
|
||||
try {
|
||||
// Build ONLY the table rows (tbody content)
|
||||
$itemsHtml = '';
|
||||
$totalCost = 0;
|
||||
$itemCount = 0;
|
||||
|
||||
foreach ($order->getAllVisibleItems() as $item) {
|
||||
$itemCount++;
|
||||
try {
|
||||
$product = $this->productRepository->getById($item->getProductId());
|
||||
$cost = (float)($product->getCost() ?: $product->getData('cost') ?: 0);
|
||||
|
||||
$this->logger->info('Product ' . $item->getSku() . ' cost: ' . $cost);
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->error('Could not load product: ' . $e->getMessage());
|
||||
$cost = 0;
|
||||
}
|
||||
|
||||
$qty = (float) $item->getQtyOrdered();
|
||||
$rowTotal = $cost * $qty;
|
||||
$totalCost += $rowTotal;
|
||||
|
||||
// Just the row content
|
||||
$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;
|
||||
|
||||
// Create a complete table HTML for standalone use
|
||||
$completeTableHtml = '<table style="width: 100%; border-collapse: collapse;">';
|
||||
$completeTableHtml .= '<thead>';
|
||||
$completeTableHtml .= '<tr>';
|
||||
$completeTableHtml .= '<th style="border: 1px solid #000; padding: 5px; text-align: left;">Item</th>';
|
||||
$completeTableHtml .= '<th style="border: 1px solid #000; padding: 5px; text-align: left;">SKU</th>';
|
||||
$completeTableHtml .= '<th style="border: 1px solid #000; padding: 5px; text-align: center;">Qty</th>';
|
||||
$completeTableHtml .= '<th style="border: 1px solid #000; padding: 5px; text-align: right;">Unit Cost</th>';
|
||||
$completeTableHtml .= '<th style="border: 1px solid #000; padding: 5px; text-align: right;">Total Cost</th>';
|
||||
$completeTableHtml .= '</tr>';
|
||||
$completeTableHtml .= '</thead>';
|
||||
$completeTableHtml .= '<tbody>';
|
||||
$completeTableHtml .= $itemsHtml;
|
||||
$completeTableHtml .= '</tbody>';
|
||||
$completeTableHtml .= '<tfoot>';
|
||||
$completeTableHtml .= '<tr>';
|
||||
$completeTableHtml .= '<td colspan="4" style="border: 1px solid #000; padding: 5px; text-align: right; font-weight: bold;">Subtotal:</td>';
|
||||
$completeTableHtml .= '<td style="border: 1px solid #000; padding: 5px; text-align: right; font-weight: bold;">' . $this->formatPrice($totalCost, $order) . '</td>';
|
||||
$completeTableHtml .= '</tr>';
|
||||
if ($shippingCost > 0) {
|
||||
$completeTableHtml .= '<tr>';
|
||||
$completeTableHtml .= '<td colspan="4" style="border: 1px solid #000; padding: 5px; text-align: right;">Shipping:</td>';
|
||||
$completeTableHtml .= '<td style="border: 1px solid #000; padding: 5px; text-align: right;">' . $this->formatPrice($shippingCost, $order) . '</td>';
|
||||
$completeTableHtml .= '</tr>';
|
||||
}
|
||||
$completeTableHtml .= '<tr>';
|
||||
$completeTableHtml .= '<td colspan="4" style="border: 1px solid #000; padding: 5px; text-align: right; font-weight: bold;">Grand Total:</td>';
|
||||
$completeTableHtml .= '<td style="border: 1px solid #000; padding: 5px; text-align: right; font-weight: bold;">' . $this->formatPrice($totalCost + $shippingCost, $order) . '</td>';
|
||||
$completeTableHtml .= '</tr>';
|
||||
$completeTableHtml .= '</tfoot>';
|
||||
$completeTableHtml .= '</table>';
|
||||
|
||||
$terms = $order->getData('terms') ?? ''; // Or $order->getTerms() if it's a getter
|
||||
$termsHtml = str_replace(',', '<br>', htmlspecialchars($terms));
|
||||
|
||||
// Add variables for both approaches
|
||||
$variables['items_html'] = $itemsHtml; // Just rows for inserting into existing table
|
||||
$variables['items_complete_table'] = $completeTableHtml; // Complete table
|
||||
$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;
|
||||
$variables['terms_html'] = $termsHtml;
|
||||
|
||||
// Test variable
|
||||
$variables['test_cost_injection'] = 'WORKING';
|
||||
|
||||
$this->logger->info('Added cost variables - Items: ' . $itemCount . ', Total: ' . $totalCost);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->error('Error adding cost variables: ' . $e->getMessage());
|
||||
}
|
||||
|
||||
return $variables;
|
||||
}
|
||||
|
||||
protected function formatPrice($price, $order)
|
||||
{
|
||||
return $order->getOrderCurrency()->formatPrecision($price, 2, [], false);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user