# Free Gift At Sign Up

{% hint style="warning" %}
Shopify Plus only
{% endhint %}

{% hint style="danger" %}
This is an advanced guide meant for developers that not is supported by the Inveterate team. You should ensure you have the appropriate development resources to implement before proceeding.&#x20;
{% endhint %}

{% hint style="warning" %}
Only one line item script can be active at a time. If you already have a line item script running and you want to run this script in tandem, this will not work. You'll need to customize the scripts to work together.
{% endhint %}

{% hint style="info" %}
This guide requires the [Shopify Script Editor](https://apps.shopify.com/script-editor) app.
{% endhint %}

## Editable Values

* `MESSAGE` - `{string}` A message that will be displayed on the free gift line item in the cart and checkout.

## Script

```ruby
# Editable Values
MESSAGE = 'FREE GIFT FOR SIGNING UP!'

########
# DO NOT EDIT PAST THIS POINT
########

class InveterateFreeGiftWithSignup
  def initialize()
    @message = MESSAGE
  end

  def run(cart)
    @cart = cart
    start
  end

  private

  def start
    @customer = @cart.customer
    @line_items = @cart.line_items

    return unless valid_cart?

    discount_gift
  end

  def valid_cart?
    valid = false

    @line_items.each do |line_item|
      if line_item.variant.product.tags.include? 'inveterate-product'
        valid = true
        break
      end
    end

    return valid
  end

  def discount_gift
    @line_items.each do |line_item|
      # Skip if membership product
      next if line_item.variant.product.tags.include? 'inveterate-product'
      # Skip if product is not the free gift
      next unless line_item.variant.product.tags.include? 'inveterate-signup-gift'

      # Check quantity to create new line item if quantity is greater than 1
      new_line_item = nil
      if line_item.quantity > 1
        new_line_item = line_item.split(take: 1)
        @cart.line_items << new_line_item
      else
        new_line_item = line_item
      end

      puts new_line_item.quantity
      # Apply 100% discount
      new_line_item.change_line_price(
        Money.zero,
        message: @message
      )

      # Once discount has been applied once, break out so it doesn't get applied to another product
      break
    end
  end
end

CAMPAIGNS = [
  InveterateFreeGiftWithSignup.new()
]

CAMPAIGNS.each do |campaign|
  campaign.run(Input.cart)
end

Output.cart = Input.cart
```


---

# 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://help.inveterate.com/help-center/advanced-guides/shopify-scripts/free-gift-at-sign-up.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.
