Skip to content

Commit

Permalink
Allow tracking of Parent Order when purchasing from a Lead Token
Browse files Browse the repository at this point in the history
where to see the changes
- consume a lead token and add the warranty to the cart.
- in the minicart/cart you will see the details of the warranty now include "Parent Order" and the increment ID of the parent order
- in the database, you now have an extra column in sales_order_item called "extend_parent_order_id" that will have the order ID (not the increment ID) of the parent order from where the lead Token originates
  • Loading branch information
jm-extend committed Aug 14, 2024
1 parent 6c04db0 commit 056bc49
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 8 deletions.
2 changes: 2 additions & 0 deletions Helper/Api/Magento/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Magento\Framework\App\Helper\Context;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Sales\Api\Data\OrderItemExtensionFactory;
use Magento\Sales\Api\Data\OrderItemExtensionInterface;
use Magento\Sales\Api\Data\OrderItemInterface;
use Magento\Sales\Model\Order\Item;

Expand All @@ -36,6 +37,7 @@ class Data extends AbstractHelper
public const WARRANTY_TERM = 'warranty_term';
public const LEAD_TOKEN = 'lead_token';
public const PLAN_TYPE = 'plan_type';
public const PARENT_ORDER_ID = 'parent_order_id';

/**
* Order Extension Attributes Factory
Expand Down
2 changes: 1 addition & 1 deletion Model/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public function cancel($order)
$extendOrder = false;
}


if ($extendOrder){
$this->getOrderRequest($order->getStoreId())
->cancel($extendOrder->getExtendOrderId());
Expand Down
38 changes: 36 additions & 2 deletions Model/Product/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Extend\Warranty\Helper\Data;
use Magento\Framework\Exception\LocalizedException;
use \Magento\Framework\Exception\NoSuchEntityException;
use \Magento\Sales\Model\ResourceModel\Order\Item\CollectionFactory as OrderItemCollectionFactory;

/**
* Class Type
Expand All @@ -40,14 +41,16 @@ class Type extends AbstractType
public const LEAD_TOKEN = 'lead_token';
public const BUY_REQUEST = 'info_buyRequest';
public const SECONDARY_SKU = 'secondary_sku';
public const ASSOCIATED_PARENT_ORDER_ID = 'parent_order_id';

/**
* Custom option labels
*/
public const ASSOCIATED_PRODUCT_LABEL = 'SKU';

public const ASSOCIATED_PRODUCT_NAME_LABEL = 'Name';
public const TERM_LABEL = 'Term';
public const PARENT_ORDER_LABEL = 'Parent Order';


/**
* Warranty Helper
Expand All @@ -57,9 +60,16 @@ class Type extends AbstractType
protected $helper;

/**
* Type constructor.
* OrderItemCollectionFactory
*
* @var Magento\Sales\Model\ResourceModel\Order\Item\CollectionFactory
*/
protected $orderItemCollectionFactory;

/**
* Type constructor.
* @param Product\Option $catalogProductOption
* @param \Magento\Sales\Model\ResourceModel\Order\Item\CollectionFactory $orderItemCollectionFactory
* @param \Magento\Eav\Model\Config $eavConfig
* @param Product\Type $catalogProductType
* @param \Magento\Framework\Event\ManagerInterface $eventManager
Expand All @@ -72,7 +82,9 @@ class Type extends AbstractType
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
*/
public function __construct(

\Magento\Catalog\Model\Product\Option $catalogProductOption,
\Magento\Sales\Model\ResourceModel\Order\Item\CollectionFactory $orderItemCollectionFactory,
\Magento\Eav\Model\Config $eavConfig,
\Magento\Catalog\Model\Product\Type $catalogProductType,
\Magento\Framework\Event\ManagerInterface $eventManager,
Expand All @@ -86,6 +98,7 @@ public function __construct(
)
{
$this->helper = $helper;
$this->orderItemCollectionFactory = $orderItemCollectionFactory;
parent::__construct(
$catalogProductOption,
$eavConfig,
Expand Down Expand Up @@ -170,6 +183,23 @@ protected function _prepareProduct(\Magento\Framework\DataObject $buyRequest, $p

if ($buyRequest->hasData('leadToken')) {
$product->addCustomOption(self::LEAD_TOKEN, $buyRequest->getData('leadToken'));

// Add parent order only if type = warranty and leadToken is set
// Find an existing sales_order_item record with lead_token matching and type is not warranty

$orderItemCollection = $this->orderItemCollectionFactory->create();
$orderItemCollection->addFieldToFilter('product_type', ['neq' => 'warranty']);
$orderItemCollection->addFieldToFilter('lead_token', ['like' =>'%'.$buyRequest->getData('leadToken').'%'] );
$existingOrderItem = $orderItemCollection->getFirstItem();
if ($existingOrderItem && $existingOrderItem->getId()) {
$parentOrderId = $existingOrderItem->getOrderId();
} else {
$parentOrderId = null;
}

if ($parentOrderId){
$product->addCustomOption(self::ASSOCIATED_PARENT_ORDER_ID, $parentOrderId);
}
}

if ($this->_isStrictProcessMode($processMode)) {
Expand Down Expand Up @@ -214,6 +244,10 @@ public function getOrderOptions($product)
$options[self::LEAD_TOKEN] = $leadToken->getValue();
}

if ($parentOrder = $product->getCustomOption(self::ASSOCIATED_PARENT_ORDER_ID)) {
$options[self::ASSOCIATED_PARENT_ORDER_ID] = $parentOrder->getValue();
}

return $options;
}
}
24 changes: 23 additions & 1 deletion Observer/CreateOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Magento\Sales\Api\Data\OrderItemInterface;
use Exception;
use Extend\Warranty\Model\CreateContract as WarrantyContractCreate;
use \Magento\Sales\Model\ResourceModel\Order\Item\CollectionFactory as OrderItemCollectionFactory;

/**
* Class CreateLead
Expand Down Expand Up @@ -67,27 +68,38 @@ class CreateOrder implements ObserverInterface
*/
private $warrantyContractCreate;

/**
* OrderItemCollectionFactory
*
* @var Magento\Sales\Model\ResourceModel\Order\Item\CollectionFactory
*/
protected $orderItemCollectionFactory;

/**
* CreateLead constructor
*
* @param OrderItemRepositoryInterface $orderItemRepository
* @param ExtendOrder $extendOrder
* @param DataHelper $dataHelper
* @param LoggerInterface $logger
* @param \Magento\Sales\Model\ResourceModel\Order\Item\CollectionFactory $orderItemCollectionFactory
*/
public function __construct(
OrderItemRepositoryInterface $orderItemRepository,
ExtendOrder $extendOrder,
DataHelper $dataHelper,
LoggerInterface $logger,
WarrantyContractCreate $warrantyContractCreate
WarrantyContractCreate $warrantyContractCreate,
\Magento\Sales\Model\ResourceModel\Order\Item\CollectionFactory $orderItemCollectionFactory
)
{
$this->orderItemRepository = $orderItemRepository;
$this->extendOrder = $extendOrder;
$this->dataHelper = $dataHelper;
$this->logger = $logger;
$this->warrantyContractCreate = $warrantyContractCreate;
$this->orderItemCollectionFactory = $orderItemCollectionFactory;

}

/**
Expand Down Expand Up @@ -157,6 +169,16 @@ private function processBuyRequestLeadToken(OrderItemInterface $warrantyItem)
$leadToken[] = $warrantyItem->getProductOptionByCode('info_buyRequest')['leadToken'];
if (!empty($leadToken)) {
$warrantyItem->setLeadToken(json_encode($leadToken));

// Set parent order id by finding an existing sales_order_item record with same lead_token and type not warranty
$orderItemCollectionCreateOrder = $this->orderItemCollectionFactory->create();
$orderItemCollectionCreateOrder->addFieldToFilter('product_type', ['neq' => 'warranty']);
$orderItemCollectionCreateOrder->addFieldToFilter('lead_token', ['like' => '%'.str_replace('"', "", $leadToken[0]).'%' ] );
$existingOrderItem = $orderItemCollectionCreateOrder->getFirstItem();
if ($existingOrderItem && $existingOrderItem->getId()) {
$parentOrderId = $existingOrderItem->getOrderId();
$warrantyItem->setExtendParentOrderId($parentOrderId);
}
}
}
} catch (Exception $exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Magento\Catalog\Helper\Product\Configuration;
use Magento\Catalog\Model\Product\Configuration\Item\ItemInterface;
use Extend\Warranty\Model\Product\Type;
use Magento\Sales\Api\OrderRepositoryInterface;

/**
* Class GetCustomOptionsPlugin
Expand All @@ -29,6 +30,23 @@ class GetCustomOptionsPlugin
* @param ItemInterface $item
* @return array
*/

/**
* @var OrderRepositoryInterface
*/
protected $orderRepository;

/**
* Constructor
*
* @param OrderRepositoryInterface $orderRepository
*/
public function __construct(OrderRepositoryInterface $orderRepository)
{
$this->orderRepository = $orderRepository;
}


public function afterGetCustomOptions(Configuration $subject, array $result, ItemInterface $item): array
{
$product = $item->getProduct();
Expand Down Expand Up @@ -64,6 +82,19 @@ public function afterGetCustomOptions(Configuration $subject, array $result, Ite
];
}

//Custom option parent order ID (this is displayed in the cart, so use Increment ID)
$parentOrderIdOption = $product->getCustomOption(Type::ASSOCIATED_PARENT_ORDER_ID);
if ($parentOrderIdOption && $parentOrderIdOption->getValue()) {
$parentOrderId = (int) $parentOrderIdOption->getValue();
$order = $this->orderRepository->get($parentOrderId);
$incrementId = $order->getIncrementId();
$parentOrderLabel = Type::PARENT_ORDER_LABEL;
$customOptions[] = [
'label' => __($parentOrderLabel),
'value' => $incrementId,
];
}

$result = array_merge($result, $customOptions);
}

Expand Down
23 changes: 21 additions & 2 deletions ViewModel/Warranty.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use Magento\Sales\Model\Order\Item;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Sales\Api\OrderRepositoryInterface;

/**
* Class Warranty
Expand Down Expand Up @@ -128,6 +129,11 @@ class Warranty implements ArgumentInterface

protected $helper;

/**
* @var OrderRepositoryInterface
*/
protected $orderRepository;

/**
* Warranty constructor
*
Expand All @@ -144,6 +150,7 @@ class Warranty implements ArgumentInterface
* @param AdminSession $adminSession
* @param LeadInfo $leadInfo
* @param WarrantyRelation $warrantyRelation
* @param OrderRepositoryInterface $orderRepository
*/
public function __construct(
DataHelper $dataHelper,
Expand All @@ -159,7 +166,8 @@ public function __construct(
AdminSession $adminSession,
LeadInfo $leadInfo,
WarrantyRelation $warrantyRelation,
ExtendHelper $helper
ExtendHelper $helper,
OrderRepositoryInterface $orderRepository
)
{
$this->dataHelper = $dataHelper;
Expand All @@ -176,6 +184,7 @@ public function __construct(
$this->adminSession = $adminSession;
$this->leadInfo = $leadInfo;
$this->warrantyRelation = $warrantyRelation;
$this->orderRepository = $orderRepository;
}

/**
Expand Down Expand Up @@ -399,7 +408,7 @@ public function itemHasLeadWarrantyInQuote($orderItem): bool
$relationSku = $this->warrantyRelation->getOfferOrderItemSku($orderItem);
return !empty($this->warrantyRelation->getWarrantyByRelationSku($relationSku));
}

/**
* Check does quote have warranty item for the item
* Kept for backwards compatibility with Hyva module
Expand Down Expand Up @@ -656,4 +665,14 @@ public function getProductInfo($product)
: \Extend\Warranty\Model\Api\Request\ProductDataBuilder::NO_CATEGORY_DEFAULT_VALUE
];
}

public function getOrderIncrementId($orderId = null){
if ($orderId){
$order = $this->orderRepository->get($orderId);
$incrementId = $order->getIncrementId();
if ($incrementId){
return $incrementId;
}
}
}
}
1 change: 1 addition & 0 deletions etc/db_schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<table name="sales_order_item">
<column xsi:type="mediumtext" name="contract_id" nullable="true" comment="Extend Contract Id"/>
<column xsi:type="mediumtext" name="lead_token" nullable="true" comment="Extend Lead Token"/>
<column xsi:type="int" name="extend_parent_order_id" nullable="true" unsigned="true" comment="Extend Parent Order ID"/>
</table>
<table name="extend_warranty_contract_create">
<column xsi:type="int" name="id" identity="true" nullable="false" unsigned="true" comment="ID"/>
Expand Down
3 changes: 2 additions & 1 deletion etc/extension_attributes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Extend Warranty
*
* @author Extend Magento Team <magento@guidance.com>
* @author Extend Magento Team <magento@extend.com>
* @category Extend
* @package Warranty
* @copyright Copyright (c) 2021 Extend Inc. (https://www.extend.com/)
Expand All @@ -21,6 +21,7 @@
<attribute code="refund" type="boolean" />
<attribute code="lead_token" type="string" />
<attribute code="plan_type" type="string" />
<attribute code="parent_order_id" type="string" />
</extension_attributes>
<extension_attributes for="Magento\Quote\Api\Data\CartItemInterface">
<attribute code="lead_token" type="string" />
Expand Down
11 changes: 10 additions & 1 deletion view/adminhtml/templates/items/column/name.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ use Magento\Framework\View\Helper\SecureHtmlRenderer;

$_refunded = (isset($_productOptions["refund"]) && true == $_productOptions["refund"])
? "-- All refunded --" : '';
;

//retrieve Parent Order ID if present
$currentOrderId = $_item->getOrderId();
$parentOrderId = isset($_productOptions["parent_order_id"]) ? $_productOptions["parent_order_id"] : null;
$incrementId = $parentOrderId ? $viewModel->getOrderIncrementId($parentOrderId) : null;
?>
<div class="product-warranty-block">
<br />
Expand All @@ -48,6 +52,11 @@ use Magento\Framework\View\Helper\SecureHtmlRenderer;
<?= $block->escapeHtml($_refunded); ?>
<?php endif; ?>
<br /><span><?= /* @noEscape */ $block->escapeHtml(__('Plan ID'))?>: </span><?= /* @noEscape */ $_planId ?>

<!-- show Parent Order ID for Post Purchase Warranty -->
<?php if ($parentOrderId && ($parentOrderId <> $currentOrderId) && $incrementId) :?>
<br /><br/><span><?= /* @noEscape */ $block->escapeHtml(__('Parent Order ID'))?>: </span><?= /* @noEscape */ $incrementId ?>
<?php endif; ?>
</div>
<?php endif; ?>

Expand Down

0 comments on commit 056bc49

Please sign in to comment.