This repository has been archived by the owner on Sep 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from byjord/master
Adds Conditionally Removable Line Item Script
- Loading branch information
Showing
42 changed files
with
120 additions
and
13 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
68 changes: 68 additions & 0 deletions
68
...tionally Remove Products from the Cart/Conditional Vendor and Country/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Conditionally Remove Vendors from the Cart with Buyer Country | ||
For reasons that are not discussed in this document some vendors demand that products not be shipped to certain countries. How can you make that happen on Shopify Plus? You can use Scripts, and with this script you can remove products from the cart when they're matching a campaign defined. | ||
|
||
### What is a Campaign | ||
A campaign in the context of this script is a list of countries (`NOOP_${GROUP_NAME}`) of which a list of vendors (`VENDOR_NOOP_${GROUP_NAME}`) cannot be shipped to. | ||
|
||
### Getting Started | ||
Remove the example settings (lines 20-42) from `script.rb`. | ||
|
||
### Create a new Campaign | ||
Add two lists, `VENDOR_NOOP_${GROUP_NAME}` and `NOOP_${GROUP_NAME}`. | ||
|
||
1. Place vendor names in `VENDOR_NOOP_${GROUP_NAME}` formatted as a list | ||
2. Input all the countries you want to include in `NOOP_${GROUP_NAME}` in [ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) format. | ||
|
||
### Examples | ||
#### Disallow Multiple Vendors to be shipped to the EU | ||
In this example we do not allow the vendors `JordansAwesomeBrand` and `Example2` to be shipped to any EU countries. | ||
|
||
```ruby | ||
VENDOR_NOOP_EU = ['JordansAwesomeBrand', 'Example2'] | ||
NOOP_EU = ['AT', 'BE', 'BG', 'CY', 'CZ', 'DK', 'EE', 'FI', 'FR', 'DE', 'GR', 'HU', 'IE', 'IT', 'IM', 'HR', 'EL', 'LV', 'LT', 'LU', 'MT', 'NL', 'PL', 'PT', 'RO', 'SK', 'SI', 'ES', 'SE', 'GB'] | ||
|
||
CAMPAIGNS = [ | ||
RemoveLineItemsCampaign.new(VENDOR_NOOP_EU, NOOP_EU) | ||
] | ||
``` | ||
|
||
#### Disallow Single Vendor to be shipped to Oceania (Multiple Countries) | ||
|
||
In this example we do not allow the vendors `Example1` to be shipped to any Oceania countries. | ||
*limitation*: Hawaii is not supported because it uses the (ISO 3166-1 alpha-2)[https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2] code of the USA. | ||
|
||
```ruby | ||
VENDOR_NOOP_OCEANIA = ['Example1'] | ||
NOOP_OCEANIA = ['AU', 'NZ', 'NF', 'FJ', 'NC', 'PG', 'SB', 'VU', 'FM', 'GU', 'KI', 'MH', 'NR', 'MP', 'PW', 'UM', 'AS', 'CK', 'CL', 'PF', 'NU', 'PN', 'WS', 'TK', 'TO', 'TV', 'WF'] | ||
|
||
CAMPAIGNS = [ | ||
RemoveLineItemsCampaign.new(VENDOR_NOOP_OCEANIA, NOOP_OCEANIA) | ||
] | ||
``` | ||
|
||
#### Disallow Single Vendor to be shipped to the US (single country) | ||
|
||
In this example we do not allow the vendors `Example2` to be shipped to the US. | ||
|
||
```ruby | ||
VENDOR_NOOP_US = ['Example2'] | ||
NOOP_US = ['US']d | ||
|
||
CAMPAIGNS = [ | ||
RemoveLineItemsCampaign.new(VENDOR_NOOP_US, NOOP_US) | ||
] | ||
``` | ||
|
||
#### Running multiple campaigns | ||
|
||
##### How to: | ||
Campaign is just a list, you can add another campaign to the list by adding a commar seperator `,` on the last campaign and then adding `RemoveLineItemsCampaign.new(VENDOR_NOOP_${GROUP_NAME}, NOOP_${GROUP_NAME})` on a new line. | ||
|
||
```ruby | ||
... | ||
CAMPAIGNS = [ | ||
RemoveLineItemsCampaign.new(VENDOR_NOOP_OCEANIA, NOOP_OCEANIA), | ||
RemoveLineItemsCampaign.new(VENDOR_NOOP_US, NOOP_US) | ||
] | ||
... | ||
``` |
52 changes: 52 additions & 0 deletions
52
...Item/Conditionally Remove Products from the Cart/Conditional Vendor and Country/script.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# ======================================= # | ||
# Script code (do not modify) # | ||
# ======================================= # | ||
|
||
class RemoveLineItemsCampaign | ||
def initialize(vendors, countries) | ||
@vendors = vendors.map(&:downcase) | ||
@countries = countries.map(&:downcase) | ||
end | ||
|
||
def run(cart) | ||
return if cart.nil? || !@countries.include?(cart.shipping_address.country_code.downcase) | ||
cart.line_items.delete_if { |item| @vendors.include?(item.variant.product.vendor.downcase) } | ||
end | ||
end | ||
|
||
# ========================= # | ||
# Settings - Modify # | ||
# ========================= # | ||
|
||
# # Set which vendors have restricted shipping countries | ||
# # Products with this vendor will be automatically removed if | ||
# # a restricted shipping address is selected during checkout: | ||
|
||
# Campaign 1: Exclude from shipping to the EU | ||
VENDOR_NOOP_EU = ['JordansAwesomeBrand', 'Example2'] | ||
NOOP_EU = ['AT', 'BE', 'BG', 'CY', 'CZ', 'DK', 'EE', 'FI', 'FR', 'DE', 'GR', 'HU', 'IE', 'IT', 'IM', 'HR', 'EL', 'LV', 'LT', 'LU', 'MT', 'NL', 'PL', 'PT', 'RO', 'SK', 'SI', 'ES', 'SE', 'GB'] | ||
|
||
# Campaign 2: Exclude from shipping to the USA | ||
VENDOR_NOOP_US = ['Example1', 'Example3', 'Example4'] | ||
NOOP_US = ['US'] | ||
|
||
# Campaign 3: Exclude from shipping to Ocenia (Not including Hawaii because it uses the same ISO 3166-1 code as the USA) | ||
VENDOR_NOOP_OCEANIA = ['JordansAwesomeBrand'] | ||
NOOP_OCEANIA = ['AU', 'NZ', 'NF', 'FJ', 'NC', 'PG', 'SB', 'VU', 'FM', 'GU', 'KI', 'MH', 'NR', 'MP', 'PW', 'UM', 'AS', 'CK', 'CL', 'PF', 'NU', 'PN', 'WS', 'TK', 'TO', 'TV', 'WF'] | ||
|
||
# Add each | ||
CAMPAIGNS = [ | ||
RemoveLineItemsCampaign.new(VENDOR_NOOP_EU, NOOP_EU), | ||
RemoveLineItemsCampaign.new(VENDOR_NOOP_US, NOOP_US), | ||
RemoveLineItemsCampaign.new(VENDOR_NOOP_OCEANIA, NOOP_OCEANIA) | ||
] | ||
|
||
# ======================================= # | ||
# Script code (do not modify) # | ||
# ======================================= # | ||
|
||
CAMPAIGNS.each do |campaign| | ||
campaign.run(Input.cart) | ||
end | ||
|
||
Output.cart = Input.cart |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.