Skip to content

Commit

Permalink
Patch Release : Merge pull request #419 from helloextend/PUE_Order_de…
Browse files Browse the repository at this point in the history
…tails_parent_order

PUE orders update the parent order item with warranty orders
  • Loading branch information
jm-extend authored Oct 17, 2024
2 parents 5cd83b9 + d78f274 commit f5e5c3c
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 3 deletions.
52 changes: 51 additions & 1 deletion Observer/CreateOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
use Exception;
use Extend\Warranty\Model\CreateContract as WarrantyContractCreate;
use \Magento\Sales\Model\ResourceModel\Order\Item\CollectionFactory as OrderItemCollectionFactory;
use Extend\Warranty\Helper\Api\Magento\Data;
use Magento\Sales\Api\OrderRepositoryInterface;


/**
* Class CreateLead
Expand All @@ -40,6 +43,13 @@ class CreateOrder implements ObserverInterface
*/
private $orderItemRepository;

/**
* Order Repository Model
*
* @var OrderRepositoryInterface
*/
private $orderRepository;

/**
* ExtendOrder Model
*
Expand Down Expand Up @@ -79,21 +89,28 @@ class CreateOrder implements ObserverInterface
* CreateLead constructor
*
* @param OrderItemRepositoryInterface $orderItemRepository
* @param OrderRepositoryInterface $orderRepository
* @param ExtendOrder $extendOrder
* @param DataHelper $dataHelper
* @param LoggerInterface $logger
* @param WarrantyContractCreate $warrantyContractCreate
* @param \Magento\Sales\Model\ResourceModel\Order\Item\CollectionFactory $orderItemCollectionFactory
*
*/

public function __construct(
OrderItemRepositoryInterface $orderItemRepository,
OrderRepositoryInterface $orderRepository,
ExtendOrder $extendOrder,
DataHelper $dataHelper,
LoggerInterface $logger,
WarrantyContractCreate $warrantyContractCreate,
\Magento\Sales\Model\ResourceModel\Order\Item\CollectionFactory $orderItemCollectionFactory

)
{
$this->orderItemRepository = $orderItemRepository;
$this->orderRepository = $orderRepository;
$this->extendOrder = $extendOrder;
$this->dataHelper = $dataHelper;
$this->logger = $logger;
Expand Down Expand Up @@ -128,7 +145,6 @@ public function execute(Observer $observer)
return;
}


/** @var OrderItemInterface $orderItem */
foreach ($order->getAllItems() as $orderItem) {
$orderItem->setOrder($order);
Expand Down Expand Up @@ -179,10 +195,44 @@ private function processBuyRequestLeadToken(OrderItemInterface $warrantyItem)
$parentOrderId = $existingOrderItem->getOrderId();
$warrantyItem->setExtendParentOrderId($parentOrderId);
}

// Find the parent order, find the item and add the current order ID as a custom option
// goal = be able to identify the warranty order from the original order without a warranty item

$original_order = $this->orderRepository->get($existingOrderItem->getOrderId());

foreach ($original_order->getAllItems() as $item) {
if ($item->getSku() === $existingOrderItem->getSku() && $item->getQuoteItemId() == $existingOrderItem->getQuoteItemId()) {
$this->updateOrderItemBuyRequest($item, $warrantyItem->getOrderId());
}
}
}
}
} catch (Exception $exception) {
$this->logger->error('Error during lead saving. ' . $exception->getMessage());
}
}

protected function updateOrderItemBuyRequest( $item, $value)
{
$buyRequest = $item->getProductOptionByCode('info_buyRequest');
if (!$buyRequest) {
return;
}

if (!key_exists('extend_warranty_order_id', $buyRequest) || !is_array($buyRequest['extend_warranty_order_id'])){
$buyRequest['extend_warranty_order_id'] = array();
}

// Add the "extend_warranty_order_id" to the buy request.
$buyRequest['extend_warranty_order_id'][] = $value;

// Save the updated buy request back to the item.
$item->setProductOptions(array_merge(
$item->getProductOptions(),
['info_buyRequest' => $buyRequest]
));
$item->save();
}

}
17 changes: 17 additions & 0 deletions view/adminhtml/templates/items/column/name.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,23 @@ use Magento\Framework\View\Helper\SecureHtmlRenderer;
<br /><br/><span><?= /* @noEscape */ $block->escapeHtml(__('Parent Order ID'))?>: </span><?= /* @noEscape */ $incrementId ?>
<?php endif; ?>
</div>
<!-- if not a warranty but PUE order information is present -->
<?php else: ?>
<?php
$_productOptions = $_item->getProductOptions();
$warrantyOrderId = $_productOptions["info_buyRequest"]["extend_warranty_order_id"] ?? null;
if ($warrantyOrderId && is_array($warrantyOrderId)){
?>
<div class="product-warranty-block">
<br /><span> <?php echo "Warranty Order ID :";
foreach ($warrantyOrderId as $warrantyOrder ) {
echo "<br><a href='" . $block->getUrl('sales/order/view', ['order_id' => $warrantyOrder]) . "'>" . $viewModel->getOrderIncrementId($warrantyOrder) . "</a>";
}
?>
</div>
<?php } ?>


<?php endif; ?>

<?php if ($block->getOrderOptions()): ?>
Expand Down
16 changes: 14 additions & 2 deletions view/frontend/templates/order/item/additional-info.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@

$_item = $block->getItem();
$block->getChildBlock('order.item.warranty')->setItem($_item);
$_productOptions = $_item->getProductOptions();

if ($_item->getProductType()=='warranty'){

$_productOptions = $_item->getProductOptions();
$_planId = isset($_productOptions["warranty_id"]) ? $_productOptions["warranty_id"] : '';
$_parentSku= isset($_productOptions["associated_product"]) ? $_productOptions["associated_product"] : '';
$_contractID = $viewModel->unserialize($_item->getContractId()) ?? [];
Expand Down Expand Up @@ -51,6 +50,19 @@
if ($parentOrderId && ($parentOrderId <> $currentOrderId) && $incrementId) {
echo " Parent Order: <a href='". $block->getUrl('sales/order/view', ['order_id' => $parentOrderId])."'>".$incrementId."</a>";
}
}else{

// product is not a warranty item. check if there is a warranty purchased tied to it
$currentOrderId = $_item->getOrderId();
$warrantyOrderId = $_productOptions["info_buyRequest"]["extend_warranty_order_id"] ?? null;
// $warrantyIncrementId = $warrantyOrderId ? $viewModel->getOrderIncrementId($warrantyOrderId) : null;

if ($warrantyOrderId && is_array($warrantyOrderId) && ($warrantyOrderId <> $currentOrderId)) {
echo "Warranty Order ID :";
foreach ($warrantyOrderId as $warrantyOrder ) {
echo "<br><a href='" . $block->getUrl('sales/order/view', ['order_id' => $warrantyOrder]) . "'>" . $viewModel->getOrderIncrementId($warrantyOrder) . "</a>";
}
}
}
?>
<?= $block->getChildHtml('', false);

0 comments on commit f5e5c3c

Please sign in to comment.