forked from m2epro/magento2-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Category.php
68 lines (52 loc) · 2.25 KB
/
Category.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
<?php
/*
* @author M2E Pro Developers Team
* @copyright M2E LTD
* @license Commercial use is forbidden
*/
namespace Ess\M2ePro\Observer;
class Category extends AbstractModel
{
//########################################
public function process()
{
/** @var \Magento\Catalog\Model\Category $category */
$category = $this->getEventObserver()->getData('category');
$categoryId = (int)$category->getId();
$websiteId = (int)$category->getStore()->getWebsiteId();
$changedProductsIds = $this->getEventObserver()->getData('product_ids');
$postedProductsIds = array_keys($this->getEventObserver()->getData('category')->getData('posted_products'));
if (!is_array($changedProductsIds) || count($changedProductsIds) <= 0) {
return;
}
$websitesProductsIds = array(
// website for default store view
0 => $changedProductsIds
);
if ($websiteId == 0) {
foreach ($changedProductsIds as $productId) {
$productModel = $this->modelFactory->getObject('Magento\Product')->setProductId($productId);
foreach ($productModel->getWebsiteIds() as $websiteId) {
$websitesProductsIds[$websiteId][] = $productId;
}
}
} else {
$websitesProductsIds[$websiteId] = $changedProductsIds;
}
foreach ($websitesProductsIds as $websiteId => $productIds) {
foreach ($productIds as $productId) {
/** @var \Magento\Catalog\Model\Product $product */
$product = $this->getHelper('Magento\Product')->getCachedAndLoadedProduct($productId);
/** @var \Ess\M2ePro\Model\Listing\Auto\Actions\Mode\Category $object */
$object = $this->modelFactory->getObject('Listing\Auto\Actions\Mode\Category');
$object->setProduct($product);
if (in_array($productId,$postedProductsIds)) {
$object->synchWithAddedCategoryId($categoryId,$websiteId);
} else {
$object->synchWithDeletedCategoryId($categoryId,$websiteId);
}
}
}
}
//########################################
}