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
Related
When you add BigCommerce product to Wishlist, the SKU is not added to {{wishlist.item}} object.
I did a {{log wishlist}}, after the product is added, the SKU is null.
the item object has a lot of key fields that are null like availability, summary, stock_level and also boolean values like pre-order, has_options etc. How can i populate them when adding a product to wishlist?
Is there a way to pass the SKU thru "/wishlist.php?action=addWIshlist&product_id={{product.id}} ??
if possible, how and where can i use JS code to pass this value to the wishlist action everytime i add a product to wishlist?
According to BC's Wishlist Object documentation, you should have access to the product's SKU. Now, if you have SKUs set at the variant level, and are trying to access that, you will be out of luck. Products only get added to the wishlist at the product level, and do not contain any variant information.
The issue you might be having is you might be trying to access this data on a page where it is not exposed. The only page where this data is exposed is on the wishlist details page for that wishlist. To access it on another page, you would need to make an AJAX request to the wishlist details page.
i'm writing on behalf of our client MOEBE.
Our developer has a problem with the way Roomle use the shortID which is used to add the items into the cart. He says that we need to change the shortID from SKU to product variant ID. Shopify doesn’t allow the cart API to use SKU to add items in the basket.
Alternative if we can get access to the backend in Roomle, then he will be able to create a map between the SKU and variant ID.
Sadly shopify do not allow using SKU codes using Ajax API.
If you modify the process of adding to the cart You probably need use some try and error run.
The very fact of adding a product to the cart can be largely solved by extending the JS code on the product page.
I suggest listing the variants of the product to the variable in the liquid template and using an additional script to handle adding to the cart. It might look dirty, but it will work.
<script type="text/javascript">
var product= `{{ product | json }}`; //Whole product variable
var variant_id = {{ product.variants[0].id }}; //First product variant id
</script>
Do you have one variant per product or more?
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.
I'm trying to get the current cart id from prestashop with:
$cookie->id_cart;
But it's blank until I add a product in the cart.
Is there any option to create that cart so I can get the id?
Thanks.
I have solved it, I just needed to create before the cart id.
if (is_null($context->cart->id)) {
$context->cart->add();
$context->cookie->__set('id_cart', $context->cart->id);
}
So I can now add products programmatically in my current cart.
Shopify has a CARTS api but it is read-only. I am trying to find a way to manipulate the line_item's line_price or price attribute. Shopify support has directed me here for an answer.
Since there is no proper documentation on this any help would be appreciated.
Products have variants, and a line item has a product ID and a variant ID. A line item has a price too. And no matter how you access that item (using the Ajax API or the backend API) you cannot manipulate the price. If you want to change a price, you have to change the product's variants price. That you do with the backend API and the product or variant calls.
You can't directly manipulate the price of a line item in a cart. As you mentioned, the Carts API is read-only. That's all there is to it.
Yes you can!
Dont mess the people, you can do everything you want with your store.
You can do it via JavaScript, this is some related code i have used:
//update price when changing quantity
function updatePricing() {
jQuery('#quantity').val(jQuery('#choose-select-value option:selected').val());
var quantity = jQuery('#choose-select-value').val();
var unitPriceTotal = jQuery('.product .total-price').text();
var totalPrice = unitPriceTotal * quantity;
jQuery('.product .price').html().replace(regInput ,totalPrice);
}
This is just a way to help you go to the right place...
But please, people dont mess if you dont know something, its your store and you can do anything....
I hope this helps to you to find the way.