|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +use Magento\Catalog\Api\Data\ProductInterfaceFactory; |
| 9 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 10 | +use Magento\Catalog\Model\Product\Attribute\Source\Status; |
| 11 | +use Magento\Catalog\Model\Product\Visibility; |
| 12 | +use Magento\CatalogRule\Api\CatalogRuleRepositoryInterface; |
| 13 | +use Magento\CatalogRule\Model\Indexer\IndexBuilder; |
| 14 | +use Magento\CatalogRule\Model\Rule; |
| 15 | +use Magento\CatalogRule\Model\RuleFactory; |
| 16 | +use Magento\Customer\Model\Group; |
| 17 | +use Magento\Store\Api\WebsiteRepositoryInterface; |
| 18 | +use Magento\TestFramework\Helper\Bootstrap; |
| 19 | +use Magento\TestFramework\Workaround\Override\Fixture\Resolver; |
| 20 | + |
| 21 | +Resolver::getInstance()->requireDataFixture('Magento/Store/_files/second_website_with_two_stores.php'); |
| 22 | + |
| 23 | +$objectManager = Bootstrap::getObjectManager(); |
| 24 | +/** @var WebsiteRepositoryInterface $websiteRepository */ |
| 25 | +$websiteRepository = $objectManager->get(WebsiteRepositoryInterface::class); |
| 26 | +/** @var IndexBuilder $indexBuilder */ |
| 27 | +$indexBuilder = $objectManager->get(IndexBuilder::class); |
| 28 | +/** @var CatalogRuleRepositoryInterface $catalogRuleRepository */ |
| 29 | +$catalogRuleRepository = $objectManager->get(CatalogRuleRepositoryInterface::class); |
| 30 | +/** @var ProductInterfaceFactory $productFactory */ |
| 31 | +$productFactory = $objectManager->get(ProductInterfaceFactory::class); |
| 32 | +/** @var ProductRepositoryInterface $productRepository */ |
| 33 | +$productRepository = $objectManager->get(ProductRepositoryInterface::class); |
| 34 | +/** @var RuleFactory $ruleFactory */ |
| 35 | +$ruleFactory = $objectManager->get(RuleFactory::class); |
| 36 | + |
| 37 | +$secondWebsite = $websiteRepository->get('test'); |
| 38 | +$product = $productFactory->create(); |
| 39 | +$product->setTypeId('simple') |
| 40 | + ->setAttributeSetId($product->getDefaultAttributeSetId()) |
| 41 | + ->setWebsiteIds([1, $secondWebsite->getId()]) |
| 42 | + ->setName('Simple Product') |
| 43 | + ->setSku('simple') |
| 44 | + ->setPrice(50) |
| 45 | + ->setVisibility(Visibility::VISIBILITY_BOTH) |
| 46 | + ->setStatus(Status::STATUS_ENABLED) |
| 47 | + ->setStockData( |
| 48 | + [ |
| 49 | + 'use_config_manage_stock' => 1, |
| 50 | + 'qty' => 100, |
| 51 | + 'is_qty_decimal' => 0, |
| 52 | + 'is_in_stock' => 1, |
| 53 | + ] |
| 54 | + ); |
| 55 | +$productRepository->save($product); |
| 56 | +/** @var Rule $rule */ |
| 57 | +$catalogRule = $ruleFactory->create(); |
| 58 | +$catalogRule->loadPost( |
| 59 | + [ |
| 60 | + 'name' => 'Test Catalog Rule 50% off tomorrow', |
| 61 | + 'is_active' => '1', |
| 62 | + 'stop_rules_processing' => 0, |
| 63 | + 'website_ids' => [1, $secondWebsite->getId()], |
| 64 | + 'customer_group_ids' => [Group::NOT_LOGGED_IN_ID, 1], |
| 65 | + 'discount_amount' => 50, |
| 66 | + 'simple_action' => 'by_percent', |
| 67 | + 'from_date' => (new \DateTime('+1 day'))->format('m/d/Y'), |
| 68 | + 'to_date' => '', |
| 69 | + 'sort_order' => 0, |
| 70 | + 'sub_is_enable' => 0, |
| 71 | + 'sub_discount_amount' => 0, |
| 72 | + 'conditions' => [], |
| 73 | + ] |
| 74 | +); |
| 75 | +$catalogRuleRepository->save($catalogRule); |
| 76 | +$indexBuilder->reindexFull(); |
0 commit comments