Get variant that was added to cart - graphql

I am using the Shopify Storefront API for a headless website, I have run into a specific issue where, when I add a variant of a product in the Cart, I can't seem to find a solid way of knowing which variant was added, the current API as is from Storefront doesn't seem to offer an option from the graphQL to know which variant was added.
Thanks in advance!
Technically, I can get this information from the variantBySelectedOptions field, however this only works when adding items to cart, since when I add a product I can send the options of the variant, however this doesn't work when I am simply trying to fetch the current items in my Cart, when for example I'm simply reloading the page, this field always expects some sort of input!

Related

Does Shopify Ajax API support Graphql?

I would like to manipulate the cart based on tags on products in the cart. I'm currently using the Shopify Ajax API to retrieve the cart contents and then the products in the cart. This leads to performance issues given the number of calls required. This is a classic issue addressed by GraphQL so I'm wondering if there is a solution available
How is making a call to the cart endpoint and getting back the contents a performance issue? Have you thought through what you are trying to do? If you do a single call to the cart, you get all the products in the cart. If you want to manipulate the products in the cart, what does that mean? If you are saying, geez, I need to see the tags of each product, and you then do an ajax call per product to get the tags, you are doing it wrong. Instead, when you place a product in the cart, add the special tag as a line item property. That way, when you want to play with the cart, you have all the data at your fingertips.
This is not a "classic GraphQL" kind of thing in my opinion, but instead, a question of perhaps not adopting the cleanest, fastest pattern of the many you could choose to use.

Shopify Script for Fixed Discount of a set price based on tags

Using Shopify Scripts :::
I am trying to achieve the below but getting issue. Could you please let me know how to do this.
$25 off a purchase of $125 or more from the "sale" section. These products have a tag "sale" and are also in a collection "sale" based on that tag.
Looking forward for your quick response.
Great Thanks
If you examine the documentation for Shopify Scripts you'll notice that there is no support for auditing the line item for tags. So you should try the other approach which is proven, and works well.
Look for an item with a compare at price. If you find one, then that typically means the item is on sale. So you can rig your logic to look for items with a compare at price, and if they have one, then determine if the cart has exceeded the $125 in value, and if so, apply your discount.
I can't give you the exact code as I don't know the structure of your code. But this is what you need to do.
Wherever a product can be added to the cart assign a JavaScript function to check if the product is of "Sale" collection. If yes, add an attribute cart.attributes.something.something and keep changing it as and when required.
When the checkout mechanism is present in the page, and if your conditions satisfy the Sale discount, take then to /checkout?DISCOUNT=<CODE> and click of checkout. It'll automatically apply the discount.

List available downloads per order in WooCommerce?

I know I can already list the full array of available downloads on the My Account page for a user, but what about filtering them out on a per order basis? As in...when a user views an order, the list of downloads available to them for that order are listed rather than every download for every order. I'm having trouble figuring out the right code that will filter them out. Any ideas?
Unfortunately WooCommerce don't have this kind of functionality nor any plugin which can fit your purpose.
Have check on filters to add downloads of that products below the respective orders but even filter nor actions where found.
Then a final way of achieving this is through a little customization which you have to code.
To achieve your requirement you have to override woocommerce template here its woocommerce/templates/myaccount/my-orders.php. Check on tutorials to override woocommerce templates in your custom plugin/child theme.
After you override, customize the new code and add you magic code in the loop of orders to display there respective download below each order.
Let me know it this solution works for you.

Pulling Custom Option ID from Magento

We have a SOAP connection to magento that is working great - we're able to pull product info, skus, descriptions, etc. using the magento API. We've been able to successfully add products to the cart programmatically with custom options, and that works great. The problem is this:
When you create a custom option for a product, it is assigned a unique ID that has to be called in order to pass that option value to the cart. For example:
www.mysite.com/magento/checkout/cart/add?product=7&qty=1&options[OPTION ID]=robots
Assuming I have my custom option ID correct, this will add 1 product with the option "robots." Which is nice.
However, we have hundreds of products, and while they all use similar custom options, each option is given a unique ID. That means I need to be able to call the magento API and get custom options details (specifically the option ID) so that we can add them to the cart properly. I have been back and forth with Varien Support (Magento), but they are less than helpful, as usual. Now, I know I can find these options by using firebug in Firefox or Chrome, which I've done to test the "add to cart" script. However, that's not a proper solution. I need to be able to grab this data from magento based on product id.
Can this really be that hard? Shouldn't this be tied to the data for the product somehow? I've done a var_dump on the catalog_product.info and i see where it asks if there are options but doesn't provide any details on them. Thoughts?
Thanks in advance.
Do you need to get all options ids for specific products or just some particular? For the latter you can use this code:
$productEntity = Mage_Catalog_Model_Product::ENTITY;
$colorAttribute = Mage::getModel('eav/config')->getAttribute($productEntity, 'color');
$colorAttribute->getId();
For the first option code in this question might be useful How to get all super attribute options for a configurable item in Magento

how to add default variants to product in magento

I'm very new to Magento.I am having product with some variants like "size" with values "large XX,small XX,medium XX".I want the feature to make any one of this values as default in admin.
my screenshot explanation here
How can i do this
please help me!
When you say you want them to be defaults, you are being a little vague. Are you talking about using configurable products for this? Custom product options? When you say "default in admin", what do you mean? Which page are you referring to? More detail will help us answer the question.
Thanks,
Joe
As far as I know, what you are asking for is not a default functionality of Magento, but it shouldn't be difficult to add some javascript to allow you to force selections at page load time. Ultimately, it's just code, so it's definitely possible to do.
my suggestion would be to add a attribute to all you products called for instance "defaultselect" then you can enter your value easily from the backend to your configurable product.
so now you have a configurable product say a hat which comes in 3 sizes, and has 3 assigned simple products. set the defaultselect value to one of the size values
now some layout xml and javascript is required to get you product attribute to work with the magento selects.
first you want to actually include you js. this is done through the layout xml. you can include update using add js method for PRODUCT_TYPE_configurable handle
now the js. store the js in the skin js folder.
to make this work you would need to know the configurable product defaultselect attribute value we're testing against. this could be retrieved with your own ajax function to a method which checks if the current product has a defaultselect attribute and returns it's value or false.
if you have a value from your ajax function you can work with that ie you have both the returned attributes from the configurable product and magento will return the rest.
however after that it seems to get complicated your going to have to do something with the js magento aready uses to render product options

Resources