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?