hryvinskyi / magento2-free-gift-frontend
Storefront of the free gift modules: the amount each cart has spent towards its gift rules and the mark of each gift line in the cart customer-data section, the free gift goal of the cart progress bar, and a free gift badge on every gift line of the minicart and the cart page, on any theme built on
Package info
github.com/hryvinskyi/magento2-free-gift-frontend
Type:magento2-module
pkg:composer/hryvinskyi/magento2-free-gift-frontend
Requires
- php: ~8.1.0||~8.2.0||~8.3.0||~8.4.0||~8.5.0
- hryvinskyi/magento2-cart-progress-bar: ^1.0
- hryvinskyi/magento2-free-gift: ^1.1
- hryvinskyi/magento2-free-gift-api: ^1.1
- magento/framework: *
- magento/module-catalog: *
- magento/module-checkout: *
- magento/module-customer: *
- magento/module-directory: *
- magento/module-quote: *
- magento/module-sales-rule: *
- magento/module-store: *
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
The storefront side of the free gift modules: the amount each cart has spent towards its gift rules and the mark of each gift line, in the cart customer-data section; the free gift goal of the cart progress bar; and a free gift badge on every gift line of the minicart and the cart page, on any theme built on Magento Blank or Luma.
What it does for a store
-
The spent amount in the browser. The
cartcustomer-data section carriesfree_gift_spent: the amount the shopper's cart has spent towards each gift rule, keyed by rule id, in the cart's currency, rounded to two decimals. It is read from the state stored with the cart, so the section needs no totals collection and no query of its own. A visitor without a saved cart gets nofree_gift_spent.{ "free_gift_spent": { "189": 50 } } -
The free gift goal. In a store view where the goal is enabled, the module contributes one goal to the bar of
hryvinskyi/magento2-cart-progress-bar:Part of the goal Value code free_gift_<rule id>amount the chosen rule's Discount Qty Step, converted to the store's current currency amount path free_gift_spent.<rule id>in thecartsectionwording, link, priority the store view's settings below image the small image of the rule's first gift SKU, resized for the theme's product_small_imagedisplay areaThe goal shows only while the store view gives free gifts at all (General › Free Gifts,
free_gift/general/enabled, read throughGiftsEnabledInterface) and the chosen rule exists, is active, has the action Free gift for every X spent, belongs to the store's website, has a spend step and runs on the store view's local date: on or after its From date and on or before its To date, both days included, an empty date leaving that side open, as the cart applies the rule. Otherwise the store has no free gift goal, and the bar never promises a gift the store cannot give. A rule without gift SKUs, a gift missing from the catalog and a gift without a small image give a goal without an image, never the catalog's placeholder. -
Full-page cache safe. Everything printed into a page is the same for every visitor of the store; the shopper's own amount arrives with the customer-data section. The gift image is resolved once per page-cache miss.
-
Cached pages follow the rule. Deleting a cart price rule whose action is Free gift for every X spent, or saving one with a change the bar shows, cleans every cached page that holds the bar, through the bar's
BarCacheInterface::clean(); a rule that had that action before the save counts too. The changes the bar shows are the action, the active flag, the spend per step, the From and To dates, the websites and the gift SKUs (a gift rule saved from the rule form carries its SKUs, so that save always cleans). A new step, new dates, a deactivated rule or a new gift SKU therefore show on the next page view. A save that changes nothing else, such as the one the store makes on every order to count the rule's uses, cleans nothing, and neither does a change to any other rule. -
Gift lines in the minicart. Every line of the
cartcustomer-data section carriesis_free_gift:truefor a line a gift rule put in the cart,falsefor a line the shopper bought.{ "items": [ { "product_name": "Flexible Lamp", "is_free_gift": true } ] }The minicart shows a Free gift badge in the price area of a gift line, before the price. The badge is a component of the minicart's
priceSidebararea, so it shows with the platform's minicart item template and with any theme template that keeps that area. -
Gift lines on the cart page. Under the name of a gift line the cart page shows the Free gift badge and the gift line note of the store view (Gift Line Note), escaped. A bought line shows neither. The block is a child of the cart's
additional.product.infoblock, which the platform's cart item template renders for every line. -
A gift line cannot be changed from the storefront. On a gift line the module's styles hide the quantity field and the edit, remove and move-to-wish-list actions, in the minicart and on the cart page. They find the line by its badge with the CSS
:has()selector, so the theme's templates stay as they are.
Requirements
- PHP 8.1 to 8.5.
hryvinskyi/magento2-free-gift-api^1.1,hryvinskyi/magento2-free-gift^1.1 andhryvinskyi/magento2-cart-progress-bar^1.0.- The module reads through the contracts of
hryvinskyi/magento2-free-gift-api; their implementations come fromhryvinskyi/magento2-free-gift, which it requires. - The platform's Framework, Catalog, Checkout, Customer, Directory, Quote, SalesRule and Store modules (what
composer.jsonandetc/module.xmldeclare).
Installation
composer require hryvinskyi/magento2-free-gift-pack bin/magento setup:upgrade bin/magento setup:di:compile bin/magento cache:flush
Configuration
Stores › Configuration › Sales › Free Gift, shown by hryvinskyi/magento2-free-gift-admin-ui. This module reads:
| Path | Label | Default | Scope |
|---|---|---|---|
free_gift/bar/enabled |
Enabled | No | default, website, store view |
free_gift/bar/rule_id |
Rule | none | default, website, store view |
free_gift/bar/priority |
Priority | 10 | default, website, store view |
free_gift/bar/link_url |
Link URL | none | default, website, store view |
free_gift/bar/label_initial |
Initial Message | none | default, website, store view |
free_gift/bar/label_progress |
Progress Message | none | default, website, store view |
free_gift/bar/label_achieved |
Achieved Message | none | default, website, store view |
free_gift/bar/label_terms |
Terms | none | default, website, store view |
free_gift/cart/gift_line_note |
Gift Line Note | none | default, website, store view |
The four messages take the placeholders {{goal}} (the amount to spend, formatted as a price) and {{goal_left}} (the amount still to spend). Where the bar shows is set under Cart Progress Bar; the bar shows only while it is enabled there.
The goal follows the rule's From and To dates when a page is rendered. A page cached while the rule ran keeps showing the goal after the To date until its page-cache lifetime ends or the page cache is refreshed; nothing cleans pages when a date passes. Saving these settings asks the admin to refresh the page cache, as for every setting.
Theming
The badge and the note come with neutral styles in view/frontend/web/css/source/_module.less, compiled into every theme built on Magento Blank or Luma. A theme recolours them through CSS custom properties, without overriding a template:
| Property | What it colours | Default |
|---|---|---|
--free-gift-badge-background |
the badge | #333 |
--free-gift-badge-color |
the badge's text | #fff |
--free-gift-badge-icon-color |
the gift icon in the badge | #f5b400 |
--free-gift-note-color |
the gift line note | the text colour around it |
The markup has these classes:
| Class | Element |
|---|---|
free-gift-badge |
the badge |
free-gift-badge-icon, free-gift-badge-text |
the icon and the text of the badge |
free-gift-line |
the badge and the note on the cart page |
free-gift-line-note |
the note on the cart page |
A theme whose minicart item template places the badge elsewhere renders the module's template there, in the line's context:
<!-- ko template: 'Hryvinskyi_FreeGiftFrontend/minicart/item/gift-badge' --><!-- /ko -->
A theme with a cart item template of its own gets the view model Hryvinskyi\FreeGiftFrontend\ViewModel\CartGiftLine through the layout. isGift($item) tells a gift line from a bought one, and note() gives the gift line note of the current store view, unescaped, or an empty string:
<argument name="gift_line" xsi:type="object">Hryvinskyi\FreeGiftFrontend\ViewModel\CartGiftLine</argument>
Limits
- The badge, the note and the styles are for themes built on Magento Blank or Luma. A theme of another family, such as Hyvä, renders neither the minicart's Knockout areas nor the modules' LESS.
- A theme whose cart item template does not render
additional.product.infoshows no badge and no note on the cart page. - The spent amount in the section is the one stored when the cart's totals were last collected; the section does not recollect them.
- A cart that carries no base to cart currency rate counts in the base currency.
Registrations
| Where | What |
|---|---|
providers of Hryvinskyi\CartProgressBar\Model\Goals (frontend) |
item free_gift, Model\FreeGiftGoalProvider |
plugin on Magento\Checkout\CustomerData\Cart::getSectionData (frontend, after) |
adds free_gift_spent to the cart section |
plugin on Magento\Checkout\CustomerData\ItemPoolInterface::getItemData (frontend, after) |
adds is_free_gift to each line of the cart section |
observer on salesrule_rule_save_after and salesrule_rule_delete_after (global) |
Observer\CleanBarCacheOnGiftRuleChange: cleans the bar's cached pages after a gift rule changes what the bar shows, or is deleted |
imageDisplayArea of Model\GiftImage |
product_small_image |
layout checkout_cart_sidebar_item_renderers |
component free_gift_badge of the minicart's item.renderer, area priceSidebar, template Hryvinskyi_FreeGiftFrontend/minicart/item/gift-badge |
layout checkout_cart_index |
block hryvinskyi.free.gift.cart.line in additional.product.info, template Hryvinskyi_FreeGiftFrontend::cart/item/gift-line.phtml |