Cart exemptions and free gifts
Overview
HidePrice Pro keeps a product whose Add to Cart button is hidden out of a customer's cart, through whichever channel the customer uses: the cart form, REST, GraphQL, the wishlist or a reorder. Orders that staff create in the admin, calls made with an admin or integration token, and scheduled jobs can add any product: this is how your team places an order for a customer after replying to a price request. A module that must still put such a product into the cart registers itself with MageMe\HidePricePro\Model\QuoteItemExemption. There are two ways:
- a promo marker, for a promotion module that gives the product away as a free gift;
- a checker, for a module with its own rule, the way EasyQuote lets the items of an accepted EasyQuote quote through.
No promotion module and no checker are registered by default. EasyQuote registers its checker itself when it is installed.
Authentication
Not applicable. Exemptions are registered in the di.xml of your own module; there is no API call. Admin users and integrations calling the cart API need no exemption: they can add any product.
Operations
How exemptions work
QuoteItemExemption::isExempt($buyRequest, $cart, $productId, $item = null) is asked twice for a product whose Add to Cart button is hidden from the customer:
- While the product is added, with
$item = null. If it answerstrue, the product goes into the cart. - When the cart is checked, with the cart item, every time HidePrice Pro looks for hidden items in it. If it answers
false, the item is taken out.
$productId is the added product; for a cart item, the product of the top-level item, so a configurable or bundle line is decided by its parent. The answer is true when the buy request carries a registered promo marker and the gift rule below holds, or when any registered checker answers true.
Free gifts from promotion modules
A promo marker is the key a promotion module puts into the buy request of the gift it adds. Register the key as an item of the promoMarkers argument; empty entries are ignored.
A product with the marker can be added to the cart. It stays there only once your module has set its custom price to 0. Before the cart totals are collected, HidePrice Pro removes every hidden item with a marker whose custom price is not 0. That covers a forged marker and a gift that keeps a price. So your module must set the price before the totals are first calculated. This check runs on the storefront and for REST, SOAP and GraphQL carts.
Magento's cart pages and APIs don't let a customer set a custom price, so a marker alone doesn't keep a product in the cart. Another add-to-cart extension that passes the customer's request on unchanged could, so check the ones you use. Register a marker only for a module that controls how many gifts a cart may get. What store owners see of this is in the FAQ.
Accepted EasyQuote quotes
EasyQuote registers a checker, MageMe\EasyQuote\Model\Quote\HidePriceProExemption, under the name easyquote. It lets an item through when its buy request names an accepted EasyQuote quote that hasn't expired. The item must also match a line of that EasyQuote quote. So a customer can buy the products of an accepted EasyQuote quote even though their Add to Cart button is hidden.
A module that needs its own rule adds a checker the same way: an object with this method, as an item of the checkers argument.
public function isExempt(
\Magento\Framework\DataObject $buyRequest,
\Magento\Quote\Api\Data\CartInterface $cart,
int $productId,
?\Magento\Quote\Api\Data\CartItemInterface $item = null
): bool;
A checker is asked for both calls described in How exemptions work. Answer true only for products your module vouches for. Checkers without an isExempt method are skipped.
Errors
| Situation | What happens |
|---|---|
| The module isn't registered | The product is refused with "… cannot be added to the cart.", which starts with the product name. |
| A gift with a marker still has a price when the totals are collected | The item is removed from the cart before any of its prices is shown. |
A checker answers false for an item already in the cart | The item is removed the next time the cart is checked. |
Examples
Register a promo marker, in etc/di.xml of your module:
<type name="MageMe\HidePricePro\Model\QuoteItemExemption">
<arguments>
<argument name="promoMarkers" xsi:type="array">
<item name="my_promo" xsi:type="string">my_promo_rule_id</item>
</argument>
</arguments>
</type>
Register a checker:
<type name="MageMe\HidePricePro\Model\QuoteItemExemption">
<arguments>
<argument name="checkers" xsi:type="array">
<item name="my_module" xsi:type="object">Vendor\Module\Model\HidePriceExemption</item>
</argument>
</arguments>
</type>
Run bin/magento setup:di:compile and clean the cache after you change di.xml.