プラグイン > バグ報告・利用に関する質問 > 同じ商品の別の規格をカートに入れさせないようにしたい |
バグ報告・利用に関する質問
フラット表示 | 前のトピック | 次のトピック |
投稿者 | スレッド |
---|---|
beginner99 |
投稿日時: 2025/3/19 14:26
対応状況: −−−
|
常連 ![]() ![]() 登録日: 2024/6/4 居住地: 投稿: 48 |
同じ商品の別の規格をカートに入れさせないようにしたい ▼テンプレート
[EC-CUBE]4.1 [現象] 同じ商品の別の規格をカートに入れさせないようにしたいと考えております。 app\Customize\Service\CustomCartService.php を作成することで対応したいと考えております。 しかしながら ソースのどこかを間違っていると思われ、 上手く機能しません。 1) プログラム上、間違っている部分がありましたら教えてほしいです。 2) デバッグ やログを出力したいのですが、方法を教えて頂けると助かります。 3) 同じ商品がある場合、エラー文を表示させる方法を知りたい。 ソースは下記になります。 app\Customize\Service\CustomCartService.php class CustomCartService extends CartService { protected $cartItemRepository; protected $session; protected $translator; // 適用する商品IDリスト private $targetProductIds = [54]; // 例: 商品ID 54 に適用 public function __construct( CartItemRepository $cartItemRepository, SessionInterface $session, TranslatorInterface $translator ) { parent::__construct($cartItemRepository, $session, $translator); $this->cartItemRepository = $cartItemRepository; $this->session = $session; $this->translator = $translator; } /** * * @throws InvalidItemException */ public function addProduct($ProductClass, $quantity = 1) { $Product = $ProductClass->getProduct(); // ProductClass から Product を取得 // 特定の商品にのみ適用 if (in_array($Product->getId(), $this->targetProductIds)) { $cartItems = $this->getCart()->getCartItems(); foreach ($cartItems as $cartItem) { if ($cartItem->getProduct()->getId() === $Product->getId()) { if ($cartItem->getProductClass()->getId() !== $ProductClass->getId()) { // 異なる商品規格がカートにある場合、追加をブロック return false; } } } } return true; } } |
フラット表示 | 前のトピック | 次のトピック |
題名 | 投稿者 | 日時 |
---|---|---|
» ![]() |
beginner99 | 2025/3/19 14:26 |
![]() |
h_tanaka | 2025/3/19 14:32 |
![]() |
beginner99 | 2025/3/19 14:43 |
![]() |
mcontact | 2025/3/19 15:04 |
![]() |
beginner99 | 2025/3/19 15:14 |
![]() |
h_tanaka | 2025/3/19 15:20 |
![]() |
tattsu | 2025/3/22 10:21 |
