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

[Voucher] restrict voucher usage per customer #2451

Merged
merged 14 commits into from
Dec 2, 2023
Merged

[Voucher] restrict voucher usage per customer #2451

merged 14 commits into from
Dec 2, 2023

Conversation

Philip-Neusta
Copy link
Contributor

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Fixed tickets -

In the conditions for limiting the use of vouchers, it was only possible to set how often a code could be used in total. However, when working with Coreshop, we wanted to be able to limit the use of a voucher per customer. In this pull request we added this feature. Our implementation so far is not final but works. Could you take a look at this and give feedback on whether this is going in the right direction?

@CLAassistant
Copy link

CLAassistant commented Nov 21, 2023

CLA assistant check
All committers have signed the CLA.

Copy link
Member

@dpfaffenbauer dpfaffenbauer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very nice idea, very good implementation. I do have some changes for you though. If you need further info, let me know.

@@ -156,6 +159,21 @@ private function addModelsSection(ArrayNodeDefinition $node): void
->end()
->end()
->end()
->arrayNode('cart_price_rule_voucher_code_user')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be called cart_price_rule_voucher_code_customer

class CartPriceRuleVoucherCodeUserRepository extends EntityRepository implements CartPriceRuleVoucherCodeUserRepositoryInterface
{

public function findByUsesById(int $userId, int $voucherCodeId): ?CartPriceRuleVoucherCodeUserInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should pass the CustomerInterface direcly


public function addCodeUserUsage(int $userId, CartPriceRuleVoucherCodeInterface $voucherCode): void
{
$voucherCodeUser = new CartPriceRuleVoucherCodeUser();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. We never create entities in the Repository
  2. You should use the factory to create the entity

return;
}

$user = $this->security->getUser();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use $order->getCustomer()

// max usage per user condtion
if (is_numeric($maxUsagePerUser)){

$user = $this->security->getUser();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get the customer from the cart/order

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason we get the user from the security component, is that we wanted to explicitly limit the vouchers for registered customers only, because it's just not really possible for guests. This means if you use such a voucher as a guest, it will always be invalid.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the customer you can get the user ;).

$cart->getCustomer()->getUser()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, but should we still rename user to customer as in:
#2451 (comment)

and still pass the CustomerInterface in:
#2451 (comment)

if we only work with the CoreShopUser?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In reality, it also works for Guest Customers. CoreShop re-uses a customer based on the email address. (that is a feature that if a customer wants to convert to a real user, we have the order history)

@@ -19,33 +19,26 @@
namespace CoreShop\Bundle\OrderBundle\Doctrine\ORM;

use CoreShop\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
use CoreShop\Component\Order\Model\CartPriceRuleVoucherCodeInterface;
use CoreShop\Component\Order\Model\CartPriceRuleVoucherCodeUser;
use CoreShop\Component\Core\Model\CustomerInterface;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use CoreShop\Component\Customer\Model\CustomerInterface.php

@@ -4,13 +4,14 @@
namespace CoreShop\Bundle\OrderBundle\EventListener;


use CoreShop\Component\Order\Model\CartPriceRuleVoucherCodeInterface;
use CoreShop\Component\Core\Model\CustomerInterface;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use CoreShop\Component\Customer\Model\CustomerInterface.php

- '@coreshop.repository.cart_price_rule_voucher_code'
- '@Symfony\Component\Security\Core\Security'
- '@CoreShop\Component\Order\Factory\CartPriceRuleVoucherCodeUserFactory.inner'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • '@coreshop\Component\Order\Factory\CartPriceRuleVoucherCodeUserFactoryInterface'

use CoreShop\Component\Order\Model\CartPriceRuleInterface;
use CoreShop\Component\Order\Model\CartPriceRuleVoucherCodeInterface;
use CoreShop\Component\Order\Model\OrderInterface;
use CoreShop\Component\Order\Model\PriceRuleItemInterface;
use CoreShop\Component\Order\Repository\CartPriceRuleVoucherCodeUserRepositoryInterface;
use CoreShop\Component\Order\Repository\CartPriceRuleVoucherRepositoryInterface;
use Symfony\Component\Security\Core\Security;
use Pimcore\Model\DataObject\CoreShopCustomer;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove usages of Pimcore DataObject Classes

@@ -18,15 +18,15 @@

namespace CoreShop\Component\Order\Repository;

use CoreShop\Component\Order\Model\CartPriceRuleVoucherCodeInterface;
use CoreShop\Component\Core\Model\CustomerInterface;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use CoreShop\Component\Customer\Model\CustomerInterface.php

@dpfaffenbauer
Copy link
Member

@Philip-Neusta I added some changes: Mainly: renaming user to customer, and I moved your listener to the voucher modifier we already have. One more thing though: We don't add new features to 3.1 anymore, can you rebase to 3.2?

@Philip-Neusta Philip-Neusta changed the base branch from 3.1 to 3.2 December 1, 2023 14:30
@Philip-Neusta
Copy link
Contributor Author

Philip-Neusta commented Dec 1, 2023

now rebased and changed base to 3.2

@dpfaffenbauer dpfaffenbauer merged commit ac287b3 into coreshop:3.2 Dec 2, 2023
418 checks passed
@dpfaffenbauer
Copy link
Member

@Philip-Neusta amazing work 🎉 🍻

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

Successfully merging this pull request may close these issues.

None yet

3 participants