-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcategory_operations.php
180 lines (151 loc) · 6.41 KB
/
category_operations.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
// Change current directory to the directory of current script
chdir(dirname(__FILE__));
require_once '../abstract.php';
class Codealist_Shell_CategoryOperations extends Mage_Shell_Abstract
{
/**
* Run script
*
*/
public function run()
{
try {
if ($this->getArg('moveCategory')) {
$this->moveCategory();
} elseif ($this->getArg('moveProducts')) {
$this->moveCategoryProducts();
} elseif ($this->getArg('removeProducts')) {
$this->removeCategoryProducts();
} elseif ($this->getArg('status')) {
$this->countCategoryProducts();
}
} catch (Exception $e) {
echo "Error occurred: {$e->getMessage()}";
}
}
/**
* Move a Category inside another one
*/
protected function moveCategory()
{
if ($this->getArg('category') && $this->getArg('category-parent')) {
$categoryId = $this->getArg('category');
$categoryParentId = $this->getArg('category-parent');
$category = Mage::getModel('catalog/category')->load($categoryId);
if (!$category) {
echo "Provided category child ID doesn't belong to a category";
exit;
}
$categoryParent = Mage::getModel('catalog/category')->load($categoryParentId);
if (!$categoryParent) {
echo "Provided category parent ID doesn't belong to a category";
exit;
}
Mage::getSingleton('catalog/category_api')->move($categoryId, $categoryParentId);
echo "Category \"{$category->getName()}\" moved inside of category \"{$categoryParent->getName()}\"";
} else {
echo "No category and/or category-parent argument provided";
}
}
/**
* Move every product from a category to another one
*/
protected function moveCategoryProducts()
{
if ($this->getArg('category-origin') && $this->getArg('category-destiny')) {
$categoryOriginId = $this->getArg('category-origin');
$categoryDestinyId = $this->getArg('category-destiny');
$categoryOrigin = Mage::getModel('catalog/category')->load($categoryOriginId);
if (!$categoryOrigin) {
echo "Provided category origin ID doesn't belong to a category";
exit;
}
$categoryDestiny = Mage::getModel('catalog/category')->load($categoryDestinyId);
if (!$categoryDestiny) {
echo "Provided category destiny ID doesn't belong to a category";
exit;
}
$products = Mage::getResourceModel('catalog/product_collection')
->setStoreId(Mage::app()->getStore()->getId())
->addCategoryFilter($categoryOrigin);
foreach ($products as $product) {
Mage::getSingleton('catalog/category_api')->removeProduct($categoryOriginId, $product->getId());
Mage::getSingleton('catalog/category_api')->assignProduct($categoryDestinyId, $product->getId());
}
echo count($products) . " Products were moved from \"{$categoryOrigin->getName()}\" to \"{$categoryDestiny->getName()}\"";
} else {
echo "No category-origin and/or category-destiny argument provided";
}
}
/**
* Move every product from a category to another one
*/
protected function removeCategoryProducts()
{
if ($this->getArg('category')) {
$categoryId = $this->getArg('category');
$category = Mage::getModel('catalog/category')->load($categoryId);
if (!$category) {
echo "Provided category ID doesn't belong to a category";
exit;
}
$products = Mage::getResourceModel('catalog/product_collection')
->setStoreId(Mage::app()->getStore()->getId())
->addCategoryFilter($categoryId);
foreach ($products as $product) {
Mage::getSingleton('catalog/category_api')->removeProduct($categoryId, $product->getId());
}
echo count($products) . " Products were removed from \"{$categoryOrigin->getName()}\"";
} else {
echo "No category argument provided";
}
}
/**
* Count the category products
*/
protected function countCategoryProducts()
{
if ($this->getArg('category')) {
$categoryId = $this->getArg('category');
$category = Mage::getModel('catalog/category')->load($categoryId);
if (!$category) {
echo "Provided category ID doesn't belong to a category";
exit;
}
$products = Mage::getResourceModel('catalog/product_collection')
->setStoreId(Mage::app()->getStore()->getId())
->addCategoryFilter($category);
echo "Category \"{$category->getName()}\" has " . count($products) . " products associated.";
} else {
echo "No category argument provided";
}
}
/**
* Retrieve Usage Help Message
*
*/
public function usageHelp()
{
return <<<USAGE
Usage: php -f test.php -- [options]
php -f test.php -- status --category 8
php -f test.php -- moveCategory --category-origin 8 --category-destiny 9
php -f test.php -- moveProducts --category-origin 8 --category-destiny 9
php -f test.php -- removeProducts --category 8
status Display the count of products associated to a category
--category <Category ID> Save log, days. (Minimum 1 day, if defined - ignoring system value)
moveCategory Move products from a category to another
--category <Category ID> Category you want to move
--category-parent <Category ID> Parent Category
moveProducts Move products from a category to another
--category-origin <Category ID> Category origin from where you want to move products
--category-destiny <Category ID> Category destiny where you want to move products
removeProducts Move products from a category to another
--category <Category ID> Category to clean up
help This help
USAGE;
}
}
$shell = new Codealist_Shell_CategoryOperations();
$shell->run();