Buy 3 get 3 free using shopify script

using this code

https://gist.githubusercontent.com/dylanjhunt/e7dd2939680b645f235d6db6252ca4ff/raw/07ab2e575d5a3d21fe6d5e2ad46fbc48f23cc8bb/Shopify%20Scripts%20-%20Buy%202%20get%201%20free

for buy 3 get 3 free

but does not work when I have quantity 8

hello there

The code that you provided applies the discount when the quantity of a product in the cart is a multiple of 6 (i.e., 6, 12, 18, etc.). This means that when the quantity is 8, the discount will not be applied because 8 is not a multiple of 6.

To modify the code to apply the discount when the quantity is 8, you can try changing the “if” statement to check for quantities that are multiples of 6 or 8. Here is an example of what the updated code might look like:

{% assign quantity = 0 %}
{% for item in cart.items %}
  {% assign quantity = quantity | plus: item.quantity %}
{% endfor %}

{% if quantity >= 6 %}
  {% assign multiples_of_six = quantity | divided_by: 6 %}
  {% assign remainder = quantity | modulo: 6 %}
  {% assign discount_times = multiples_of_six | times: 3 %}
  {% if remainder >= 2 %}
    {% assign discount_times = discount_times | plus: 3 %}
  {% endif %}
  {% if remainder == 1 or remainder == 7 %}
    {% assign discount_times = discount_times | plus: 1 %}
  {% endif %}
  {% if remainder == 4 %}
    {% assign discount_times = discount_times | plus: 2 %}
  {% endif %}

  {% assign discount_applied = false %}
  {% for discount in cart.discounts %}
    {% if discount.title == "Buy 3 Get 3 Free" %}
      {% assign discount_applied = true %}
    {% endif %}
  {% endfor %}

  {% if discount_applied == false %}
    {% assign discount_value = 100 %}
    {% for line in checkout.lines %}
      {% if line.line_price <= discount_value %}
        {% assign discount_value = line.line_price %}
      {% endif %}
    {% endfor %}
    {% assign discount_amount = discount_value | times: discount_times %}
    {% if discount_amount > 0 %}
      {% assign discount = cart.create_discount %}
      {% assign discount.title = "Buy 3 Get 3 Free" %}
      {% assign discount.amount = -discount_amount %}
      {% assign discount.save %}
    {% endif %}
  {% endif %}
{% endif %}

This updated code checks for quantities that are multiples of 6 or 8 and applies the discount accordingly. The updated “if” statement checks for remainders of 2, 4, 7, and 1 (in addition to multiples of 6) to determine the number of times the discount should be applied.