Search by

hryvinskyi / magento2-free-gift

hryvinskyi

Free gift for every step of spending: a cart price rule action that adds the rule's gift products to the cart at no charge, stores the spent amount on the cart and keeps the gift lines in line with what the cart earns.

Package info

github.com/hryvinskyi/magento2-free-gift

Type:magento2-module

pkg:composer/hryvinskyi/magento2-free-gift

Statistics

Installs: 3

Dependents: 4

Suggesters: 0

Stars: 0

Open Issues: 0

1.1.0 2026-11-27 09:22 UTC

This package is auto-updated.

Last update: 2026-11-27 09:26:37 UTC


README

Gives a gift for every step of spending: a cart price rule action, Free gift for every X spent, adds the rule's gift products to the cart at no charge as the cart's spent amount grows, and takes them away when it falls. Websites, customer groups, dates, coupons, conditions and priority stay the ones every cart price rule has.

What it does for a store

  • A native cart price rule. A rule with the action free_gift_spent goes through every check the platform makes of a rule, then takes nothing off the lines. Its fields mean:

    Field of the rule Meaning for this action
    Discount Amount gifts per step
    Discount Qty Step (Buy X) amount to spend per step, in the base currency
    Maximum Qty Discount is Applied To the most gifts the rule gives; empty or 0 for no limit
    Free Gift SKUs the gift products (field added by hryvinskyi/magento2-free-gift-admin-ui)

    The cart earns floor(spent / step) × gifts per step of each gift SKU, up to the maximum. A step is a whole one: £69.99 of a £70.00 step earns nothing. Gifts per step below 1 earn nothing. Every SKU of the rule is given.

    The amount to spend per step is a whole amount: the platform stores it as a whole number, so a gift rule saved with pence in it (49.99) is refused with Discount Qty Step (Buy X) is the amount to spend per step and must be a whole amount for a free gift rule. rather than cut to 49. A step of 0 or less is left alone and gives nothing.

  • The spent amount. The sum, over the lines the rule's own Actions conditions accept, of the line's row total less its discount, in the base currency. Gift lines, the children of a bundle, and lines that carry a past gift's mark count nothing; a bought line of a gift's SKU counts like any other. Two settings per website choose whether the row total includes tax and whether the discount is subtracted. Every line's amount is turned into whole hundredths before it is added, so float sums never miss a step by a rounding error.

  • Gift lines. A gift is a line of the gift product at price 0 whose quantity is the quantity earned; a quantity the shopper types is put back. It cannot be edited into another line: the cart's Edit of a gift line is refused with The free gift cannot be changed. and the cart stays as it was. It ships free. It never merges with a bought line of the same product, so a cart can hold the bought line and the gift. The order keeps the gift at price 0 with the rule's id in its applied rules and in its buy request.

  • Kept in line with the cart. After the totals of a cart are collected the gift lines are compared with the gifts earned: missing gifts are added, quantities set to the earned ones, and lines nobody earns, lines of a declined rule and repeated lines of one gift are removed. A cart whose gift lines already match costs nothing more: no second totals collection, no product load, no save.

  • A gift that cannot be given is skipped. A gift product that is missing, disabled, outside the cart's website or not salable is not added, with no error and no message. Its stock status in the cart's website is read first, one indexed lookup through the stock registry (which answers for the website's own stock when the store runs several), and the product is loaded only when that lookup says it can be sold; a gift found unsellable is not asked about again in the same request; a gift line whose product can no longer be sold, or that carries an error, is removed. The cart can always be checked out.

  • A declined gift stays declined. A gift line the shopper removes (in the storefront, through the web APIs or in the admin order form) is not added again to that cart. A gift line removed by the reconciliation is no decline. When a guest cart merges into the customer's cart at login, its declined gifts are handed over.

  • A past gift is refused at the door. An order item or a wish list entry keeps the buy request its product was added with, and the buy request of a gift line carries a past gift's mark. A request in which any registered mark finds a past gift is refused before it becomes a cart line, with was a free gift of an earlier order. The cart adds its free gift again when it earns one. A bought line of the same product is never touched, and the cart gets its own gift line from the reconciliation when it earns one. How each path behaves:

    Path What happens
    Reorder from the storefront The other items are added and the cart is saved. The platform's reorder collects the sentence among its per-item errors: the web API's reorder returns it; the storefront's reorder button shows no per-item error of any kind, so the gift is simply left out.
    Reorder in the admin order form The order form goes on with the other items and shows the sentence as an error.
    Wish list to cart, one entry The sentence shows as a notice, the entry stays in the wish list and the cart gets no line of it.
    Wish list to cart, Add All to Cart The other entries are moved; the sentence shows as an error naming the product, the entry stays, and a bought line of the same product in the cart stays: the platform deletes the cart line that stands for a refused product, and a refused past gift stands for none.
    Admin order form: an item from the customer's wish list or cart sidebar The sentence shows as an error and the item stays where it was.

    A line that carries a mark anyway (one saved before this refusal existed, or added without the cart's add) counts nothing towards the spent amount and is removed at the next totals collection.

  • A store view can be left without free gifts. With Free Gifts set to No for a store view, its carts are treated like a cart shipped to several addresses (below) and the cart progress bar offers no gift goal there. Use it for a store view whose carts must never get a gift, such as one that marketplace orders are imported into, when the gift rule is shared with the other store views of the website.

  • A cart shipped to several addresses earns no gift. Its gift lines leave it, whatever quantity they were given, and it stores no spent amount; the gift comes back once the cart ships to one address again and earns it.

  • The stored spent amount is for the rules that apply. A rule whose conditions the cart fails gets no spent amount, and a cart without lines stores none, so the progress shown never points at a gift the cart cannot get.

Requirements

  • PHP 8.1 to 8.5.
  • hryvinskyi/magento2-free-gift-api ^1.1.
  • The platform's Framework, Quote, Rule, SalesRule, Catalog, CatalogInventory, OfflineShipping and Store modules (what composer.json and etc/module.xml declare).

Installation

composer require hryvinskyi/magento2-free-gift-pack
bin/magento setup:upgrade
bin/magento setup:di:compile
bin/magento cache:flush

The pack installs this module with the admin screens, the storefront module and the cart progress bar. composer require hryvinskyi/magento2-free-gift installs this module and its contracts alone, which is enough for a rule created by code; the admin then has neither the action in the rule form nor the field for the gift SKUs.

Configuration

The settings are shown by hryvinskyi/magento2-free-gift-admin-ui under Stores › Configuration › Sales › Free Gift; this module ships their defaults. Free Gifts is read for the store view of the cart, the spent amount settings for its website.

Path Label Default Scope
free_gift/general/enabled General › Free Gifts Yes default, website, store view
free_gift/spent/include_tax Spent Amount › Include Tax Yes default, website
free_gift/spent/subtract_discount Spent Amount › Subtract Discounts Yes default, website

Stored data

Where What
table hryvinskyi_free_gift_rule_sku rule_id (foreign key to salesrule, rows deleted with the rule), sku (varchar 64), position; primary key rule_id, sku. Read once per request and replaced in one transaction.
column quote.free_gift_state JSON text, {"spent": {"<rule id>": <hundredths>}, "declined": [<rule id>]}; spent holds only the gift rules that apply to the cart; empty when there is nothing to store. Loaded with the cart row, so it costs no query.
cart line option free_gift_rule_id The id of the rule that added a gift line. The same key travels in the line's buy request, so the order item keeps it; there it is this module's past gift mark.

A rule saved with the data key free_gift_skus (a text with one SKU per comma or line, or a list) replaces its gift SKUs; an empty value removes them, and a save without the key keeps them.

Limits

  • Gifts are earned by a cart with one address: the shipping address, the billing address for a virtual cart. A cart shipped to several addresses holds no gift line.
  • The storefront's reorder button of the platform shows no per-item error, so a shopper who reorders a gift is not told why it is missing; the web API's reorder returns the sentence.
  • "The shopper chooses one gift" is not offered: every SKU of a rule is given.
  • Amounts are counted in hundredths of the base currency.
  • A rule counts as applied when the latest totals collection applied it to one of the address's lines; a rule that does not apply earns nothing and its gift lines leave.

Registrations

Where What
discountRules of Magento\SalesRule\Model\Rule\Action\Discount\CalculatorFactory free_gift_spent, Model\Rule\Action\NoDiscountCalculator
plugin on Magento\Quote\Model\Quote::collectTotals (around) keeps gift lines at price 0, then reconciles them once the collection has finished and saves a changed cart
plugin on Magento\Quote\Model\QuoteRepository\SaveHandler::save (around) tells the reconciliation that the repository saves the cart right after
plugin on Magento\OfflineShipping\Model\SalesRule\Calculator::processFreeShipping and on Magento\Quote\Model\Quote\Address\FreeShippingInterface::isFreeShipping (after) marks gift lines as shipping free
plugin on Magento\Quote\Model\Quote::updateItem (before) refuses to edit a gift line
plugin on Magento\Quote\Model\Quote::addProduct (before) and ::getItemByProduct (after) refuses a request that carries a past gift's mark, and lets the refused product stand for no cart line
marks of Model\PastGiftMark\CompositePastGiftMark free_gift_rule_id, Model\PastGiftMark\GiftRuleIdMark
event salesrule_rule_save_before refuses a gift rule whose spend per step holds pence
event salesrule_rule_save_after saves the gift SKUs of the rule
event sales_quote_remove_item records a declined gift
event sales_quote_merge_after hands a merged cart's declined gifts to the cart it merges into
preferences the contracts of hryvinskyi/magento2-free-gift-api; PastGiftMarkInterface resolves to the composite

Extension point: past gift marks

A module whose gift lines, now or in an earlier setup of the store, write a mark of their own into the buy request makes the cart refuse such requests by contributing one mark. A mark implements Hryvinskyi\FreeGiftApi\Api\PastGiftMarkInterface: ruleIdIn(array $buyRequest): ?int gets the fields of a request to add a product (an order item's stored buy request, a wish list entry's request) and returns the positive id of the rule that gave the gift, or null when the request carries no mark of that module. It is added with one item in the module's etc/di.xml:

<type name="Hryvinskyi\FreeGift\Model\PastGiftMark\CompositePastGiftMark">
    <arguments>
        <argument name="marks" xsi:type="array">
            <item name="vendor_module_mark" xsi:type="object">Vendor\Module\Model\PastGiftMark</item>
        </argument>
    </arguments>
</type>

The composite (@api) asks the marks in the order they are registered, and the first that finds a past gift names its rule. Every mark is read by the refusal on the way in, by the reconciliation that removes a marked line, and by the spent amount that leaves such a line out. The module's composer.json requires hryvinskyi/magento2-free-gift-api ^1.1 and hryvinskyi/magento2-free-gift ^1.1, and its module.xml lists Hryvinskyi_FreeGift in its sequence.