0

I am working on Custom Discount Coupon (Cart Rule) I am using following Data Patch but I don't find to exclude Categories

namespace Vendor\ReferAFriend\Setup\Patch\Data;

use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\SalesRule\Model\ResourceModel\Rule as RuleResource;
use Magento\SalesRule\Model\Rule;
use Magento\SalesRule\Model\RuleFactory;
use Magento\Store\Model\StoreManagerInterface;


class CreateReferAFriendRule implements DataPatchInterface
{
    /**
     * @var RuleFactory
     */
    private $ruleFactory;

/**
 * @var RuleResource
 */
private $ruleResource;

/**
 * @var StoreManagerInterface
 */
private $storeManager;

/**
 * CreateReferAFriendRule constructor.
 * @param RuleFactory $ruleFactory
 * @param RuleResource $ruleResource
 * @param StoreManagerInterface $storeManager
 */
public function __construct(
    RuleFactory $ruleFactory,
    RuleResource $ruleResource,
    StoreManagerInterface $storeManager
) {
    $this->ruleFactory = $ruleFactory;
    $this->ruleResource = $ruleResource;
    $this->storeManager = $storeManager;
}

/**
 * {@inheritdoc}
 */
public function apply()
{
    /** @var Rule $shoppingCartPriceRule */
    $shoppingCartPriceRule = $this->ruleFactory->create();

    $shoppingCartPriceRule
        ->setName('Refer A Friend')
        ->setDescription('Allow customers to share a coupon code with their friends exclude Sticks')
        ->setUsesPerCustomer(null)
        ->setCustomerGroupIds(['0', '1', '2', '3'])
        ->setIsActive(1)
        ->setStopRulesProcessing(0)
        ->setIsAdvanced(1)
        ->setProductIds(null)
        ->setSortOrder(1)
        ->setSimpleAction(Rule::BY_PERCENT_ACTION)
        ->setDiscountAmount(100)
        ->setDiscountQty(null)
        ->setDiscountStep(0)
        ->setSimpleFreeShipping(2)
        ->setTimesUsed(0)
        ->setIsRss(0)
        ->setCouponType(Rule::COUPON_TYPE_SPECIFIC)
        ->setUseAutoGeneration(1)
        ->setCouponCode(null)
        ->setCategoryIds()
        ->setWebsiteIds($this->getWebsiteIds())
        ->setUsesPerCoupon(null);

    $this->ruleResource->save($shoppingCartPriceRule);
}

/**
 * @return array
 */
private function getWebsiteIds(): array
{
    $websiteIds = [];
    foreach ($this->storeManager->getWebsites() as $website) {
        $websiteIds[] = $website->getId();
    }
    return $websiteIds;
}

/**
 * {@inheritdoc}
 */
public static function getDependencies()
{
    return [];
}

/**
 * {@inheritdoc}
 */
public function getAliases()
{
    return [];
}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.