Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
All Data Structures Namespaces Files Functions Variables Pages
website_attribute_sync.php
Go to the documentation of this file.
1 <?php
7 use \Magento\Store\Model\Website;
8 use \Magento\Store\Model\ResourceModel\Website as WebsiteResourceModel;
9 use \Magento\Store\Model\Group;
13 use \Magento\Catalog\Api\ProductRepositoryInterface;
14 use \Magento\Framework\App\ResourceConnection;
15 use \Magento\Catalog\Model\Product\Attribute\Source\Status as AttributeStatus;
16 use \Magento\Catalog\Model\Product;
18 use \Magento\TestFramework\Helper\Bootstrap;
19 
26 $productId = 333;
27 $objectManager = Bootstrap::getObjectManager();
28 $websiteResourceModel = $objectManager->get(WebsiteResourceModel::class);
29 $storeGroupResourceModel = $objectManager->get(GroupResourceModel::class);
30 $storeResourceModel = $objectManager->get(StoreResourceModel::class);
31 $productRepository = $objectManager->create(ProductRepositoryInterface::class);
32 $resourceConnection = $objectManager->get(ResourceConnection::class);
33 $flagManager = $objectManager->get(\Magento\Framework\FlagManager::class);
34 $productLinkField = $objectManager->get(\Magento\Framework\EntityManager\MetadataPool::class)
35  ->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class)
36  ->getLinkField();
37 
45 $website = $objectManager->create(Website::class);
46 $website->setName('custom website for av test')
47  ->setCode('customwebsite1');
48 $website->isObjectNew(true);
50 
51 
59 $storeGroup = $objectManager->create(Group::class);
60 $storeGroup->setCode('customstoregroup1')
61  ->setName('custom store group for av test')
62  ->setWebsite($website);
63 
65 
66 $website->setDefaultGroupId($storeGroup->getId());
68 
75 $storeOne = $objectManager->create(Store::class);
76 $storeOne->setName('custom store for av test')
77  ->setCode('customstoreview1')
78  ->setGroup($storeGroup);
79 $storeOne->setWebsite($website);
80 
82 
83 $storeGroup->setDefaultStoreId($storeOne->getId());
85 
89 $storeTwo = $objectManager->create(Store::class);
90 $storeTwo->setName('custom store for av test 2')
91  ->setCode('customstoreview2')
92  ->setGroup($storeGroup);
93 $storeTwo->setWebsite($website);
96  $storeOne,
97  $storeTwo,
98 ];
99 
101  ->create(\Magento\CatalogSearch\Model\Indexer\Fulltext\Processor::class)
102  ->reindexAll();
103 
109 $product = $objectManager->create(Product::class);
110 $product->isObjectNew(true);
111 $product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
112  ->setId($productId)
113  ->setAttributeSetId(4)
114  ->setName('Simple Product custom')
115  ->setSku('simplecustomproduct')
116  ->setTaxClassId('none')
117  ->setDescription('description')
118  ->setShortDescription('short description')
119  ->setOptionsContainer('container1')
120  ->setMsrpDisplayActualPriceType(\Magento\Msrp\Model\Product\Attribute\Source\Type::TYPE_IN_CART)
121  ->setPrice(10)
122  ->setWeight(1)
123  ->setMetaTitle('meta title')
124  ->setMetaKeyword('meta keyword')
125  ->setMetaDescription('meta description')
126  ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
127  ->setStatus(AttributeStatus::STATUS_ENABLED)
128  ->setWebsiteIds([$website->getId()])
129  ->setCategoryIds([])
130  ->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1]);
131 
133 
134 
138 foreach ($stores as $store) {
139  $product->addAttributeUpdate('status', AttributeStatus::STATUS_ENABLED, $store->getId());
140 }
141 
145 $connection = $resourceConnection->getConnection();
146 $connection->query(
147  sprintf(
148  'UPDATE %s SET `value` = %d ' .
149  'WHERE `%s` = %d ' .
150  'AND `store_id`= %d ' .
151  'AND `attribute_id` = ' .
152  '(SELECT `ea`.`attribute_id` FROM %s ea WHERE `ea`.`attribute_code` = "status" LIMIT 1)',
153  $resourceConnection->getTableName('catalog_product_entity_int'),
154  AttributeStatus::STATUS_DISABLED,
156  $product->getData($productLinkField),
157  $storeOne->getId(),
158  $resourceConnection->getTableName('eav_attribute')
159  )
160 );
161 
165 $flagManager->saveFlag(
166  WebsiteAttributesSynchronizer::FLAG_NAME,
167  WebsiteAttributesSynchronizer::FLAG_REQUIRES_SYNCHRONIZATION
168 );
$storeGroupResourceModel
foreach($stores as $store) $connection