CartBonus (optional)
https://github.com/magesuite/cart-bonus
This module creates bonuses visible in the cart The feature is actually a cart price rule which can be triggered under certain conditions. On the storefront the user will notice a progress bar that hints at a potential bonus when meeting a subtotal threshold.
- 1 Installation
- 2 User manual
- 3 Admin settings
- 4 Backend
- 5 Frontend
- 5.1 PHTML template
- 5.2 Styling
Installation
This module is optional
composer require "creativestyle/magesuite-cart-bonus" ^1.0.0
User manual
Admin settings
To add a cart bonus a new sales rule should be created in Marketing -> Promotions -> Cart price rules
The rule must be set as No coupon
In the Cart bonuses
tab, a rule can be cart-bonus
It is also possible to hide the rule label in order to make it a surprise for a user.
Free shipping
rule is set as a cart bonus with a hidden label. The label is visible when a user passes the threshold.
Backend
Template
app/design/frontend/creativestyle/theme-creativeshop/MageSuite_CartBonus/templates/status.phtml
<?php /** @var $block \Creativestyle\CartBonusExtension\Block\Status */
$status = $block->getBonusesStatus();
$progressToNextBonusPercentage = $status->getProgressPercentage();
$amountToNextLevel = $status->getRemainingAmountForNextBonusWithCurrency();
$awardedBonusesCount = $status->getAwardedBonusesCount();
$allBonusesCount = $status->getBonusesCount();
$isFirstNotAwarded = true;
$tooltipLeftLimit = $this->getTooltipLeftLimit();
$tooltipRightLimit = $this->getTooltipRightLimit();
.........
Block
vendor/creativestyle/magesuite-cart-bonus/Block/Status.php:32
class Status extends \Magento\Framework\View\Element\Template
{
public function getBonusesStatus()
{
$totals = $this->cart->getQuote()->getTotals();
$currentCartValue = $totals['subtotal']['value'];
return $this->statusBuilder->build($currentCartValue);
}
}
Service
vendor/creativestyle/magesuite-cart-bonus/Service/StatusBuilder.php:51
class StatusBuilder
{
public function build($cartValue)
{
$status = $this->statusFactory->create();
$rules = $this->getBonusCartRules();
$bonuses = [];
.....
usort($bonuses, function ($bonus1, $bonus2) {
return $bonus1->getMinimumCartValue() <=> $bonus2->getMinimumCartValue();
});
$status->setBonuses($bonuses);
$this->calculateCurrentProgress($status, $cartValue);
return $status;
}
}
Model
vendor/creativestyle/magesuite-cart-bonus/Model/Bonus/Status.php:34
class Status
{
public function getAwardedBonusesCount() {
$awardedBonuses = 0;
foreach($this->bonuses as $bonus) {
if(!$bonus->wasAwarded()) {
continue;
}
$awardedBonuses++;
}
return $awardedBonuses;
}
}
Plugin
vendor/creativestyle/magesuite-shipping-addons/Plugin/Checkout/Block/Cart/Totals/AddFreeShippingBarVariables.php:40
class AddFreeShippingBarVariables
{
public function afterGetJsLayout(\Magento\Checkout\Block\Cart\Totals $subject, $jsLayout)
{
.....
if (!empty($freeShippingBarConfig)) {
$freeShippingBarConfig['freeShippingFrom'] = $this->shippingMethodsHelper->getMinimumFreeShippingAmount();
$freeShippingBarConfig['priceFormat'] = $this->taxHelper->getPriceFormat($this->storeManager->getStore()->getId());
$jsLayout = $this->arrayManager->set(self::FREE_SHIPPING_BAR_CONFIG_PATH, $jsLayout, $freeShippingBarConfig);
}
return json_encode($jsLayout);
}
}
Frontend
Each bonus is displayed as a section of a bar. After cart page is loaded the progress bar is animated.
PHTML template
The PHTML template is placed in theme-creativeshop/src/MageSuite_CartBonus/templates/status.phtml
It is possible to pass some configuration from the layout - theme-creativeshop/src/MageSuite_CartBonus/layout/default.xml
<referenceBlock name="bonus.gift.status">
<arguments>
<argument name="additional_css_classes" xsi:type="string"></argument>
<argument name="headline" xsi:type="string" translate="true">Acquire your gift now!</argument>
</arguments>
</referenceBlock>
Also, paths or configurations for the following icons can be adjusted:
bonus.gift.icon.headline
bonus.gift.icon.gift
bonus.gift.icon.status
Styling
The styling for this component is provided by default in theme-cerativeshop. Styles are placed in components/cart/cart-bonus
file.