Created
August 19, 2020 11:24
-
-
Save mohamm6d/e2c0a7c2fa7e8d5343b74700fb19aa6d to your computer and use it in GitHub Desktop.
This gist make tags for Shopify store based on variants
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
require_once dirname(__FILE__) . '/vendor/autoload.php'; | |
class ShopifySdk | |
{ | |
public function __construct() | |
{ | |
$this->username = ''; | |
$this->password = ''; | |
$this->shop = ''; | |
$this->api = 'api/2020-07/'; | |
$this->auth = 'https://' . $this->username . ':' . $this->password . '@' . $this->shop . '.myshopify.com/admin/' . $this->api; | |
} | |
public function getResources($request, $fields = '', $endpoints = '', $limit = 10, $no_pagination = true) | |
{ | |
$request = $this->auth . $request . '.json'; | |
$merged = array(); | |
$page_info = ''; | |
$last_page = false; | |
$limit = '?limit=' . $limit; | |
$debug = 0; | |
while (!$last_page) { | |
$url = $request . $limit . $fields . $endpoints . $page_info; | |
$curl = curl_init(); | |
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_URL, $url); | |
curl_setopt($curl, CURLOPT_HEADERFUNCTION, function ($curl, $header) use (&$headers) { | |
$len = strlen($header); | |
$header = explode(':', $header, 2); | |
if (count($header) >= 2) { | |
$headers[strtolower(trim($header[0]))] = trim($header[1]); | |
} | |
return $len; | |
}); | |
$result = curl_exec($curl); | |
curl_close($curl); | |
if (isset($headers['link'])) { | |
$links = explode(',', $headers['link']); | |
foreach ($links as $link) { | |
$next_page = false; | |
if (strpos($link, 'rel="next"')) { | |
$next_page = $link; | |
} | |
} | |
if ($next_page) { | |
preg_match('~<(.*?)>~', $next_page, $next); | |
$url_components = parse_url($next[1]); | |
parse_str($url_components['query'], $params); | |
$page_info = '&page_info=' . $params['page_info']; | |
$endpoints = ''; // Link pagination does not support endpoints on pages 2 and up | |
} else { | |
$last_page = true; // There's no next page, we're at the last page | |
} | |
} else { | |
$last_page = true; // Couldn't find parameter link in headers, stop loop | |
} | |
$source_array = json_decode($result, true); | |
$merged = array_merge_recursive($merged, $source_array); | |
if ($no_pagination) { | |
$last_page = true; | |
} | |
// Used for debugging to prevent infinite loops, comment to disable | |
//if($debug >= 150) { | |
// //break; | |
//} | |
//$debug++; | |
sleep(1); // Limit calls to 1 per second | |
} | |
return $merged; | |
} | |
public function getTags($productId) | |
{ | |
$fields = '&fields=tags,variants,vendor'; | |
$endpoints = '&published_status=published'; | |
$product = $this->getResources('products/' . $productId, $fields, $endpoints, 250); | |
foreach ($product['product']['variants'] as $variants) { | |
$tags[] = "color_" . str_replace(" ", "", $variants['option1']); | |
$tags[] = "size_" . $variants['option2']; | |
} | |
if ($product['product']['vendor']) { | |
$tags[] = "vendor_" . $product['product']['vendor']; | |
} | |
return array_unique($tags); | |
} | |
public function sendRequest($request, $data) | |
{ | |
$request = $this->auth . $request . '.json'; | |
$curl = curl_init(); | |
curl_setopt($curl, CURLOPT_URL, $request); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_VERBOSE, 0); | |
curl_setopt($curl, CURLOPT_HEADER, 1); | |
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT"); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); | |
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); | |
$response = curl_exec($curl); | |
curl_close($curl); | |
return $response; | |
} | |
public function insertTags($tags, $productId) | |
{ | |
$tags = implode(",", $tags); | |
$params = array('product' => array('id' => $productId, 'tags' => $tags)); | |
return $this->sendRequest('products/' . $productId, $params); | |
} | |
public function getProductIds() | |
{ | |
$fields = '&fields=id'; | |
$endpoints = '&published_status=published'; | |
$products = $this->getResources('products', $fields, $endpoints, 250); | |
$productIds = []; | |
array_map(function ($product) { | |
$productIds[] = $product['id']; | |
}, $products['products']); | |
file_put_contents('ids.txt', implode("\r\n", $productIds)); | |
} | |
} | |
$ShopifySdk = new ShopifySdk(); | |
$ids = explode("\r\n", file_get_contents("./ids_bk.txt")); | |
if ($ids[$_GET['page']]) { | |
$tags = $ShopifySdk->getTags($ids[$_GET['page']]); | |
$result = $ShopifySdk->insertTags($tags, $ids[$_GET['page']]); | |
echo $ids[$_GET['page']] . ' done<br>'; | |
?> | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Document</title> | |
</head> | |
<body> | |
<a href="?page=<?=($_GET['page'] + 1)?>">Next page</a> | |
<script> | |
setTimeout(function(){ | |
window.location.href='?page=<?=($_GET['page'] + 1)?>'; | |
},2000); | |
</script> | |
</body> | |
</html> | |
<? | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment