woocommerce-coupon-generator icon indicating copy to clipboard operation
woocommerce-coupon-generator copied to clipboard

Add a custom filter to easily customize coupon code generation

Open robert777robert777 opened this issue 5 years ago • 4 comments

so any site owner would be able to use code in his plugin/theme such as: add_filter('wccg_customize_random_coupon','my_wccg_customize_random_coupon'); function my_wccg_customize_random_coupon($coupon_code) { $prefix = 'my-prefix-'; return ($prefix . $coupon_code); }

I also did some cleanup

robert777robert777 avatar Dec 20 '19 12:12 robert777robert777

One issue with filtering this way, like the existing filter in that function, is that the coupon code is generated before the filter is applied. This makes coupon generation twice as slow because the filter would then be generating a code a second time.

Might it be better to have a filter before coupon generation where an array of all of the parameters can be filtered? This could also be a way to offer a filterable prefix, suffix, and control of dashes between characters without having to build out a UI for it yet.

douglsmith avatar Jan 19 '20 02:01 douglsmith

My idea in this filter is to have a single filter for any use for any developer. This filter can be used to add prefix/suffix/etc, modify the generated code (add/remove dashes or any other characters) or even discard the generated code and generate a brand new code with any kind of logic (other character set, a mix of blocks of different character sets of variable length, date codes, serial numbers or anything else).

string concatenation takes less than a microsecond, so i doubt if it will have any performance problems

having a filter before the generation wouldn't allow all of those usage examples

robert777robert777 avatar Jan 28 '20 17:01 robert777robert777

Maybe this was not here at the time of this pull request, but there is a filter on the generated code:

// Ensure coupon code is correctly formatted
$coupon_code = apply_filters( 'woocommerce_coupon_code', $random_coupon );

Couldn't one hook into that and give back whatever further modifications to the default format that you would like? I think you would have to just make any changes and then call the filter yourself to get the "WooCommerce" OK on it before returning your modified value, or maybe just set your priority lower than the default so you are called first.

I put in a pull request for a different kind of customization, but these are not really incompatible filters, they can work together and you would override whichever makes sense for you.

n9yty avatar Feb 12 '20 01:02 n9yty

Maybe this was not here at the time of this pull request, but there is a filter on the generated code:

// Ensure coupon code is correctly formatted
$coupon_code = apply_filters( 'woocommerce_coupon_code', $random_coupon );

Couldn't one hook into that and give back whatever further modifications to the default format that you would like? I think you would have to just make any changes and then call the filter yourself to get the "WooCommerce" OK on it before returning your modified value, or maybe just set your priority lower than the default so you are called first.

I put in a pull request for a different kind of customization, but these are not really incompatible filters, they can work together and you would override whichever makes sense for you.

Did you actually try to use it? This hook is used a lot by woocommerce, not just in coupon generation. The only way to use it is using the following dirty trick (without it it would modify existing coupons, not just the newly generated ones):

add_filter('woocommerce_coupon_code','robert_get_random_coupon');
function robert_get_random_coupon($arg) {
	if (debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS,4)[3]['function']!=='wccg_get_random_coupon') return $arg;
	// do your modification here
}

robert777robert777 avatar Feb 13 '20 13:02 robert777robert777