I would like to create a coupon code that will give you 50% off 1 category and 25% off another.
I do not want 2 different coupons. I have only been able to do this with customer groups catalog rules but that's no ideal.
Any extensions or ideas how I would go about this ?
Hi,
Create 2 shopping cart rules with the same coupon code. That's it. Just remove the unique key index from the database and the rule model (you can create a new extension for this)
Mage_SalesRule_Model_Resource_Coupon
protected function _construct() { $this->_init('salesrule/coupon', 'coupon_id'); $this->addUniqueField(array( 'field' => 'code', 'title' => Mage::helper('salesrule')->__('Coupon with the same code') )); }
Remove the addUniqueField call. Don't modify the core file, override it in a custom module.
You'll also need to modify the table salesrule_coupon
`ALTER TABLE salesrule_coupon DROP INDEX UNQ_SALESRULE_COUPON_CODE;`
Again, do this in a custom module as opposed to a direct sql call.
Thanks!!
Can you explain a little more how to create the module ?
Thank you
Its working fine .
We tested this by adding duplicate coupons as well .
But later when we try to run setup:upgrade it gives a error
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'tescoupon' for key for key 'SALESRULE_COUPON_CODE', query was: ALTER TABLE `salesrule_coupon` ADD CONSTRAINT `SALESRULE_COUPON_CODE` UNIQUE KEY (`code`)
Any idea why it again try t add constraint ?
Thanks
I have fixed the issue with the 1062 Duplicate entry.
Actually the salesrule_coupon table is created using db_schama.xml in core salesrule module.
And I was removing the contraint using the UpgradeSchema Script .
Here is the solution for above issue
1. Create Custom Module in your vendor directory
2. Create db_schema.xml file in your etc directory
3. Paste the below code in the newly created db_scham.xml file
<?xml version="1.0"?> <schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd"> <table name="salesrule_coupon" resource="default" engine="innodb" comment="Salesrule Coupon"> <constraint xsi:type="unique" referenceId="SALESRULE_COUPON_CODE" disabled="true"> <column name="code"/> </constraint> </table> </schema>
4. Generate the whitelist json using below commnd (Change the module name as your module name)
php bin/magento setup:db-declaration:generate-whitelist --module-name=Vendor_SalesRule
5. Overide the
Magento\SalesRule\Model\ResourceModel\Coupon
Override the method like below
/** * @return void */ protected function _construct(): void { $this->_init('salesrule_coupon', 'coupon_id'); }
thats it . It should work .
Thanks
This is also something I am trying to setup on my store, however, I don't really have that much coding experience. I'd be very thankful if someone could send me some easy instructions on this or the module code that they are using?
Thanks!
Not working.
Getting an Error:
Hello @mousepad,
Hope you are doing well,
Instead of solving the errors, I have a permanent solution for your query. You can look at Magento 2 Import Coupon Codes extension for simplifying the coupon code processes in your Magento 2 store.
It will help in reducing your tasks of adding discount codes manually in your Magento 2 store. You can invest that time in other developing tasks which help your eCommerce store grow.
Hit the kudos and accept as a solution if you find the solution helpful.
Thank-You