I am trying to apply Two Discounts in Shopify cart. I read the documentation and I found out that you can do the same by Using Scripts App provided to Shopify Plus Accounts. So, I went ahead and created a Discount Script. The script executed perfectly after creation in the Shopify Console. But, when I tried to do a preview it did not seem to work as by default a discount was not applied to the cart.
Secondly, after creating the Script it provided me a ruby script. I also don't know where to place it. I mean i may be missing that part as a result of which i am not getting default discounts applied to the cart. Below is the auto-generated ruby script:
DISCOUNTS_BY_QUANTITY = {
10_000 => 20,
1_000 => 15,
100 => 10,
10 => 5,
}
Input.cart.line_items.each do |line_item|
next if line_item.variant.product.gift_card?
quantity, discount = DISCOUNTS_BY_QUANTITY.find do |quantity, _|
line_item.quantity >= quantity
end
next unless discount
message = "#{discount}% off when buying at least #{quantity}."
line_item.change_line_price(
line_item.line_price * (Decimal.new(1) - discount.to_d / 100),
message: message,
)
end
Output.cart = Input.cart
My Question is:
How can I implement stackable discounts in Shopify?
What is the use of the autogenerated code in the Scripts Editor and how to use it?
I asked this question quite a long time back and coming back again as i have a fair idea how shopify scripts work now. The script is executed everytime on checkout page and if a condition is satisfied then it will execute as per the code written. Below is an example where 10% OFF shipping has been provided to a user who has been tagged as a Platinum User.
customer = Input.cart.customer
if customer
customer.tags.each do |tag|
next unless tag == 'Platinum'
Input.shipping_rates.each do |shipping_rate|
shipping_rate.apply_discount(shipping_rate.price * 0.10, message: "Discounted shipping")
end
end
end
Output.shipping_rates = Input.shipping_rates
The Shopify Script app is a very powerful tool and can be used to provide really good customization to a Shopify Store. The documentation provided for the App also is very explanatory and should help one out if you want to code this on your own.
Documentaion Link - Link
Update:
Seems shopify scripts are now only available to Shopify Plus customers. This answer likely would not help customers who do not have Shopify Plus.
Related
I would like to show the product variant quantity on the product page like this:
Location 1: 5 in stock
Location 2: 2 in stock
Location 3: 63 in stock
Location 4: 5 in stock
and so on...
Is it possible to do this using the admin API from Shopify?
Thanks in advance.
It is not recommended to use the Shopify admin API directly in the store front as you could be exposing the API username and keys in your JS code.
You should use a backend service or something similar where your frontend will send a request to the backend. The backend will then fetch the records using the Shopify rest admin APIs and return the data back.
If you prefer to go the custom code route below are a few things for you to keep in mind
Shopify has API rate limits. You most likely will be hitting these if you fetch every time a product page is loaded. Limits mentioned here
As an option for the rate limits, you could query the backend every x mins, save them in the database and serve the inventory levels from the DB. Or you could try to cache your request for a certain time limit instead of saving in the DB.
If you just want to how much is available across all inventory locations, you can use the below liquid/JS code.
<script>
var variantStock = {};
{% for variant in product.variants %}
variantStock[{{- variant.id -}}] = {{ variant.inventory_quantity }};
{% endfor %}
console.log(variantStock);
</script>
In the above code, you will have the available inventory quantity for each variant in the variantStock object. You can then write some extra code to display the stock levels when the user changes variants accordingly.
Check the liquid object document for variants here
Having said the above, another option is to use an app that will do the above for you. There are many apps in the Shopify app store that does exactly what you want.
I'm a developer working on a private app to manage the database and the inventory of a Shopify Online Store with Symfony and I'm looking for getting all products or inventory of the store.
I tried with the url https://{{api_key}}:{{password}}#{{shopify_domain}}/admin/products.json , but it gets only 50 to 250 products and there's more than 200 000 products on this online Store. I also tried with /admin/inventory_items, but it returns missing or invalid parameter.
Recently, I checked the documentation of cursor-based pagination and I saw that there's a parameter call page_info but I didn't know how to get it. So after many researches, I learned that I had to find the parameter on the last product displayed to go to the next product which will be on the next page. I also seen that there was a parameter on GraphQL which is called storefrontId, it looked like page_info so I tried to add it to the URL but it didn't worked. I noticed that there were a = to the end of the URL, so I tried it with =0 =1 =2 and =3, also with =rel=next =next and =page=2 but it returns the same error : { "errors": { "page_info": "Invalid value." } }
For people who will find me a solution with downloading data and uploading it daily, this is not relevant because on this online store there is more than 200 000 products and the employee aren't developer.
If there is a possibility to update and display the database in real-time, without uploading a file, it would be perfect !
If you know how to help me, I'm open to suggestions !
Thanks,
Karim HADJ-ABDELKADER
The parameter page_info should be unique ID and not just number, look at this docoment about the pagination paramenters
https://shopify.dev/docs/api/usage/pagination-rest#parameters
for example :
https://{shop}.myshopify.com/admin/api/2019-07/products.json?page_info=hijgklmn&limit=3
I would like to add a new product (line item) via Shopify Scripts when a user uses a discount code. Is it possible?
if Input.cart.discount_code && Input.cart.discount_code.code == "first10"
Input.cart.line_items << LineItem.new(variant: 24665166184512, quantity: 10, source_indices:false,grams: 0, properties_was:false, properties:false, line_price_was:false, line_price:50, original_line_price:50, discounts:0, adjustments:nil)
end
Output.cart = Input.cart
And I have error:
[Error] undefined method 'id' for 24665166184512
shopify/std_lib_mutable/core/resources/line_item.rb:164:in LineItem.to_hash
shopify/std_lib_mutable/core/resources/cart.rb:43:in Cart.to_hash
shopify/std_lib_mutable/cart_line_items/output.rb:4:in #<Class:0x7f85471e6280>.to_hash
shopify/std_lib_mutable/cart_line_items/output.rb:4:in #<Class:0x7f85471e6280>.to_hash
shopify/std_lib_mutable/core/script_kernel.rb:12:in Object.prepare_output
(prepare_output):1
Unfortunately you cannot add products to a cart using Shopify Cart Scripts.
Cart scripts can only see and handle items that are already present in the cart.
as mentioned in comments, you can delete an item because it already exists in the cart.
My advise is to use either of these methods.
1) discount codes that when someone buys something they get another product at a certain price or free.
2) create an Ajax API script that when an item is added to the cart it looks up what items are in the cart and if the item is found then adds the free item or discounted item to the cart.
https://help.shopify.com/en/themes/development/getting-started/using-ajax-api
3) (PAID OPTION) is to have a look in the app market place and you can find a few different apps available to help you with this. this one looks like it might help you? https://apps.shopify.com/special-offers
Using the legacy API in bigcommerce, trying to add a product image without success. I have done this before but it seems to not work now.
The API documentation says that there is a function to 'create a product image', however it says that the field 'product_id' is read only and any request will be rejected if it is included. If that is true and not a typo, how are you supposed to create a product image against a product?
Don't add product ID to the parameters, add it to the url. So if you want to add images to product 12345, make a post to
'/products/12345/images'
of the image data
{ 'sort_order': 1, 'image_file': 'http://blah.jpg' }
i'm using shopify_app gem and i was able to read to my shop. but then i can't write to it. i have set up the read/write credentials while installing my app so i'm sure the problem's not there. here's what i did to POST a new product in my shop:
product = ShopifyAPI::Product.new
product.title= "Nike Bag"
product.price_range = "27.00"
product.save
but it doesn't save.
Thank you so much. I need this badly.
It looks like this problem has already been solved in the shopify api google group, but just posting it here just in case somebody else finds it useful.
The product above can't be saved because not all required fields are specified. Check the sample in the shopify api docs for a list of required fields. Also check product.errors if save fails, which should give an idea of why it failed.
This should work:
product = ShopifyAPI::Product.new
product.product_type = "Snowboard"
product.title = "Burton Custom Freestlye 151"
product.body_html = "<strong>Good snowboard!</strong>"
product.vendor = "Burton"
product.variants = [ShopifyAPI::Variant.new(:price =>10)]
product.save