如何在PrestaShop 1.7中批量生成100+优惠券?
Hey there! I totally get it—needing to crank out over 100 coupons for your PrestaShop 1.7 store, only to realize the back office doesn’t have a built-in bulk generation tool, is such a hassle. Let’s break down a few solid, doable ways to get this sorted without too much fuss.
If you’re comfortable working with databases, this is the quickest way. First, make sure you back up your database—better safe than sorry!
You can run this SQL query in your database management tool (like phpMyAdmin):
-- Configure these variables first SET @coupon_prefix = 'SAVE10_'; -- Prefix for your coupon codes SET @number_of_coupons = 150; -- Number of coupons you want SET @discount_type = 'percentage'; -- 'percentage' or 'amount' SET @discount_value = 10; -- Discount amount/percentage SET @valid_from = '2024-01-01'; -- Start date (YYYY-MM-DD) SET @valid_to = '2024-12-31'; -- End date (YYYY-MM-DD) SET @usage_limit = 1; -- How many times each coupon can be used SET @cart_minimum = 0; -- Minimum cart value required (0 = no minimum) -- Don't edit below unless you know what you're doing INSERT INTO `ps_cart_rule` ( `name`, `description`, `date_from`, `date_to`, `active`, `highlight`, `display_on_product`, `display_on_cart`, `quantity`, `quantity_per_user`, `priority`, `partial_use`, `code`, `minimum_amount`, `minimum_amount_tax`, `minimum_amount_currency`, `minimum_amount_shipping`, `country_restriction`, `carrier_restriction`, `group_restriction`, `cart_rule_restriction`, `product_restriction`, `shop_restriction`, `free_shipping`, `reduction_percent`, `reduction_amount`, `reduction_tax`, `reduction_currency`, `gift_product`, `gift_product_attribute`, `date_add`, `date_upd` ) SELECT CONCAT(@coupon_prefix, FLOOR(RAND() * 1000000)), -- Generate unique code 'Bulk-generated coupon', -- Description @valid_from, @valid_to, 1, -- Active (1 = yes) 0, -- Don't highlight 0, -- Don't display on product pages 0, -- Don't display on cart page 1, -- Total available uses per coupon @usage_limit, -- Uses per user 1, -- Priority 1, -- Allow partial use CONCAT(@coupon_prefix, FLOOR(RAND() * 1000000)), -- Coupon code (same as name here) @cart_minimum, 1, -- Include tax in minimum amount 1, -- Default currency ID (adjust if needed) 0, -- Don't include shipping in minimum amount 0, -- No country restriction 0, -- No carrier restriction 0, -- No group restriction 0, -- No cart rule restriction 0, -- No product restriction 0, -- No shop restriction (for multi-shop setups, adjust to your shop ID) 0, -- No free shipping IF(@discount_type = 'percentage', @discount_value, 0), IF(@discount_type = 'amount', @discount_value, 0), 1, -- Apply discount to tax 1, -- Default currency ID 0, -- No gift product 0, -- No gift product attribute NOW(), NOW() FROM INFORMATION_SCHEMA.TABLES LIMIT @number_of_coupons;
Just tweak the top variables to match your needs, run the query, and you’ll have your coupons ready in seconds.
If SQL feels too risky, a small PHP script that uses PrestaShop’s core classes is a great alternative. Here’s how:
- Create a file named
bulk_coupons.phpin your PrestaShop root folder. - Paste this code into it:
<?php require_once(dirname(__FILE__).'/config/config.inc.php'); require_once(dirname(__FILE__).'/init.php'); // Configuration $prefix = 'WELCOME20_'; $numberOfCoupons = 120; $discountType = CartRule::REDUCTION_PERCENT; // Use CartRule::REDUCTION_AMOUNT for fixed amount $discountValue = 20; $validFrom = '2024-01-01'; $validTo = '2024-12-31'; $usageLimitPerCoupon = 1; $minimumCartAmount = 0; for ($i = 0; $i < $numberOfCoupons; $i++) { $couponCode = $prefix . substr(str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'), 0, 6); $cartRule = new CartRule(); $cartRule->name = [Configuration::get('PS_LANG_DEFAULT') => $couponCode]; $cartRule->description = [Configuration::get('PS_LANG_DEFAULT') => 'Bulk-generated welcome coupon']; $cartRule->date_from = $validFrom; $cartRule->date_to = $validTo; $cartRule->active = 1; $cartRule->quantity = 1; $cartRule->quantity_per_user = $usageLimitPerCoupon; $cartRule->code = $couponCode; $cartRule->minimum_amount = $minimumCartAmount; $cartRule->minimum_amount_tax = 1; $cartRule->reduction_percent = ($discountType == CartRule::REDUCTION_PERCENT) ? $discountValue : 0; $cartRule->reduction_amount = ($discountType == CartRule::REDUCTION_AMOUNT) ? $discountValue : 0; $cartRule->reduction_tax = 1; try { $cartRule->add(); echo "Coupon created: $couponCode<br>"; } catch (Exception $e) { echo "Failed to create coupon: $couponCode - Error: " . $e->getMessage() . "<br>"; } } echo "<br>Done! Generated $numberOfCoupons coupons."; ?>
- Edit the configuration variables at the top to fit your requirements.
- Run the script by visiting
yourstore.com/bulk_coupons.phpin your browser. - Once done, delete the file from your server for security.
This method uses PrestaShop’s native CartRule class, so it’s safe and respects all your store’s settings.
If you’d rather not mess with code at all, head over to the PrestaShop Addons Marketplace and search for bulk coupon generator or similar keywords. Look for modules that explicitly say they’re compatible with PrestaShop 1.7—most of these let you set up all your coupon parameters (prefix, discount, validity, etc.) and generate hundreds of coupons with a single click.
Quick Tips Before You Start
- Always test with 2-3 coupons first to make sure the discount, validity, and usage limits work as expected.
- If you’re using the SQL or PHP method, back up your database beforehand—just in case something goes sideways.
- For multi-shop setups, adjust the
shop_restrictionvalue in the SQL query or add$cartRule->id_shop = YOUR_SHOP_ID;in the PHP script.
内容的提问来源于stack exchange,提问作者user2955324

