What this article will cover
- Variants in Shopify & default ordering
- Why lowest price variant at top ?
- Checking for a products variants
- Re-positioning with API
Pre requisite : You will require Shopify general knowledge, which includes knowledge about shopify store, products and variants. Variants can be simply understood as a product available in different types/styles.
In shopify and other eCommerce platform, a product in various forms or types is general thing. For eg. a PC Monitor in a store might come in various color or sizes. And with those variations in those attributes different pricing can be set. Below is the screenshot of a variant of a product.
These variants ordering is random and the one appearing at top appears as the product price on product listing page and product detail page. The business need i encountered was to show the lowest price on listing price.
Convention : I have used this Shopify private app skeleton https://github.com/phpish/shopify_private_app-skeleton.git for API requests. This has managed things better than we do manually.
So i wrote a script that would check figure out all the products which don’t have lowest price variant at top. With the script below i could check all the products whose default variant price isn’t lowest among other.
<?php session_start(); require __DIR__.'/vendor/autoload.php'; use phpish\shopify; require __DIR__.'/conf.php'; $shopify = shopify\client(SHOPIFY_SHOP, SHOPIFY_APP_API_KEY, SHOPIFY_APP_PASSWORD, true); try { $products = $shopify('GET /admin/products/count.json', array('published_status'=>'published')); $totalproducts = $shopify('GET /admin/products/count.json', array('published_status'=>'published')); $limit = 50; $totalpage = ceil($totalproducts/$limit); for($i=1; $i<=$totalpage; $i++){ $products = $shopify('GET /admin/products.json?limit=50&page='.$i, array('published_status'=>'published')); foreach($products as $product){ $product = (object)$product; if(count($product->variants)>1){ $defaultprice = $product->variants; $defaultprice = $defaultprice[0]['price']; //first variant price //loop through variants and find if any of the variants has lower price than first variant price foreach ($product->variants as $variant) { if($variant['price']<$defaultprice){ echo $product->title.'-----'.$product->handle.'<br/>'; break; } } } } } } catch (shopify\ApiException $e) { # HTTP status code was >= 400 or response contained the key 'errors' echo $e; print_R($e->getRequest()); print_R($e->getResponse()); } catch (shopify\CurlException $e) { # cURL error echo $e; print_R($e->getRequest()); print_R($e->getResponse()); } ?>
The logic to update the lowest price variant is, the first lowest price variant position to set to position 1. I first tried updating the position of the first lowest price variant to position 1. Hoping that other variants position would adjust as accordingly. But doing that it ended up having two variants having position 1. i.e. other variants position didn’t adjust as accordingly, So now i updated other variants position manually.
<?php session_start(); require __DIR__.'/vendor/autoload.php'; use phpish\shopify; require __DIR__.'/conf.php'; $shopify = shopify\client(SHOPIFY_SHOP, SHOPIFY_APP_API_KEY, SHOPIFY_APP_PASSWORD, true); try { $totalproducts = $shopify('GET /admin/products/count.json', array('published_status'=>'published')); $limit = 50; $totalpage = ceil($totalproducts/$limit); for($i=1; $i<=$totalpage; $i++){ $products = $shopify('GET /admin/products.json?limit=50&page='.$i, array('published_status'=>'published')); foreach($products as $product){ $product = (object)$product; if(count($product->variants)>1){ $defaultprice = $product->variants; $defaultprice = $defaultprice[0]['price']; //first variant price $lowestvarientpriceid = ''; //loop through variants and find if any of the variants has lower price than first variant price foreach ($product->variants as $variant) { if($variant['price']<$defaultprice){ //echo $product->title.'xxx'.$product->handle.'<br/>'; $defaultprice = $variant['price']; $lowestvarientpriceid = $variant['id']; } } if($lowestvarientpriceid!=null){ $j=2; foreach ($product->variants as $variant) { if($variant['id']==$lowestvarientpriceid){ //set position 1; $requestx = $shopify('PUT /admin/variants/'.$lowestvarientpriceid.'.json',array(),array('variant'=>array('id'=>$lowestvarientpriceid,'position'=>1))); }else{ $requestx = $shopify('PUT /admin/variants/'.$variant['id'].'.json',array(),array('variant'=>array('id'=>$variant['id'],'position'=>$j))); $j++; } } } } } } } catch (shopify\ApiException $e) { # HTTP status code was >= 400 or response contained the key 'errors' echo $e; print_R($e->getRequest()); print_R($e->getResponse()); } catch (shopify\CurlException $e) { # cURL error echo $e; print_R($e->getRequest()); print_R($e->getResponse()); } ?>
Now this is the result i obtained. the lowest first price variant position appeared at top.
And now for reconfirmation among all the products i ran the script that would check if any product don’t have a lowest variant at position 1, then i found it to be null.
Leave a Reply