Commit 1ae12f3e by lmf

升级购物车优惠规则

增加谷歌营销代码
parents c83db5e9 057a55f5
......@@ -7,6 +7,22 @@
// phpcs:disable Magento2.Templates.ThisInTemplate
/** @var $block \Magento\Checkout\Block\Cart\Grid */
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$_cart = $objectManager->get('\Magento\Checkout\Model\Cart');
$items = [];
$priceSum = 0;
$_items = $block->getItems();
if (count($_items) > 0) {
foreach ($_items as $key => $value) {
$_item['google_business_vertical'] = 'retail';
$_item['id'] = $value->getSku();
$items[] = $_item;
$priceSum += $value->getPrice() * $value->getQty();
}
}
?>
<?php $mergedCells = ($this->helper(Magento\Tax\Helper\Data::class)->displayCartBothPrices() ? 2 : 1); ?>
<?= $block->getChildHtml('form_before') ?>
......@@ -106,5 +122,13 @@ require(['jquery'], function ($) {
});
});
</script>
<script>
dataLayer = [];
dataLayer.push({
'event': 'add_to_cart',
'value': <?= $priceSum ?>,
'items': <?= json_encode($items, true) ?>
);
</script>
<?= $block->getChildHtml('checkout.cart.order.actions') ?>
<?= $block->getChildHtml('shopping.cart.table.after') ?>
......@@ -7,47 +7,99 @@ declare(strict_types=1);
namespace Magento\SalesRule\Model\Rule\Action\Discount;
use Magento\Quote\Model\Quote\Item\AbstractItem;
use Magento\SalesRule\Model\Rule;
use Magento\SalesRule\Model\Rule\Action\Discount\AbstractDiscount;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\SalesRule\Model\DeltaPriceRound;
use Magento\SalesRule\Model\Rule\Action\Discount\DataFactory;
use Magento\SalesRule\Model\Validator;
class BuyXGetY extends AbstractDiscount
{
/**
* Calculate discount data for BuyXGetY action.
*
* @param Rule $rule
* @param AbstractItem $item
* @var DeltaPriceRound
*/
private $deltaPriceRound;
private static $discountType = 'BuyXGetY';
/**
* @param Validator $validator
* @param DataFactory $discountDataFactory
* @param PriceCurrencyInterface $priceCurrency
* @param DeltaPriceRound $deltaPriceRound
*/
public function __construct(
Validator $validator,
DataFactory $discountDataFactory,
PriceCurrencyInterface $priceCurrency,
DeltaPriceRound $deltaPriceRound = null
) {
$this->deltaPriceRound = $deltaPriceRound ?: ObjectManager::getInstance()->get(DeltaPriceRound::class);
parent::__construct($validator, $discountDataFactory, $priceCurrency);
}
/**
* @param \Magento\SalesRule\Model\Rule $rule
* @param \Magento\Quote\Model\Quote\Item\AbstractItem $item
* @param float $qty
* @return Data
* @return \Magento\SalesRule\Model\Rule\Action\Discount\Data
*/
public function calculate($rule, $item, $qty): Data
public function calculate($rule, $item, $qty)
{
/** @var \Magento\SalesRule\Model\Rule\Action\Discount\Data $discountData */
$discountData = $this->discountFactory->create();
$ruleId = $rule->getId();
$ruleTotals = $this->validator->getRuleItemTotalsInfo($ruleId);
$quote = $item->getQuote();
$address = $item->getAddress();
$itemPrice = $this->validator->getItemPrice($item);
$baseItemPrice = $this->validator->getItemBasePrice($item);
$itemOriginalPrice = $this->validator->getItemOriginalPrice($item);
$baseItemOriginalPrice = $this->validator->getItemBaseOriginalPrice($item);
$x = $rule->getDiscountStep();
$y = $rule->getDiscountAmount();
if (!$x || $y > $x) {
$cartRules = $address->getBuyXGetYRules();
if (!isset($cartRules[$ruleId])) {
return $discountData;
}
$buyAndDiscountQty = $x + $y;
$availableDiscountAmount = (float)$cartRules[$ruleId]['amount'];
$ruldDiscountAmount = (float)$cartRules[$ruleId]['discount_amount'];
$discountType = self::$discountType . $ruleId;
if ($availableDiscountAmount > 0) {
$store = $quote->getStore();
if ($ruleTotals['items_count'] <= 1) {
$quoteAmount = $this->priceCurrency->convert($availableDiscountAmount, $store);
$baseDiscountAmount = min($baseItemPrice * $qty, $availableDiscountAmount);
$this->deltaPriceRound->reset($discountType);
} else {
$ratio = $baseItemPrice * $qty / $ruleTotals['base_items_price'];
$maximumItemDiscount = $this->deltaPriceRound->round(
$ruldDiscountAmount * $ratio,
$discountType
);
$quoteAmount = $this->priceCurrency->convert($maximumItemDiscount, $store);
$baseDiscountAmount = min($baseItemPrice * $qty, $maximumItemDiscount);
$this->validator->decrementRuleItemTotalsCount($ruleId);
}
$fullRuleQtyPeriod = floor($qty / $buyAndDiscountQty);
$freeQty = $qty - $fullRuleQtyPeriod * $buyAndDiscountQty;
$baseDiscountAmount = $this->priceCurrency->round($baseDiscountAmount);
$discountQty = $fullRuleQtyPeriod * $y;
if ($freeQty > $x) {
$discountQty += $freeQty - $x;
$availableDiscountAmount -= $baseDiscountAmount;
$cartRules[$ruleId]['amount'] = $availableDiscountAmount;
if ($availableDiscountAmount <= 0) {
$this->deltaPriceRound->reset($discountType);
}
$discountData->setAmount($discountQty * $itemPrice);
$discountData->setBaseAmount($discountQty * $baseItemPrice);
$discountData->setOriginalAmount($discountQty * $itemOriginalPrice);
$discountData->setBaseOriginalAmount($discountQty * $baseItemOriginalPrice);
$discountData->setAmount($this->priceCurrency->round(min($itemPrice * $qty, $quoteAmount)));
$discountData->setBaseAmount($baseDiscountAmount);
$discountData->setOriginalAmount(min($itemOriginalPrice * $qty, $quoteAmount));
$discountData->setBaseOriginalAmount($this->priceCurrency->round($baseItemOriginalPrice));
}
$address->setBuyXGetYRules($cartRules);
return $discountData;
}
......
......@@ -432,7 +432,7 @@ class Validator extends \Magento\Framework\Model\AbstractModel
* @throws \Zend_Validate_Exception
* @throws \Zend_Db_Select_Exception
*/
public function initTotals($items, Address $address)
public function initTotals_bak($items, Address $address)
{
$address->setCartFixedRules([]);
......@@ -470,6 +470,97 @@ class Validator extends \Magento\Framework\Model\AbstractModel
return $this;
}
public function initTotals($items, Address $address)
{
$address->setCartFixedRules([]);
$address->setBuyXGetYRules([]);
$address->setBuyXPayYRules([]);
if (!$items) {
return $this;
}
/** @var \Magento\SalesRule\Model\Rule $rule */
foreach ($this->_getRules($address) as $rule) {
$ruleId = $rule->getId();
$simpleAction = $rule->getSimpleAction();
if (in_array($simpleAction, [\Magento\SalesRule\Model\Rule::CART_FIXED_ACTION, \Magento\SalesRule\Model\Rule::BUY_X_GET_Y_ACTION])
&& $this->validatorUtility->canProcessRule($rule, $address)
) {
$ruleTotalItemsPrice = 0;
$ruleTotalBaseItemsPrice = 0;
$validItemsCount = 0;
$totalQty = 0;
$buyXGetYItems = [];
//$buyXPayYItems = [];
foreach ($items as $item) {
//Skipping child items to avoid double calculations
if ($item->getParentItemId()) {
continue;
}
if (!$rule->getActions()->validate($item)) {
continue;
}
if (!$this->canApplyDiscount($item)) {
continue;
}
if ($simpleAction == \Magento\SalesRule\Model\Rule::CART_FIXED_ACTION) {
$qty = $this->validatorUtility->getItemQty($item, $rule);
} else {
$qty = $item->getTotalQty();
}
$totalQty += $qty;
$basePrice = $this->getItemBasePrice($item);
$ruleTotalItemsPrice += $this->getItemPrice($item) * $qty;
$ruleTotalBaseItemsPrice += $basePrice * $qty;
$validItemsCount++;
if ($simpleAction == \Magento\SalesRule\Model\Rule::BUY_X_GET_Y_ACTION) {
$buyXGetYItems[] = ['price' => $basePrice, 'qty' => $qty];
}
// if ($simpleAction == self::BUY_X_PAY_Y_ACTION) {
// $buyXPayYItems[] = ['price' => $basePrice, 'qty' => $qty];
// }
}
if (!empty($buyXGetYItems)) {
$x = $rule->getDiscountStep();
$y = $rule->getDiscountAmount();
$buy = ceil($x + $y);
$discountQty = $rule->getDiscountQty();
$maxQty = floor($totalQty / $buy) * $y;
if ($discountQty > 0) {
$maxQty = min($maxQty, $discountQty);
}
$sortCol = array_column($buyXGetYItems, 'price');
array_multisort($sortCol, SORT_ASC, $buyXGetYItems);
$amount = 0;
foreach ($buyXGetYItems as $item) {
if ($item['qty'] > $maxQty) {
$amount += $item['price'] * $maxQty;
$maxQty = 0;
} else {
$amount += $item['price'] * $item['qty'];
$maxQty -= $item['qty'];
}
if ($maxQty <= 0) {
break;
}
}
$address->setBuyXGetYRules([$ruleId => ['amount' => $amount, 'discount_amount' => $amount]]);
}
$this->_rulesItemTotals[$ruleId] = [
'items_price' => $ruleTotalItemsPrice,
'base_items_price' => $ruleTotalBaseItemsPrice,
'items_count' => $validItemsCount,
];
}
}
return $this;
}
/**
* Determine if quote item is valid for a given sales rule
......@@ -625,10 +716,12 @@ class Validator extends \Magento\Framework\Model\AbstractModel
if (!empty($itemsSorted)) {
$items = array_merge($itemsSorted, $items);
}
usort($items, array('self', 'sortByPrice'));
return $items;
}
static function sortByPrice($a,$b){
return ($a->getPrice() < $b->getPrice()) ? -1 : 1;
}
/**
* Rule total items getter.
*
......
......@@ -43,6 +43,18 @@ $_helper = $block->getData('outputHelper');
* Position for actions regarding image size changing in vde if needed
*/
$pos = $block->getPositioned();
$price = 0;
$items = [];
if ($_productCollection->getSize() > 0) {
$price = array_sum($_productCollection->getColumnValues('price'));
foreach ($_productCollection as $key => $product) {
$_product['id'] = $product->getSku();
$_product['google_business_vertical'] = 'retail';
$items[] = $_product;
}
}
$googleGtmName = is_null($pos) ? 'view_item_list' : 'view_search_results';
?>
<style>
.mode-list{
......@@ -199,4 +211,12 @@ $_helper = $block->getData('outputHelper');
}
}
</script>
<script>
dataLayer = [];
dataLayer.push({
'event' : <?= $googleGtmName ?>,
'value' : <?= $price ?>,
'item' : <?= json_encode($items, true) ?>
})
</script>
<?php endif; ?>
......@@ -3,6 +3,9 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');
/** @var \Magento\Catalog\Block\Product\View\Details $block */
?>
......@@ -48,5 +51,16 @@
$("#tab-label-reviews").trigger("click");
},8000);
});
window.dataLayer = window.dataLayer || [];
dataLayer.push({
'event': 'view_item',
'value': '<?= $product->getFinalPrice() ?>',
'items': [{
'id': <?= $product->getSku() ?>,
'google_business_vertical': 'retail'}]
});
</script>
<?php /** @var $block \Magento\Checkout\Block\Onepage\Success */ ?>
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
$lid = $block->getOrderId();
$totalPrice = 0;
$googleItems = [];
if ($lid) {
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId($lid);
if ($order) {
$totalPrice = $order->getGrandTotal();
$_itemsTmp = $order->getAllItems();
foreach ($_itemsTmp as $value) {
$_item['google_business_vertical'] = 'retail';
$_item['id'] = $value->getSku();
$googleItems[] = $_item;
}
}
}
?>
<?php /** @var $block \Magento\Checkout\Block\Onepage\Success */ ?>
<div class="checkout-success">
<?php if ($block->getOrderId()) :?>
<?php if ($block->getCanViewOrder()) :?>
......@@ -48,3 +64,11 @@
</style>
<script>
dataLayer = [];
dataLayer.push({
'event': 'purchase',
'value': <?= $totalPrice ?>,
'items': <?= json_encode($googleItems, true) ?>
});
</script>
......@@ -653,6 +653,61 @@ p.shopbycate-title {
}
}
.categories_tab {
clear: both;
margin: 0px 0 ;
padding: 50px;
width: 100%;
height: 400px;
}
.section-element.shop_by_categories .container {
padding-bottom: 15px;
}
.shop-by-cata {
padding: 20px;
width: 25%;
float: left;
}
.shopcatewpt {
width: 80%;
margin: 0 auto;
text-align:center;
height: 200px;
}
p.shopbycate-title {
background: #fff;
text-align: center;
font-size: 28px;
font-weight: bold;
color: #000;
text-transform: uppercase;
margin-bottom: 20px;
padding-bottom: 20px;
font-family: 'Outfit', sans-serif;
}
@media(max-width:789px){
.shop-by-cata {
padding:2px;
width: 50%;
float: left;
}
p.shopbycate-title{
font-size:20px;
margin-top: -20px;
}
.shopcatewpt {
width: 130%;
margin: 0 auto;
margin-top: -20px;
margin-left: -40px;
}
.shop-by-cata img {
height: 54px;
}
}
>>>>>>> .theirs
.top-content-home{
width: 100%;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment