· 9 years ago · Dec 13, 2016, 08:26 AM
1<?php
2
3require_once 'abstract.php';
4
5class MassiveProductDeletion extends Mage_Shell_Abstract
6{
7 protected $idFrom = null;
8 protected $idTo = null;
9
10 /**
11 * Function try to remove products with IDs in range provided by user
12 */
13 public function run()
14 {
15 try {
16 if ($this->getArg('help')) {
17 echo $this->usageHelp();
18 die();
19 }
20
21 $this->getParameters();
22 $this->checkParameters();
23 $this->deleteProducts();
24 } catch (Exception $ex) {
25 $this->returnError($ex);
26 }
27 }
28
29 /**
30 * Function deletes products with IDs in $idFrom to $idTo range
31 */
32 private function deleteProducts()
33 {
34 Mage::app();
35 $this->createBackupTable();
36
37 $deletedProducts = 0;
38
39 for ($i = $this->idFrom; $i <= $this->idTo; $i++) {
40 try {
41 $product = Mage::getModel('catalog/product')->load($i);
42 // If product with this ID has no SKU it doesn't exists, ignore it
43 if(!$product->getSku()){
44 continue;
45 }
46
47 $this->backupProduct($product);
48 $product->delete();
49
50 $deletedProducts++;
51 } catch (Exception $ex) {
52 $this->returnError($ex);
53 }
54 }
55
56 echo $deletedProducts . ' products deleted successfully';
57 }
58
59 /**
60 * Function checks if table products_backup_table exists, if not creates it
61 */
62 private function createBackupTable()
63 {
64 $query = "CREATE TABLE IF NOT EXISTS `products_backup_table` (
65 `id` int(11) NOT NULL AUTO_INCREMENT,
66 `sku` varchar(64) CHARACTER SET utf8,
67 `entity_id` int(10) NOT NULL,
68 `store_id` varchar(256) CHARACTER SET utf8,
69 `product_type` varchar(256) CHARACTER SET utf8,
70 `name` varchar(256) CHARACTER SET utf8,
71 `description` varchar(256) CHARACTER SET utf8,
72 `short_description` varchar (256) CHARACTER SET utf8,
73 `url_key` varchar (256) CHARACTER SET utf8,
74 `url_path` varchar (256) CHARACTER SET utf8,
75 `image_url` varchar (256) CHARACTER SET utf8,
76 PRIMARY KEY (`id`)
77 );";
78
79 $this->executeQuery($query);
80 }
81
82 /**
83 * Function connects to database and executes query
84 *
85 * @param $query
86 */
87 private function executeQuery($query)
88 {
89 $singleton = Mage::getSingleton('core/resource');
90 $connection = $singleton->getConnection('core_write');
91
92 $connection->query($query);
93 }
94
95 /**
96 * Function adds products information to deleted_products_backup table
97 *
98 * @param $product
99 */
100 private function backupProduct(Mage_Catalog_Model_Product $product)
101 {
102 $data = $product->getData();
103 $imageUrl = $product->getImageUrl();
104 $storeIDs = serialize($product->getStoreIds());
105
106 $query = "INSERT INTO `products_backup_table`
107 (`sku`, `entity_id`, `store_id`, `product_type`, `name`, `description`, `short_description`, `url_key`, `url_path`, `image_url`)
108 VALUES ('{$data['sku']}', '{$data['entity_id']}', '{$storeIDs}', '{$data['type_id']}', '{$data['name']}', '{$data['description']}',
109 '{$data['short_description']}', '{$data['url_key']}', '{$data['url_path']}', '{$imageUrl}');";
110
111 $this->executeQuery($query);
112 }
113
114 /**
115 * Function get parameters provided by user
116 */
117 private function getParameters()
118 {
119 $this->idFrom = $this->getArg('product-entity-from');
120 $this->idTo = $this->getArg('product-entity-to');
121
122 if (!$this->idFrom) {
123 throw new Exception('product-entity-from parameter not provided');
124 }
125
126 if (!$this->idTo) {
127 throw new Exception('product-entity-to parameter not provided');
128 }
129 }
130
131 /**
132 * Function checks if parameters provided by client are correct
133 */
134 private function checkParameters()
135 {
136 if (!$this->isInteger($this->idFrom)) {
137 throw new Exception('product-entity-from parameter is not integer');
138 }
139
140 if (!$this->isInteger($this->idTo)) {
141 throw new Exception('product-entity-to parameter is not integer');
142 }
143
144 if ($this->idFrom > $this->idTo) {
145 throw new Exception('product-entity-from parameter is larger than product-entity-to parameter');
146 }
147 }
148
149 /**
150 * Function check if provided value is integer
151 *
152 * @param $value
153 *
154 * @return true if $value is integer, otherwise return false
155 */
156 private function isInteger($value)
157 {
158 return (ctype_digit(strval($value)));
159 }
160
161 /**
162 * Function return exception message on screen
163 *
164 * @param Exception $ex
165 */
166 private function returnError(Exception $ex)
167 {
168 echo PHP_EOL . 'Error: ' . $ex->getMessage() . PHP_EOL;
169 }
170
171 /**
172 * Function return correct structure of shell script usage and its parameters
173 */
174 public function usageHelp()
175 {
176 return <<<USAGE
177
178Usage: php delete_product.php --product-entity-from 1000 --product-entity-to 15000
179 --product-entity-from Product ID which script should start delete procedure
180 --product-entity-to Product ID which script should finish delete procedure
181
182USAGE;
183 }
184}
185
186$shell = new MassiveProductDeletion();
187$shell->run();