• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
Locked
0

Discount code when buying two specific products

Community Beginner ,
Jan 17, 2019 Jan 17, 2019

Copy link to clipboard

Copied

Hello,

Is there a way to create a Discount code that only works if the users buys at least 1 quantity of two different products at the same time?

Our site only has 2 products. We want to create a discount code, ex: SAVE123, and when the user is buying both of our products, they add the discount code to save a percentage on the total. The code should not work if they are buying 2 quantities of one product.

I see how to create a discount code based on percentage, but not sure how to check if both products are in their shopping cart before applying it.

Thank you,

TOPICS
Developer

Views

1.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 17, 2019 Jan 17, 2019

Copy link to clipboard

Copied

You can look to only show the discount code area in your cart if those two products exist.

You can do that with Liquid Markup or Javascript by detecting that the two types of products exist in the cart.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 18, 2019 Jan 18, 2019

Copy link to clipboard

Copied

Thanks for always replying Liam. Do you know any tutorials or videos that would show how to do that exactly?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 18, 2019 Jan 18, 2019

Copy link to clipboard

Copied

I (kind of) got it! Because our site only has 2 products I can use "this.items.size" in the Shopping Cart module to check if it's greater than 1 and then hide/show the discount code field:

{% if this.items.size > 1 %}

[DISCOUNT FIELD]

{% endif %}

Thank you thank you Liam for getting me on the right track! This doesn't fix the original question of 2 specific products so I don't want to make this as the correct answer, but I'm sure that solution isn't too different from checking the "items size".

Thanks again.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 18, 2019 Jan 18, 2019

Copy link to clipboard

Copied

Ok I figured out how to check for one specific item.code:

{% for item in this.items -%}

  {% if item.code == "a123" %}

    <p>You have Apple in your cart</p>

  {% endif %}

{% endfor %}

But I need to check for two. I am able to create an array of the item.codes and output it using "{{ productCodesArray }}", but when I try to use an 'if' statement to check the array it doesn't work:

{% assign productCodesArray = this.items | map: "code" %}

{{ productCodesArray }} <!- - Outputs all item.codes of products in shopping cart (ex: a123b456) -->

    {% if productCodesArray contains 'a123' and 'b456' %} <!- - Doesn't do anything even though item.codes are part of output above -->

  <p>Both Apple and Banana are in your cart</p>

{% endif %}

Any idea why this code is not working? Thank you.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 20, 2019 Jan 20, 2019

Copy link to clipboard

Copied

Your on the right track with the array. Contains is for a string not an array.

So simply convert your array to a string.

Then you can use Contains.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 23, 2019 Jan 23, 2019

Copy link to clipboard

Copied

Hi Liam,

Why doesn't this code?

{% assign productCodesArray = this.items | map: "code" %}

According to Filters | Business Catalyst Support  "map" should turn an array into a string. I have  "assign productCodesArray = this.items" to create the array, I thought that's how an array was created. Or are arrays created differently? All the examples on that link are "Assuming myArray is..." but I need to create the array first.

i also tried the following, comments of what works in blue and did not work in red, output in green:

{% assign productCodesArrayONE = this.items | map: "code" %}

{{ productCodesArrayONE }} a123b456 <- WORKS: this is correct, only the codes of the two products in cart are shown (a123 and b456)

{% capture my_variableONE %} {{ productCodesArrayONE }} {% endcapture %}

{{ my_variableONE }} a123b456 <- WORKS: same as above

{% if my_variableONE contains "a123" %}

     <p class="lead">Apple product in your cart</p> <- WORKS: shows on front end ONLY when apple product is in cart

{% endif %}

{% if my_variableONE contains "b456" %}

     <p class="lead">Banana product in your cart</p> <- WORKS: shows on front end ONLY when banana product is in cart

{% endif %}

{% if my_variableONE contains "a123" and "b456" %}

     <p class="lead">Both Apple and Banana products are in your cart</p> <- WORKS: shows on front end if both apple and banana products are in cart, does not show if ONLY apple product is in cart. DOES NOT WORK: shows when ONLY banana product is in cart.

{% endif %}

                         

Any idea why "{% if my_variableONE contains "a123" and "b456" %}" is showing the output when only "b456" product is in the cart? If it weren't for that this code would be exactly what I needed.

Thank you.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 23, 2019 Jan 23, 2019

Copy link to clipboard

Copied

LATEST

Map creates an array of values, not a string.

You overcomplicated the solution though.

on the cart page..

- Assign a value of showDiscount to false

- You can map if you want.

- Then use join to get your string

- hen if value contains "code" and "code

- If true then set showDiscount to true

- Around your discount html - if showDiscount = true

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines