Jump to content

[SOLVED] Check if current user has used a specific voucher code


jamiedevine

Recommended Posts

My client wants to run a promotion on their PrestaShop site that allows customers to use a voucher code to get money off certain products that only works once per customer.

 

The voucher is set up and working but as part of the promotion we have also have an overlay that pops up after xxx seconds that lets the user know about the code and the promotion.

 

The problem is this overlay should only appear if the customer has not already used the voucher code in the past, because each customer is only allowed to use it once there is no point in showing it to them again.

 

In FrontController.php, within the display() method I'm trying to create a boolean variable (that is available in the footer) which will be true or false depending on if the user has already used the voucher for this promotion.

 

I can then control whether the HTML for the overlay is output or not.

 

How can I check if the customer has already used the voucher code?

Edited by jamiedevine (see edit history)
Link to comment
Share on other sites

  • 5 months later...
  • 1 year later...

Sure, here is the code to do it:
 

// $id_rule is the id of the voucher code
$objCartRule = new CartRule($id_rule);

// Will return true or false
$objCartRule->usedByCustomer($id_customer);

And to get all the rules a customer has used - place the following code in your CartRule override class (maybe this fits better in the Customer class, but whichever you think):
 

/**
* Get rules used by customer
* @param $id_customer
 */
public static function getRulesUsedByCustomer($id_customer)
{
	return Db::getInstance()->executeS('
	SELECT DISTINCT id_cart_rule
	FROM `'._DB_PREFIX_.'order_cart_rule` ocr
	LEFT JOIN `'._DB_PREFIX_.'orders` o ON ocr.`id_order` = o.`id_order`
	AND o.`id_customer` = '.(int)$id_customer);
}

And then get a list of rule IDs the customer has used by doing this:

$arrRuleIds = CartRule::getRulesUsedByCustomer($id_customer);
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...