Skip to content

Commit 0a51fa3

Browse files
committed
MAGETWO-55038: Add CategoryProductLinkInterface as Extension attributes to ProductInterface entity
2 parents dd07ac6 + 1f2c1ce commit 0a51fa3

File tree

15 files changed

+773
-89
lines changed

15 files changed

+773
-89
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\Model\Product\Website;
7+
8+
use Magento\Catalog\Api\Data\ProductInterface;
9+
use Magento\Catalog\Model\ResourceModel\Product\Website\Link as ProductWebsiteLink;
10+
use Magento\Framework\EntityManager\Operation\ExtensionInterface;
11+
12+
class ReadHandler implements ExtensionInterface
13+
{
14+
/** @var ProductWebsiteLink */
15+
private $productWebsiteLink;
16+
17+
/**
18+
* ReadHandler constructor.
19+
* @param ProductWebsiteLink $resourceModel
20+
*/
21+
public function __construct(
22+
ProductWebsiteLink $productWebsiteLink
23+
) {
24+
$this->productWebsiteLink = $productWebsiteLink;
25+
}
26+
27+
/**
28+
* @param ProductInterface $product
29+
* @param array $arguments
30+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
31+
* @return ProductInterface
32+
*/
33+
public function execute($product, $arguments = [])
34+
{
35+
if ($product->getExtensionAttributes()->getWebsiteIds() !== null) {
36+
return $product;
37+
}
38+
$websiteIds = $this->productWebsiteLink->getWebsiteIdsByProductId($product->getId());
39+
40+
$extensionAttributes = $product->getExtensionAttributes();
41+
$extensionAttributes->setWebsiteIds($websiteIds);
42+
$product->setExtensionAttributes($extensionAttributes);
43+
44+
return $product;
45+
}
46+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\Model\Product\Website;
7+
8+
use Magento\Catalog\Api\Data\ProductInterface;
9+
use Magento\Catalog\Model\ResourceModel\Product\Website\Link as ProductWebsiteLink;
10+
use Magento\Framework\EntityManager\Operation\ExtensionInterface;
11+
use Magento\Store\Model\StoreManagerInterface;
12+
13+
/**
14+
* Class SaveHandler
15+
* @package Magento\Catalog\Model\Product\Website
16+
*/
17+
class SaveHandler implements ExtensionInterface
18+
{
19+
/** @var ProductWebsiteLink */
20+
private $productWebsiteLink;
21+
22+
/** @var StoreManagerInterface */
23+
private $storeManager;
24+
25+
/**
26+
* SaveHandler constructor.
27+
* @param ProductWebsiteLink $productWebsiteLink
28+
* @param StoreManagerInterface $storeManager
29+
*/
30+
public function __construct(
31+
ProductWebsiteLink $productWebsiteLink,
32+
StoreManagerInterface $storeManager
33+
) {
34+
$this->productWebsiteLink = $productWebsiteLink;
35+
$this->storeManager = $storeManager;
36+
}
37+
38+
/**
39+
* Get website ids from extension attributes and persist them
40+
* @param ProductInterface $product
41+
* @param array $arguments
42+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
43+
* @return ProductInterface
44+
*/
45+
public function execute($product, $arguments = [])
46+
{
47+
if ($this->storeManager->isSingleStoreMode()) {
48+
$defaultWebsiteId = $this->storeManager->getDefaultStoreView()->getWebsiteId();
49+
$websiteIds = [$defaultWebsiteId];
50+
} else {
51+
$extensionAttributes = $product->getExtensionAttributes();
52+
$websiteIds = $extensionAttributes->getWebsiteIds();
53+
}
54+
55+
if ($websiteIds !== null) {
56+
$this->productWebsiteLink->saveWebsiteIds($product, $websiteIds);
57+
}
58+
59+
return $product;
60+
}
61+
}

app/code/Magento/Catalog/Model/ProductRepository.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,6 @@ protected function initializeProductData(array $productData, $createNew)
303303
private function assignProductToWebsites(\Magento\Catalog\Model\Product $product)
304304
{
305305
if (!$this->storeManager->hasSingleStore()) {
306-
307306
if ($this->storeManager->getStore()->getCode() == \Magento\Store\Model\Store::ADMIN_CODE) {
308307
$websiteIds = array_keys($this->storeManager->getWebsites());
309308
} else {

app/code/Magento/Catalog/Model/ResourceModel/Product.php

Lines changed: 24 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
namespace Magento\Catalog\Model\ResourceModel;
77

88
use Magento\Catalog\Api\Data\ProductInterface;
9+
use Magento\Catalog\Model\ResourceModel\Product\Website\Link as ProductWebsiteLink;
10+
use Magento\Framework\App\ObjectManager;
911

1012
/**
1113
* Product entity resource model
@@ -173,28 +175,19 @@ protected function _getDefaultAttributes()
173175
/**
174176
* Retrieve product website identifiers
175177
*
178+
* @deprecated
176179
* @param \Magento\Catalog\Model\Product|int $product
177180
* @return array
178181
*/
179182
public function getWebsiteIds($product)
180183
{
181-
$connection = $this->getConnection();
182-
183184
if ($product instanceof \Magento\Catalog\Model\Product) {
184185
$productId = $product->getEntityId();
185186
} else {
186187
$productId = $product;
187188
}
188189

189-
$select = $connection->select()->from(
190-
$this->getProductWebsiteTable(),
191-
'website_id'
192-
)->where(
193-
'product_id = ?',
194-
(int)$productId
195-
);
196-
197-
return $connection->fetchCol($select);
190+
return $this->getProductWebsiteLink()->getWebsiteIdsByProductId($productId);
198191
}
199192

200193
/**
@@ -299,48 +292,26 @@ public function delete($object)
299292
/**
300293
* Save product website relations
301294
*
295+
* @deprecated
302296
* @param \Magento\Catalog\Model\Product $product
303297
* @return $this
304298
*/
305299
protected function _saveWebsiteIds($product)
306300
{
307-
if ($this->_storeManager->isSingleStoreMode()) {
308-
$id = $this->_storeManager->getDefaultStoreView()->getWebsiteId();
309-
$product->setWebsiteIds([$id]);
310-
}
311-
$websiteIds = $product->getWebsiteIds();
312-
313-
$oldWebsiteIds = [];
314-
315-
$product->setIsChangedWebsites(false);
316-
317-
$connection = $this->getConnection();
318-
319-
$oldWebsiteIds = $this->getWebsiteIds($product);
320-
321-
$insert = array_diff($websiteIds, $oldWebsiteIds);
322-
$delete = array_diff($oldWebsiteIds, $websiteIds);
323-
324-
if (!empty($insert)) {
325-
$data = [];
326-
foreach ($insert as $websiteId) {
327-
$data[] = ['product_id' => (int)$product->getEntityId(), 'website_id' => (int)$websiteId];
301+
if ($product->hasWebsiteIds()) {
302+
if ($this->_storeManager->isSingleStoreMode()) {
303+
$id = $this->_storeManager->getDefaultStoreView()->getWebsiteId();
304+
$product->setWebsiteIds([$id]);
328305
}
329-
$connection->insertMultiple($this->getProductWebsiteTable(), $data);
330-
}
306+
$websiteIds = $product->getWebsiteIds();
307+
$product->setIsChangedWebsites(false);
308+
$changed = $this->getProductWebsiteLink()->saveWebsiteIds($product, $websiteIds);
331309

332-
if (!empty($delete)) {
333-
foreach ($delete as $websiteId) {
334-
$condition = ['product_id = ?' => (int)$product->getEntityId(), 'website_id = ?' => (int)$websiteId];
335-
336-
$connection->delete($this->getProductWebsiteTable(), $condition);
310+
if ($changed) {
311+
$product->setIsChangedWebsites(true);
337312
}
338313
}
339314

340-
if (!empty($insert) || !empty($delete)) {
341-
$product->setIsChangedWebsites(true);
342-
}
343-
344315
return $this;
345316
}
346317

@@ -654,6 +625,16 @@ private function getEntityManager()
654625
}
655626

656627
/**
628+
* @deprecated
629+
* @return ProductWebsiteLink
630+
*/
631+
private function getProductWebsiteLink()
632+
{
633+
return ObjectManager::getInstance()->get(ProductWebsiteLink::class);
634+
}
635+
636+
/**
637+
* @deprecated
657638
* @return \Magento\Catalog\Model\ResourceModel\Product\CategoryLink
658639
*/
659640
private function getProductCategoryLink()
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\Model\ResourceModel\Product\Website;
7+
8+
use Magento\Catalog\Api\Data\ProductInterface;
9+
use Magento\Framework\App\ResourceConnection;
10+
use Magento\Framework\EntityManager\MetadataPool;
11+
12+
class Link
13+
{
14+
/** @var ResourceConnection */
15+
private $resourceConnection;
16+
17+
/**
18+
* Link constructor.
19+
* @param ResourceConnection $resourceConnection
20+
*/
21+
public function __construct(
22+
ResourceConnection $resourceConnection
23+
) {
24+
$this->resourceConnection = $resourceConnection;
25+
}
26+
27+
/**
28+
* Retrieve associated with product websites ids
29+
* @param int $productId
30+
* @return array
31+
*/
32+
public function getWebsiteIdsByProductId($productId)
33+
{
34+
$connection = $this->resourceConnection->getConnection();
35+
36+
$select = $connection->select()->from(
37+
$this->getProductWebsiteTable(),
38+
'website_id'
39+
)->where(
40+
'product_id = ?',
41+
(int) $productId
42+
);
43+
44+
return $connection->fetchCol($select);
45+
}
46+
47+
/**
48+
* Return true - if websites was changed, and false - if not
49+
* @param ProductInterface $product
50+
* @param array $websiteIds
51+
* @return bool
52+
*/
53+
public function saveWebsiteIds(ProductInterface $product, array $websiteIds)
54+
{
55+
$connection = $this->resourceConnection->getConnection();
56+
57+
$oldWebsiteIds = $this->getWebsiteIdsByProductId($product->getId());
58+
$insert = array_diff($websiteIds, $oldWebsiteIds);
59+
$delete = array_diff($oldWebsiteIds, $websiteIds);
60+
61+
if (!empty($insert)) {
62+
$data = [];
63+
foreach ($insert as $websiteId) {
64+
$data[] = ['product_id' => (int) $product->getId(), 'website_id' => (int) $websiteId];
65+
}
66+
$connection->insertMultiple($this->getProductWebsiteTable(), $data);
67+
}
68+
69+
if (!empty($delete)) {
70+
foreach ($delete as $websiteId) {
71+
$condition = ['product_id = ?' => (int) $product->getId(), 'website_id = ?' => (int) $websiteId];
72+
$connection->delete($this->getProductWebsiteTable(), $condition);
73+
}
74+
}
75+
76+
if (!empty($insert) || !empty($delete)) {
77+
return true;
78+
}
79+
80+
return false;
81+
}
82+
83+
/**
84+
* @return string
85+
*/
86+
private function getProductWebsiteTable()
87+
{
88+
return $this->resourceConnection->getTableName('catalog_product_website');
89+
}
90+
}

0 commit comments

Comments
 (0)