Last active
February 6, 2018 08:28
-
-
Save hussnainsheikh/3e11484e30a47c518fcd0500621817bb to your computer and use it in GitHub Desktop.
Simple PHP script to delete all products from Shopify store using a private App(Api key and Pass). Just replace your App info and enjoy. Thanks
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
<?php | |
/** | |
* | |
*/ | |
class ShopifyProducts{ | |
public function getProducts($count = false, $page){ | |
$apikey = 'API_KEY'; //Replace with your's | |
$pass = 'PASS'; //Replace with your's | |
$store = 'STORE_URL'; //Replace with your's | |
if ($count == true) { | |
$products_count = file_get_contents('https://'.$apikey.':'.$pass.'@'.$store.'/admin/products/count.json'); | |
$data = json_decode($products_count); | |
return $data->count; | |
} | |
$products = file_get_contents('https://'.$apikey.':'.$pass.'@'.$store.'/admin/products.json?limit=250&page='.$page); | |
$data = json_decode($products); | |
return $data; | |
} | |
} | |
$obj = new ShopifyProducts(); | |
$count = $obj->getProducts(true, null); | |
$pages = ceil((int)$count/250); | |
echo "<br>Total no of pages: ".$pages; | |
for ($i=1; $i <= $pages; $i++) { | |
echo "<br>Deleting products of Page No.: ". $i; | |
$products = $obj->getProducts(false, $i); | |
foreach ($products->products as $index => $product) { | |
echo "<br>"; | |
print_r($product->id); | |
$url = "https://API_KEY:PASS@STORE_URL/admin/products/".$product->id.".json"; //Replace with your's | |
//Initiate cURL | |
$ch = curl_init($url); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
$response = curl_exec($ch); | |
echo "<br><h1>Response</h1>"; | |
var_dump($response); | |
curl_close($ch); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment