0

I have my custom module Pricebycustomergroup i have created a rule where a specific product price will be decreased like orignal is 34$ and the new one will be 20$ but this will works if max quantity is 2 in cart..

I wanted to create graphql APIs for this now what i do is i use AfterGetPrice Plugin

public function afterGetPrice(Product $subject, $result) { if ($this->state->getAreaCode() === Area::AREA_WEBAPI_REST || $this->state->getAreaCode() === Area::AREA_GRAPHQL) { //my code here } return $result; } 

It is working fine but the issue is quantity logic failed like what i did is for quantity greater than 2 for that specific product logic not working because it will always return the same price every time.. another option is i can use cart here but what in case of guest cart ?

Please help Thanks in advance !!

1 Answer 1

1

I'm not sure why you're handling this through code when you can achieve the same functionality using cart price rules.

enter image description here

But if you still want to do by code then create a custom module using an Observer observer for the event sales_quote_collect_totals_after

<?php namespace Vendor\PriceByCustomerGroup\Observer; use Magento\Framework\Event\Observer; use Magento\Framework\Event\ObserverInterface; use Magento\Quote\Model\Quote\Item; class AdjustCartPrice implements ObserverInterface { public function execute(Observer $observer) { $quote = $observer->getQuote(); foreach ($quote->getAllItems() as $item) { if ($item->getProduct()->getId() == YOUR_PRODUCT_ID) { $qty = $item->getQty(); if ($qty <= 2) { $item->setCustomPrice(20); // Set your custom price $item->setOriginalCustomPrice(20); // Retain the original custom price $item->getProduct()->setIsSuperMode(true); // Prevent overwriting the custom price } } } } } 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.