Ad

Shopify Product Doesn't Have Variant But The Id Showed Up In Url

I'm having a strange situation where my product doesn't have any variants but the Url is showing variant id in the product link of the shopping cart...

ex) www.website.com/products/singleProduct?variant=12345678901

I've tried setting enableHistoryState to false,

enableHistoryState: false

and also removing

if (!history.replaceState || !variant) {
    return;
  }

var newurl = window.location.protocol + '//' + window.location.host + window.location.pathname + '?variant=' + variant.id;
window.history.replaceState({path: newurl}, '', newurl);

but these changes don't work.

And I actually don't want to remove these code either because I do have some products with variants. I'm just wondering why the product is showing variant id in the url without having a variant...

Ad

Answer

Every product in shopify have at least one variant.

If you haven't set any variant it's called the "Default" variant.

If you read the documentation here: https://help.shopify.com/themes/liquid/objects/product you will noticed that there is a property called has_only_default_variant that checks if a product has it's default variant. This is what I'm talking about.

Any time you buy a product in Shopify you are buying a variant of that product ( even if that variant is the default one, that you don't see it listed in the variants section ).

You can view your product JSON response buy entering the product page from the admin panel and typing .json after the URL address. So something like so /admin/products/189421092890.json and you will be able to see the default variant for that product.

So if you don't like to show the variant for products with the default variant, just use the check product.has_only_default_variant and modify the href attribute of the link that points to the product page so that it doesn't include the default variant id.

Ad
source: stackoverflow.com
Ad