Skip to content

Commit

Permalink
Merge pull request #3 from helloextend/v1.1.2
Browse files Browse the repository at this point in the history
simplification and optimizations
  • Loading branch information
jm-extend authored Jun 12, 2024
2 parents 568a2fd + f96a95c commit 6b51b8b
Show file tree
Hide file tree
Showing 10 changed files with 729 additions and 728 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
/** @var Magento\Catalog\Block\Product\View $block */

$stringUtils = $escaper->escapeJs($block->getViewFileUrl('Extend_HyvaIntegration::js/utils/string-utils.js'));
$cartUtils = $escaper->escapeJs($block->getViewFileUrl('Extend_HyvaIntegration::js/utils/cart-utils.js'));

/** @var \Magento\Quote\Model\Quote\Item $_item */
$item = $block->getItem();
Expand All @@ -16,10 +15,16 @@
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $item->getQuote();
$productType = $product->getTypeId();
$categoryName = '';

if ($category = $product->getCategory()) {
$categoryName = $category->getName();
// note that you need at minimum helloextend/integration:v3.10.0
// to be able to capture category at the cart level

$categoryModel = $block->getData('categoryModel');
$categoryName = '';
$categories = $product->getCategoryIds();

if (count($categories) > 0) {
$categoryName = $categoryModel->getCategoryNameById($categories[0]);
}

$viewModel = $block->getData('viewModel');
Expand All @@ -34,148 +39,143 @@
/** sanitizing to prevent whitespace and other abnormal characters from being used in a div id */
$sanitizedProductSku = preg_replace('/[^a-zA-Z0-9_|]/', '', $productSku);


?>

<?php if ($shouldRender) : ?>
<div>
<div>
<div class="product-protection-offer"
id="product_protection_offer_<?= $sanitizedProductSku ?>"
x-data="product_protection_offer_<?= $sanitizedProductSku ?>"
@private-content-loaded.window="initProductWarrantyOffers(event)">
id="product_protection_offer_<?= $sanitizedProductSku ?>">
</div>
</div>
</div>

<script>
(async() => {
let cart_utils = await import('<?= $cartUtils ?>');
cart_utils = cart_utils.default;
window.addEventListener(
'private-content-loaded', (event) => {
initProductWarrantyOffers(event);
});

let stringUtils = await import('<?= $stringUtils ?>');
stringUtils = stringUtils.default;
async function initProductWarrantyOffers(event) {

window.product_protection_offer_<?= $sanitizedProductSku ?> = function() {
return {
customerData: {},
const config = [{
extendStoreUuid: "<?= $block->getData('viewModel')->getExtendStoreUuid() ?>",
activeEnvironment: "<?= $block->getData('viewModel')->getActiveEnvironment() ?>",
selectedProductSku: "<?= $productSku; ?>",
selectedProductPrice: "<?= $product->getPrice(); ?>",
productCategory: "<?= $categoryName; ?>",
currencyCode: "<?= $currencyCode ?>",
selectedConfigurableProduct: {}
}]

initProductWarrantyOffers(event) {
const customerData = event.detail.data;

const config = [{
extendStoreUuid: "<?= $block->getData('viewModel')->getExtendStoreUuid() ?>",
activeEnvironment: "<?= $block->getData('viewModel')->getActiveEnvironment() ?>",
selectedProductSku: "<?= $productSku; ?>",
selectedProductPrice: "<?= $product->getPrice(); ?>",
productCategory: "<?= $categoryName; ?>",
currencyCode: "<?= $currencyCode ?>",
selectedConfigurableProduct: {}
}]
let stringUtils = await import('<?= $stringUtils ?>');
stringUtils = stringUtils.default;

this.customerData = event.detail.data;
const cartUtils = cart_utils(this.customerData);

if (window.Extend && window.ExtendMagento){
productProtectionOffer(config, customerData, stringUtils)

if (window.Extend && window.ExtendMagento){
this.productProtectionOffer(config, cartUtils)
}else {
window.addEventListener(
'extendLoaded', () => {
productProtectionOffer(config, customerData, stringUtils)
});
}
}

}
window.addEventListener(
'load', () => {
this.productProtectionOffer(config, cartUtils)
});
},

productProtectionOffer(config, cartUtils) {
'use strict';

function getProductQuantity(cartItems, product) {
let quantity = 1

for (let i = 0; i < cartItems.length; i++) {
const cartItem = cartItems[i];
if (cartItem.sku === product.id) {
quantity = cartItem.qty;
break; // Exit the loop once a matching item is found
}
}
return quantity
}
function productProtectionOffer(config, customerData, stringUtils ) {
'use strict';
const cartUtils = window.cart_utils(customerData);

function getProductQuantity(cartItems, product) {
let quantity = 1

function handleAddToCartClick(opts) {
const { plan, product, quantity } = opts

if (plan && product) {
const { planId, price, term, title, coverageType, offerId } = plan
const { id: productId, price: listPrice } = product

const planToUpsert = {
planId,
price,
term,
title,
coverageType,
}
const cartItems = cartUtils
.getCartItems()
.map(cartUtils.mapToExtendCartItem)

ExtendMagento.upsertProductProtection({
plan: planToUpsert,
cartItems,
productId,
listPrice,
offerId,
quantity: quantity ?? getProductQuantity(cartItems, product),
}).then(function () {
cartUtils.refreshMiniCart()
window.location.reload()
})
}
for (let i = 0; i < cartItems.length; i++) {
const cartItem = cartItems[i];
if (cartItem.sku === product.id) {
quantity = cartItem.qty;
break; // Exit the loop once a matching item is found
}
}
return quantity
}

function renderSimpleOffer(cartItems, config) {
const sku = config[0].selectedProductSku
const isWarrantyInCart = ExtendMagento.warrantyInCart({
lineItemSku: sku,
lineItems: cartItems,
})

if (
sku === 'extend-protection-plan' ||
sku === 'xtd-pp-pln' ||
isWarrantyInCart
) {
return
}

const activeProductData = {
referenceId: config[0].selectedProductSku,
price: config[0].selectedProductPrice * 100,
onAddToCart: handleAddToCartClick,
}

Extend.buttons.renderSimpleOffer(
'#product_protection_offer_' +
stringUtils.sanitizeForElementId(config[0].selectedProductSku),
activeProductData,
)
}
function handleAddToCartClick(opts) {
const { plan, product, quantity } = opts

return function() {
const cartData = cartUtils.getCartData()
if (plan && product) {
const { planId, price, term, title, coverageType, offerId } = plan
const { id: productId, price: listPrice } = product

let cartItems = cartUtils.getCartItems()
const planToUpsert = {
planId,
price,
term,
title,
coverageType,
}
const cartItems = cartUtils
.getCartItems()
.map(cartUtils.mapToExtendCartItem)

ExtendMagento.upsertProductProtection({
plan: planToUpsert,
cartItems,
productId,
listPrice,
offerId,
quantity: quantity ?? getProductQuantity(cartItems, product),
}).then(function () {
cartUtils.refreshMiniCart()
window.location.reload()
})
}
}

if (cartItems.length > 0) {
renderSimpleOffer(cartItems, config)
}
function renderSimpleOffer(cartItems, config) {
const sku = config[0].selectedProductSku
const isWarrantyInCart = ExtendMagento.warrantyInCart({
lineItemSku: sku,
lineItems: cartItems,
})

if (
sku === 'extend-protection-plan' ||
sku === 'xtd-pp-pln' ||
isWarrantyInCart
) {
return
}

cartItems = cartData.items
renderSimpleOffer(cartItems, config)
}.bind(window.Extend, window.ExtendMagento)();
const activeProductData = {
referenceId: config[0].selectedProductSku,
price: config[0].selectedProductPrice * 100,
onAddToCart: handleAddToCartClick,
}
if (!Extend.buttons.instance('#product_protection_offer_' +
stringUtils.sanitizeForElementId(config[0].selectedProductSku))){
window.Extend.buttons.renderSimpleOffer(
'#product_protection_offer_' +
stringUtils.sanitizeForElementId(config[0].selectedProductSku),
activeProductData,
)
}

}
}
})(); //async

return function() {
const cartData = cartUtils.getCartData()
let cartItems = cartUtils.getCartItems()

if (cartItems.length > 0) {
renderSimpleOffer(cartItems, config)
}

cartItems = cartData.items
renderSimpleOffer(cartItems, config)
}.bind(window.Extend, window.ExtendMagento)();
}
</script>

<?php endif; ?>
13 changes: 3 additions & 10 deletions src/view/frontend/templates/cart/minicart-simple-offer.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
/** @var Magento\Catalog\Block\Product\View $block */

$stringUtils = $escaper->escapeJs($block->getViewFileUrl('Extend_HyvaIntegration::js/utils/string-utils.js'));
$cartUtils = $escaper->escapeJs($block->getViewFileUrl('Extend_HyvaIntegration::js/utils/cart-utils.js'));
$viewModel = $block->getData('viewModel');
$extendStoreUuid = $viewModel->getExtendStoreUuid();
$activeEnvironment = $viewModel->getActiveEnvironment();
Expand All @@ -27,18 +26,13 @@

<script>

(async() => {

let cart_utils = await import('<?= $cartUtils ?>');
cart_utils = cart_utils.default;

const minicartSelector = '[data-block="minicart"]'
const productItemSelector = '.items-start'
const itemDetailsSelector = '.price-excluding-tax'
const simpleOfferClass = 'extend-minicart-simple-offer'

window.minicartSimpleOffer = function() {

window.minicartSimpleOffer = function() {
return {
customerData: {},

Expand All @@ -63,7 +57,7 @@

if (toggle && event.detail && !event.detail.isOpen ) return;

const cartUtils = cart_utils(this.customerData);
const cartUtils = window.cart_utils(this.customerData);

function handleUpdate() {
let cartItems = cartUtils.getCartItems();
Expand Down Expand Up @@ -93,7 +87,7 @@

const itemDetailsElem = itemContainerElem.querySelector(itemDetailsSelector)

if (itemDetailsElem) {
if (itemDetailsElem && !Extend.buttons.instance(simpleOfferElem)) {
itemDetailsElem.append(simpleOfferElem)
Extend.buttons.renderSimpleOffer(simpleOfferElem, {
referenceId: cartItem.product_sku,
Expand Down Expand Up @@ -154,7 +148,6 @@
}
}
}
})(); //async
</script>

<?php endif; ?>
Loading

0 comments on commit 6b51b8b

Please sign in to comment.