vendredi 17 novembre 2017

Wordpress unique coupon code generation & print - PHP

Newby here. I have been trying to implement unique code generation functionality to my theme. I am trying to explain the current function and the function that I want to get down below. As of theme design, a coupon code is predefined on admin panel when creating a coupon. So it is just a code that appears all the time when you click on 'display Code' button. What I want to get is a unique coupon code whenever a visitor clicks on the button. The way I want to do this is generating 4 decimal random string consisting of letters and numbers and paste it near to the coupon code value that I define when I create the deal. Example: Let's say I am creating a deal for Walmart. I define the coupon code as WMT. What I want is to show coupon codes like WMT12Y4, WMTt5ff,.... whenever a visitor clicks on the button.

To do so, I found the code right below from another question.

$chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$res = "";
for ($i = 0; $i < 4; $i++) {
$res .= $chars[mt_rand(0, strlen($chars)-1)];
}

I guess this will help me to generate 4 digits random value.

Then I need to paste it to the coupon code value, but I don't know how to by changing the code in the theme file right below. I try to add the random code generator there and $res in code but it won't work:

else {
        $enable_redirect = get_field('redirect_to_offer') ? 
get_field('redirect_to_offer') : array('');

        $chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$res = "";
for ($i = 0; $i < 4; $i++) {
$res .= $chars[mt_rand(0, strlen($chars)-1)];
}

        echo'<a href="' . esc_url(get_field('url')) . '" target="_blank" 
class="btn btn-color btn-deal show-coupon-code" data-target="#discount-modal" 
data-clipboard-text="' . esc_attr(get_field('coupon_code')) . $res . '" data-
redirect="' . $enable_redirect[0] . '">' . fw_ssd_get_option('show-code-text') 
. '</a>';
    }

Looking for some help. Thanks in advance :)




Aucun commentaire:

Enregistrer un commentaire