Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple issues #1

Open
gewaechshaus opened this issue Jan 17, 2018 · 9 comments
Open

Multiple issues #1

gewaechshaus opened this issue Jan 17, 2018 · 9 comments

Comments

@gewaechshaus
Copy link

gewaechshaus commented Jan 17, 2018

Hey,

we just installed your module in a M2.2.2 system... Thanks for providing it. We tried to install it by composer first, but it looks like the package is missing on packagist.

So far so good, we wen't on by installing it in the app folder and procede the setup:upgrade, di:compile etc. This did work as expected.

In the next step we tried to avoid the discount like described in the readme. We got a rule, that enables 5% discount for new customers if they type in the code "neukunde". The field "Uses per coupon" is set to 0 (everything else doesn't make sense in case of one code for all), the field "Uses per customer" is set to 1 which never match the methods condition here.

Regards
Jan

@gewaechshaus
Copy link
Author

gewaechshaus commented Jan 17, 2018

I think the condition linked above does make no sense for our configuration, while it generally makes no sense to provide unique codes for welcome discounts.

@srenon
Copy link
Contributor

srenon commented Jan 17, 2018

If I understand what you are trying to do correctly, you are trying to create a coupon that can use an unlimited amount of time but only once per customer.

The problem that I was solving at the time was slightly different, and only half of the original problem which was to prevent a guest customer from adding a coupon that was set to use X times (not unlimited).

But to accomplish your goal should be easy

    public function aroundCanProcessRule($subject, \Closure $proceed, $rule, $address)
    {
        $result = $proceed($rule, $address);
        $couponCode = $address->getQuote()->getCouponCode();
        if ($this->helperData->isEnabled() && $couponCode) {
           ........
        }

        if(get customer email from quote){
             //assuming you not using muti coupon
             $searchCriteria = "select from sales_order where email = 'customer email' and coupon_code = $couponCode"
              $orders = $this->orderRepository->getList($searchCriteria);
             if( $orders->getSize() >= $rule->getUsesPerCustomer()){
                  $result = false;
             }
        } 
        ....

You can find most of the logic here https://github.com/magepal/magento2-guest-to-customer/blob/master/Controller/Guesttocustomer/LookupformPost.php#L83

@gewaechshaus
Copy link
Author

Thanks for pointing me into the right direction. I will post the final code. First thing would be to implement searchCriteriaBuilder.

@srenon
Copy link
Contributor

srenon commented Jan 17, 2018

Untested, but should look something like this

public function __construct(
    ....
    \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
    \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
) {
    $this->orderRepository = $orderRepository;
    $this->searchCriteriaBuilder = $searchCriteriaBuilder;
    parent::__construct($context);
}

public function aroundCanProcessRule()
{
    if (!$quote->getCustomerId() && $quote->getCustomerEmail()) {
        $searchCriteria = $this->searchCriteriaBuilder
                                    ->addFilter('customer_email', $$quote->getCustomerEmail(), 'eq')
                                    ->addFilter('coupon_code', $$quote->getCouponCode(), 'eq')
                                    ->create();
        $order = $this->orderRepository->getList($searchCriteria)->getFirstItem();

@gewaechshaus
Copy link
Author

Thanks again - I also didn't test your code, I was just reading about multiple AND's to realize the query you posted first. If it is as easy as adding multiple addFilter calls that would be nice. I found a note by Alan Storm that the searchCriteriaBuilder doesn't have the addFilter method. I have to test it.

@gewaechshaus
Copy link
Author

My current state is here
https://gist.github.com/gewaechshaus/afe06347e5bb8d4bb0a149c031dc8b66

That is working so far, but I need to observe another event or call a custom controller in the checkout on e-mail fields focusout event. That is cause if you enter the discount code in cart, and then procede to checkout were you enter the email address afterwards, aroundCanProcessRule() method want be called again.

@srenon
Copy link
Contributor

srenon commented Jan 18, 2018

I just did a quick test, and whenever I pressed the "Next" button on checkout shipping step the aroundCanProcessRule method get invoke which would revalidate the email address.

One of the reasons I did not bother to implement this was because it was so easy to bypass ..... I could just checkout with [email protected] ... [email protected] etc

@gewaechshaus
Copy link
Author

gewaechshaus commented Jan 18, 2018

Yes, you are right, but we are using a onestepcheckout...
Feel free to test under https://dev.makerdise.com

I can understand your arguments, but I see things a bit different:

  • This solution is better than none
  • According to studies, this will be a barrier for those who are using one email address (or similar constellation) for orders they are placing
  • Everybody else who wants to manipulate or grab the discount in every case, the solution would be to extend the method for double or tripple validation, e.g. read out the name or street address.
    This will be one of the next steps.

@srenon
Copy link
Contributor

srenon commented Jan 18, 2018

I have used this code on "onestepcheckout" before but It's hard to say without knowing which one step checkout you are using.

You could use a mixture of JS mix-in and plugin for rest/default/V1/customers/isEmailAvailable to trigger to remove the coupon in invalid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants