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.
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; } }
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.