Can you change the shortID from SKU to variant ID? (Shopify integration) - shopify

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?

Related

when you add a BigCommerce product to Wishlist, the SKU is not added to {{wishlist.item}} object

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.

How to show product variant inventory quantity per location on Shopify product Page using Admin API?

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.

Filter BigCommerce products on a custom field using API

I have products in my catalog that benefit from a rebate.
If a product does have a rebate, he has a custom field "rebate" set to 1.
I want to display a page with all products having a rebate, and I'm doing it trough the API.
As I'm new, I'm wondering what should be the syntax to get the custom field value and filter on it.
I'm testing with something like this :
https://api.bigcommerce.com/stores/{store_hash}/v3/catalog/products?include=custom_fields&rebate=1
But not working at all ... I'm getting a 422, saying that rebate is not a valid filter.
Thank you for your help,
Jaad
That's correct--custom_field keys are not a valid filter on a product request. To see the list of valid parameters you can use with a Get Product request, see our documentation here (expand the Query Params section):
https://developer.bigcommerce.com/api-reference/catalog/catalog-api/products/getproducts
You could make the request for all product data and sort the products by custom field key within your application. Or, if you wanted to limit the request to only rebate products, you could tag all rebate products in a Rebate category (this category could even be hidden). Then you could filter the request to get all products that are in that category:
https://api.bigcommerce.com/stores/{store_hash}/v3/catalog/products?categories:in={rebateCategoryID}

Shopify Scripts - New Line Item

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

Shopify API CARTS - Changing line_item line_price for price Override

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.