# Gift With Purchase

{% hint style="info" %}
Disclaimer:

* Discount Ninja provides these tips for information only. Use the script outlined below at your own risk and modify where necessary.
* Customers with a technical background and/or intimate understanding of Shopify's APIs can technically still bypass the approach outlined below. For a robust solution, you'll need to implement additional automation on the back-end (e.g. using Flow) to check incoming orders.
  {% endhint %}

## Context <a href="#h_47c5ecc423" id="h_47c5ecc423"></a>

Discount Ninja allows for products to be added to an order as a gift with purchase. These products need to be active and published to the Online Store channel.

To avoid customers adding more than the allowed amount of gifts to their order, we advise setting a price for the gift products. Gifts have a 100% discount, ensuring the price is reduced to zero for the gift products at checkout. The app knows how many gifts are allowed per order and charges the full amount for any surplus gifts that the customer has added to the order.

If you set the price of the gift product to zero, you run the risk that customers will add more gifts than allowed. If there is no price associated with the gift product the customer will not be charged for surplus gifts at checkout.

Discount Ninja can automatically add (and remove) the gift products to the cart (and therefore to the order), based on the rules of the promotion.

The tips below are useful if you want to avoid that customers can add the gift products manually.

## How customers can discover (and order) gift products manually <a href="#h_e80b1b21ff" id="h_e80b1b21ff"></a>

Customers can add gift products in the same ways as any other product in your store. That is to say, they can find them in the following ways:

* Collection page
* Search results
* Link to the product page (from cart page for example)
* Via a search engine

To avoid customers finding the product, you'll need to implement logic to avoid each of those scenarios.

## Marking products as gift products <a href="#h_71a43f5104" id="h_71a43f5104"></a>

First, we'll add a tag to those products that we don't want to be sellable, in other words, the gift products that we do not want customers to find and add to the cart manually.

IMPORTANT: the case of the tag matters. `gwp` is not the same as `GWP`.

To do this, open the product in Shopify and add the tag to the product. In this example, we're using the tag GWP, but you can choose a different name for your tag:

[![](https://downloads.intercomcdn.com/i/o/435540139/b4674337561e1bb1cca6c75b/image.png)](https://downloads.intercomcdn.com/i/o/435540139/b4674337561e1bb1cca6c75b/image.png)

## Product Detail Page (PDP) redirect <a href="#h_4f22fc53d0" id="h_4f22fc53d0"></a>

To avoid that customers can access the product page, add the following script at the top of the product template (`product.json` or `product.liquid` file):

```liquid
{%- if product.tags contains 'GWP' -%}
    <script>window.location.href="/"</script>
{%- endif %}
```

This script detects products with a specific tag (in the above example GWP) and redirects the customer to the home page of the shop if the product has the tag.

## GWP product page template

Alternatively, you can develop a new product template, specifically designed for your gift products. To do that, edit the code of your theme and click "Add new template". Then, choose the type of template and provide a name:

<div align="left"><figure><img src="https://326468467-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkEkTCDyi4rdjkChWHzFd%2Fuploads%2Fq9OPQbi0HOpZZFmz0Txo%2Fimage.png?alt=media&#x26;token=f715609d-119e-4261-9e81-907e8e184820" alt=""><figcaption></figcaption></figure></div>

You'll want the new template to be based on the default template. The gwp template should be different from your default product template in that it does not contain a form and "Add to cart" button.

You can then set the gwp template for each of your gift products using the **Theme template**:

<div align="left"><figure><img src="https://326468467-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkEkTCDyi4rdjkChWHzFd%2Fuploads%2F1FWTGaAe95BHV0zZTo00%2Fimage.png?alt=media&#x26;token=b6871cac-bb50-4ebe-b803-1d49d5eb6028" alt=""><figcaption></figcaption></figure></div>

## Product List Page (PLP) <a href="#h_b157435f3b" id="h_b157435f3b"></a>

Find the collection template that contains the for loop that loops over the products in a collection.

Place the following script inside that for loop:

```liquid
{%- assign limoniapps_discountninja_product = product -%}
{%- if limoniapps_discountninja_product.object_type == 'product' -%}
    {%- if limoniapps_discountninja_product.tags contains 'GWP' -%}
        {%- continue -%}
    {%- endif -%}
{%- endif -%}
```

This script detects products with a specific tag (in the above example: GWP) and filters them out. That is to say, the product card for those products with the GWP tag is not rendered on the collection page.

The above script expects to be placed in a `for` loop that loops over the products in a collection with the variable name product. Change the `assign` statement if the variable name is different.

## Search results <a href="#h_b157435f3b" id="h_b157435f3b"></a>

Use the same logic for the search results.

Place the script in the `for` loop that renders the search results.

## Search engine <a href="#h_2703fcfe36" id="h_2703fcfe36"></a>

Avoid that products are indexed by search engines.

In the `theme.liquid` file, add the following to the `head` section:

```liquid
{%- if template == 'product' and product.tags contains 'GWP' -%}
    <meta name='robots' content='noindex'>
{%- endif -%}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://developers.discountninja.io/discount-ninja-developer-hub/theme-edits/code-edits/gift-with-purchase.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
