magento 我想停止特定的客户群购买不超过两个产品,也停止在洋红色的购物车的具体金额2

wdebmtf2  于 2023-10-19  发布在  其他
关注(0)|答案(1)|浏览(128)

我想阻止特定客户群购买不超过两种产品。我也需要停止他们在车的具体数额。这是可能的,如果是的话,那么什么是最好的方法来做观察者,插件或preference?如果有人以前做过,请分享你的解决方案。
我试图通过插件停止添加到购物车,但页面重新加载,我无法显示异常消息。

  1. <?php
  2. namespace CustomPlugin\CartProductRestriction\Plugin\Cart;
  3. use Magento\Checkout\Controller\Cart\Add;
  4. use Magento\Framework\Message\ManagerInterface;
  5. use Magento\Checkout\Model\Cart;
  6. class RestrictCartQty
  7. {
  8. protected $customerSession;
  9. protected $messageManager;
  10. protected $cart;
  11. public function __construct(
  12. \Magento\Customer\Model\Session $customerSession,
  13. ManagerInterface $messageManager,
  14. Cart $cart
  15. ) {
  16. $this->customerSession = $customerSession;
  17. $this->messageManager = $messageManager;
  18. $this->cart = $cart;
  19. }
  20. public function aroundExecute(Add $subject, callable $proceed)
  21. {
  22. $allowedCustomerGroup = 4;
  23. $customerGroupId = $this->customerSession->getCustomer()->getGroupId();
  24. if ($customerGroupId == $allowedCustomerGroup) {
  25. $itemCount = $this->cart->getQuote()->getItemsQty();
  26. if ($itemCount >= 2) {
  27. $message = __('You can only add 2 items to your cart.');
  28. $this->messageManager->addError($message);
  29. return $subject;
  30. }
  31. }
  32. return $proceed();
  33. }
  34. }
jgzswidk

jgzswidk1#

  1. //di.xml file
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
  4. <type name="Magento\Quote\Model\Quote">
  5. <plugin name="limit_cart_items_plugin_name" type="YourVendor\YourModule\Plugin\Quote\ClassName" sortOrder="10" disabled="false"/>
  6. </type>
  7. </config>
  8. //YourVendor\YourModule\Plugin\Quote
  9. <?php
  10. namespace YourVendor\YourModule\Plugin\Quote;
  11. use Magento\Quote\Model\Quote;
  12. use Magento\Framework\Exception\LocalizedException;
  13. class ClassName
  14. {
  15. public function beforeBeforeSave(Quote $subject)
  16. {
  17. $customerGroupId = $subject->getCustomerGroupId();
  18. // Define the customer group(s) you want to restrict
  19. $restrictedCustomerGroups = [1, 3];
  20. // Check if the customer group is in the restricted list
  21. if (in_array($customerGroupId, $restrictedCustomerGroups)) {
  22. // Get the items in the cart
  23. $items = $subject->getAllItems();
  24. // Define the maximum number of items allowed in the cart
  25. $maxItemsAllowed = 2;
  26. // Define the maximum cart total amount and here get specific amount
  27. $maxCartTotal = 120;
  28. $totalQty = 0;
  29. $cartTotal = 0;
  30. $itemsToRemove = [];
  31. foreach ($items as $item) {
  32. $totalQty += $item->getQty();
  33. $cartTotal += $item->getRowTotal();
  34. // Check if the item quantity exceeds the limit
  35. if ($item->getQty() > $maxItemsAllowed) {
  36. $itemsToRemove[] = $item;
  37. }
  38. }
  39. // Check if the cart exceeds the limits
  40. if ($totalQty > $maxItemsAllowed || $cartTotal > $maxCartTotal) {
  41. // Remove extra items
  42. foreach ($itemsToRemove as $itemToRemove) {
  43. $subject->removeItem($itemToRemove->getId());
  44. }
  45. throw new LocalizedException(__('You can only add up to 2 products to your cart. and maximum amount 120.'));
  46. }
  47. }
  48. return [$subject];
  49. }
  50. }
展开查看全部

相关问题