hryvinskyi / magento2-free-gift-import-amasty
Imports the free gift setup of Amasty Free Gift and Free Bar into the free gift module and the cart progress bar: the gift rules, the progress bar settings, the spent and note settings, and the cleanup of the carts that hold an Amasty gift line. Reads the tables those modules left behind; it does no
Package info
github.com/hryvinskyi/magento2-free-gift-import-amasty
Type:magento2-module
pkg:composer/hryvinskyi/magento2-free-gift-import-amasty
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
- hryvinskyi/magento2-free-gift-frontend: ^1.1
- magento/framework: *
- magento/module-config: *
- magento/module-quote: *
- magento/module-sales: *
- magento/module-sales-rule: *
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Moves a store from Amasty Free Gift (Amasty_Promo) and Amasty Free Bar onto hryvinskyi/magento2-free-gift and hryvinskyi/magento2-cart-progress-bar. A data patch imports the gift rules, the Free Bar profile and the settings once, on setup:upgrade, and cleans the active carts. A past gift mark makes the cart refuse the Amasty gift lines of old orders on reorder and wish list paths.
It reads the tables the Amasty modules left behind and requires none of their packages. It runs on a database without those tables too.
What the import does
The patch runs inside the transaction the platform opens around a data patch: it applies whole or not at all. In this order:
-
Gift rules. Every cart price rule with the action
ampromo_spent("gift for every X spent") gets the actionfree_gift_spent. Its gift SKUs are copied fromamasty_ampromo_rule.sku, in their order. Discount Amount (gifts per step), Discount Qty Step (spend per step), Maximum Qty, websites, groups, dates, coupons, conditions and priority stay as they are. -
Options the new action cannot express. The free gift action gives every SKU at no charge, taxed on its zero price and shipped free. A converted rule whose Amasty row asks for more becomes a free gift of every SKU, and a warning names the dropped options. Dropped are a gift discount that is not empty and not
100%(items_discount), a choice of one SKU (typenot 0), a minimal gift price above 0 (minimal_items_price), tax on the original price (apply_tax) and shipping charged for the gift (apply_shipping). -
Other Amasty Free Gift rules. Every rule with another
ampromo_*action is switched off. The platform refuses to load a rule with an action it does not know, so such a rule would break every cart. -
Rules that name an Amasty class. Every active rule whose conditions or actions name an Amasty class is switched off: such a condition can no longer be built. A converted gift rule switched off this way is logged as critical.
-
The bar. Of the active Free Bar profiles of type
ampromo_barlinked to an active converted rule, the one with the lowest priority value is imported, the one the old bar showed. A profile linked to several converted rules counts once and shows the lowest of their ids. -
Settings. Written only when the table
amasty_ampromo_ruleexists:From To the profile's rule free_gift/bar/rule_id(default scope)the profile's store views free_gift/bar/enabledandcart_progress_bar/general/enabled: 1 for those store views, 0 at the default scope; 1 at the default scope for a profile of all store viewsthe profile's pages the cart progress bar has a page type for cart_progress_bar/general/page_types, in the profile's order; empty when the profile has none of themthe profile's link, when it is clickable free_gift/bar/link_url, if the cart progress bar accepts the linklabels init_message,progress_message,achieved_message,terms_messagefree_gift/bar/label_initial,label_progress,label_achieved,label_terms, per store view (a label of all store views at the default scope);{{ruleGoal}}becomes{{goal}}and{{ruleGoalLeft}}becomes{{goal_left}}; markup is reduced to its textampromo/general/tax_includefree_gift/spent/include_tax, at the default and website scopesampromo/general/discount_includefree_gift/spent/subtract_discount, at the default and website scopesampromo/messages/cart_messagefree_gift/cart/gift_line_note, at its own scopeA setting the store never saved at the default scope held the Amasty default, and the new settings default to other values. So the Amasty default is written at the default scope:
include_tax0,subtract_discount0, the gift line noteFREE!. -
Carts. In every active cart, the lines whose buy request carries a non-empty, positive whole
options.ampromo_rule_idare deleted, with their child lines and options. Those carts are flagged to recollect their totals when they next load, so they get the new module's gift line when they earn it. A line whose rule id is empty or malformed stays. -
Amasty settings. Every
core_config_datarow underampromo/is deleted.
What it does not import
- The Free Bar's look and behaviour: colours, font, text size, position, the close button, custom CSS, its customer groups and its goal (the goal is the rule's spend per step). Bar types other than
ampromo_bar. - The Free Bar's pages the cart progress bar has no page type for, such as
checkoutandother("Another Pages"): the bar is not shown at the top of those pages, and a warning names each one. - Amasty Free Gift settings other than the three above, the pop-up and the banners.
- The other Amasty rule actions: those rules are switched off, not converted.
- Orders. Historical orders stay as they are.
- The Amasty tables. They stay in the database and nothing reads them after the import.
The log
Every line goes to the platform's logger (var/log/system.log) and starts with Free gift import: .
| Level | Line |
|---|---|
| info | cart price rule <id> converted from ampromo_spent to free_gift_spent (is_active <0|1>, spend step <step>, gift SKUs "<sku,sku>") |
| warning | cart price rule <id> now gives every gift SKU free of charge; the options the free gift action does not have are dropped: <column> "<old value>", … |
| info | cart price rule <id> switched off (action <action>, is_active was <0|1>) |
| warning | cart price rule <id> switched off (action <action>, is_active was 1): its conditions name a class of a removed module |
| critical | the same line, followed by ; it is a free gift rule, so its gift is off until it is fixed |
| warning | <n> active progress bar profiles show a gift rule; profile <id>, of the lowest priority value, which the old bar showed, is imported |
| warning | <path> at stores <id> is not copied: <target> has no store view scope |
| warning | <label path> of store <id>: "<old label>" is written as "<new label>" |
| warning | Link "<link>" is refused by the cart progress bar; the bar is not a link |
| warning | Page "<code>" of the progress bar profile is not copied: the cart progress bar has no such page type |
| info | <n> Free Gift gift lines deleted from <m> active carts, which recollect their totals |
Reverting the rules
The rule lines of the log hold the old values. With the Amasty modules installed again, run one statement for each line:
-- "cart price rule <id> converted from ampromo_spent to free_gift_spent …" UPDATE salesrule SET simple_action = 'ampromo_spent' WHERE rule_id = <id>; DELETE FROM hryvinskyi_free_gift_rule_sku WHERE rule_id = <id>; -- "cart price rule <id> switched off (action <action>, is_active was <n>) …" UPDATE salesrule SET is_active = <n> WHERE rule_id = <id>;
The import does not log the deleted Amasty settings and cart lines. To keep them, back up the core_config_data rows under ampromo/ and the quote_item and quote_item_option tables before setup:upgrade.
Past gifts of old orders
An order item that Amasty Free Gift gave keeps options.ampromo_rule_id in its buy request. This module registers a past gift mark that reads it: a positive whole number names the rule; an empty, zero or malformed value marks nothing. The free gift module then refuses such a request on every reorder and wish list path, and the cart gets its own gift line when it earns one. This module adds no observer.
An order the admin creates from an earlier order (Reorder in the admin) rebuilds the options of an item's request from the item's custom options, which drops the rule id when the gift product has custom options. So in the admin area a plugin on Magento\Sales\Model\AdminOrder\Create::initFromOrderItem() reads the mark from the request the order item keeps, before anything is rebuilt: a marked item is not added, the admin reads the sentence the cart shows for any refused past gift ("… was a free gift of an earlier order. The cart adds its free gift again when it earns one."), and the other items of the order are added.
Requirements
- PHP 8.1 to 8.5.
hryvinskyi/magento2-free-gift-api^1.1,hryvinskyi/magento2-free-gift^1.1,hryvinskyi/magento2-free-gift-frontend^1.1 andhryvinskyi/magento2-cart-progress-bar^1.0: the settings the import writes are theirs.- The platform's Framework, Config, Quote, Sales and SalesRule modules.
The module.xml sequence makes the free gift tables exist before the patch runs.
Installation
Install it in the same release that removes the Amasty modules:
composer require hryvinskyi/magento2-free-gift-import-amasty bin/magento setup:upgrade bin/magento setup:di:compile bin/magento cache:flush
Once the import has run, the patch never runs again. Keep the module installed: its past gift mark guards reorders of old orders.
Running without the Amasty tables
On a database where Amasty Free Gift never ran, the patch converts nothing, writes no setting and only deletes rows under ampromo/, if there are any.
Limits
- The patch runs once per database. Do not run it on a database where another import already deleted the
ampromo/settings while the Amasty tables stayed. It would read those settings as never saved and write the Amasty defaults at the default scope. - The "shopper chooses one gift" and discounted-gift options are not kept: such a rule gives every SKU free of charge (see the log).
Registrations
| Where | What |
|---|---|
| data patch | Setup\Patch\Data\ImportFreeGiftSetupPatch |
marks of Hryvinskyi\FreeGift\Model\PastGiftMark\CompositePastGiftMark |
amasty_free_gift, Model\LegacyGiftMark |
plugin on Magento\Sales\Model\AdminOrder\Create (admin area) |
hryvinskyi_free_gift_import_refuse_legacy_gift_on_admin_reorder, Plugin\RefuseLegacyGiftOnAdminReorder |
| preferences | Api\SettingsMapperInterface → Model\SettingsMapper, Api\DroppedRuleOptionsInterface → Model\DroppedRuleOptions |