0

I have a product for which another product needs to be added in the cart.

For this I need to check if the quantity of said product is changed in order to change the number of associated products.

I wrote a Plugin on the SetQty function in order to get this information:

    class ChangeQtyPlugin
{
    protected $checkoutSession;
    protected $cartRepository;
    protected $logger;

    public function __construct(
        Session $checkoutSession,
        CartRepositoryInterface $cartRepository,
        LoggerInterface $logger
    ) {
        $this->checkoutSession = $checkoutSession;
        $this->cartRepository  = $cartRepository;
        $this->logger    = $logger;
    }

    public function beforeSetQty(Item $subject, $result) : array
    {
        $oldQty   = $subject->getOrigData('qty');
        $addingQty   = $subject->getData('qty');
        $this->logger->info('testOld');
        $this->logger->info(print_r($oldQty, true));
        $this->logger->info('testNew');
        $this->logger->info(print_r($addingQty, true));

        return [$result];
    }
}

When I change the qty how, it returns:

[2021-10-11 12:52:51] main.INFO: testOld [] []

[2021-10-11 12:52:51] main.INFO: 10.0000 [] []

[2021-10-11 12:52:51] main.INFO: testNew [] []

[2021-10-11 12:52:51] main.INFO: 10.0000 [] []

Which is the New quantity. How can I retrieve the old Qty before the change?

1

You can get all the params value using this code

$subject->getRequest()->getParams();

You can get all the post value using this way,

$subject->getRequest()->getPostValue();

1
  • getRequest seems undifined here. It's a plugin, that might have something to do with it? Oct 12 at 7:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.