Jump to content

Module to apply CartRules or Voucher code on Shopping Order through Ajax PHP


naal1

Recommended Posts

Hello Support,

 

Please please Help i am keep asking and i didn't get any reply.. so please help me.

 

I am developing module for voucher redemption after user submit with voucher code, i will add Cart Rules through $cartRule = new CartRule()

 

but after that how can i apply that CartRules to Product order and display in the place of total voucher discount.

 

Please help me

Link to comment
Share on other sites

Hi there,

 

Sorry for leaving you out in the dark, some of us do have a life beyond 1010 esp during weekends :)

 

Let's try to look more on your details

1. Customer goes to checkout page

2. Customer type-in the Voucher code

3. Page doesn't reload, instead send an AJAX request, apply / redempt the voucher

4. Update cart summary to reflect the voucher redemption

 

Is my point of view correct?

Where are you now exactly, what are the files you have modified?

 

cheers

Link to comment
Share on other sites

First of all thanks for replying ... hmmm

 

I am nearly done with all 4 steps like added new form on shopping cart through hook, then after code enter, i use ajax request but as per prestashop we have to create rules first ..so i m creating after submit in ajax request it self and merge with order.. etc

 

my question is now what if i get error like cart rules is against it then through ajax request how can i display error as i can only able to reload page when i am try to redirect i got error or ajax request stop working..

 

though i made module but being new bie i want you expert opinion...

 

Below is code if any error come during cart merge but it didn't work for me

 

if ($error = $cartRule->checkValidity($context, false, true))
  $this->errors[] = $error;
 else
 {

  $codeid = $cartRule->getIdByCode($voucher-code);
  $context->cart->addCartRule($codeid);
  Tools::redirect('index.php?controller=order-opc');
 }

 

And i want same user donot get same voucher code facility again in website

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

Wait, so you're saying the customer type-in a *voucher* code that doesn't exist in the first place in the PS cart rule?

 

1. Customer type-in (validate using external/third party validation I assume)

--> AJAX request

2. Create the cart rule

3. Apply the cart rule

--> AJAX response (update cart summary, prices, total amount, etc)

 

Now can you just apply the error message / handling in the AJAX response?

Link to comment
Share on other sites

Ok, but now you're stuck because every voucher your customer input is being treated as a valid voucher, even though is a bogus voucher code?

Are you trying to tell the customer with bogus voucher code something like :

-------------------

There is 1 error

 

This voucher does not exists.

-------------------

 

What method of validation are you using to determine whether a voucher code is valid or not ?

You can use the existing voucher code form, just change the form behavior to AJAX call instead of regular submit.

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

I have created new form because for identification like online user come to know that website has facility to redeem from us (means our companyname). secondly that website might have their own (in house) voucher or coupon system.

 

Our voucher won't be treat as bogus because user get code from othere source like signup bonus to any website etc.

 

I just want to show error on page instead of giving alert error message like you're successfully done or There is 1 error. because i am reloading page after ajax call over. How can i show error message like default voucher error message as u mentioned above.

Link to comment
Share on other sites

Okay, I'm entering another chapter in your story now.

So you basically are a third party voucher / deal provider company ?

You have vouchers that can be redeemed in other people websites (in this case people with Prestashop website). The vouchers will be validated by your website, and then return whether a voucher is valid or not.

If the voucher is valid, the Prestashop in that website will create a cart rule for that voucher (you got this already, no problem)

If the voucher is invalid, you need to tell the customer an error message. (this is where you're stucked)

 

Assuming you used AJAX call to send voucher code from Prestashop website to your website (or web service),

if you encounter an error, you can just display it anywhere in the page, for example

// ajax call
if (response_data->error) {
$('#template').before('error voucher code');
}

Link to comment
Share on other sites

Yes we are thridparty and please see below code, this is related to cart rules error ... i can take care of error from thirparty but what if i also got error in prestashop cart rule? some part of rules is not matching like total is less or minimum purchase..etc

 

How can i display that error through ajax?

 

if ($error = $cartRule->checkValidity($context, false, true))
  $context->errors[] = $error;
 else
 {

  $code = $cartRule->getIdByCode($mycode);
  $context->cart->addCartRule($code);
 }

Link to comment
Share on other sites

Hello, I know that my question is not exactly related with the thread.

 

But I am modifying the prestashop referral module a bit, in order to make "incompatible"with some other defaults voucher code I have...

 

But looking on the cartRule class, I dont see any rule to find out how to do this .... any idea ?

 

 

Thanks a lot in advance

Link to comment
Share on other sites

  • 2 years later...
  • 2 months later...

Hi there,

First of all. I'm new using Prestashop. 

I need to do basically what this topic says: 

1. Customer type-in voucher code that doesn't exist in the PS cart rule. It's an external voucher.
2. V
alidate voucher using external/third party validation server via an AJAX request.
3. If voucher is valid, create the cart rule, now using an existing PS voucher. 
4. Apply the cart rule or show error.

I don´t know how should I do that, which hook should I use to take control of voucher validation? Where and how should I place the AJAX call? 

Thanks in advance!
Regards.


 

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...