Buy X Get Y's auto add does not respect a product's stock

Created on 5 October 2023, 9 months ago
Updated 15 December 2023, 7 months ago

I think this would be the right place to raise this issue and not in commerce_stock.

To reproduce:
Enable commerce_promotion, commerce_stock, commerce_stock_enforcement, commerce_stock_field, commerce_stock_local, (commerce_stock_ui)
Create two products, one with stock and one without.
Create a Buy X Get Y promotion, put the product with stock as 'buy' and the product without stock as as 'get'.
Set the promotion to 100% discount and enable auto-add.
Add the 'buy' product to cart. The 'get' product will also be added, and the checkout will error.

Resolution:
I've locally patched in a guard clause right after if ($this->configuration['get_auto_add'] && ($get_purchasable_entity = $this->findSinglePurchasableEntity($get_conditions))) { that directly checks the stock level; if out of stock it displays a warning and returns.
I've seen other places in commerce core use AvailabilityManager's check() function, which is probably a better way to go.

πŸ› Bug report
Status

Postponed: needs info

Version

1.0

Component

Code

Created by

πŸ‡³πŸ‡±Netherlands Zevior

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @Zevior
  • πŸ‡ΊπŸ‡ΈUnited States rszrama

    Ultimately, it's up to Commerce Stock to find a way to prevent this from happening. Commerce Core has no knowledge of or model for accommodating stock levels defined by other modules at this point.

  • πŸ‡³πŸ‡±Netherlands Zevior

    @rszrama shouldn't the promotion code check the target product's availability using AvailabilityManager? That sounds like a way to give modules an opportunity to deny the action.

  • Status changed to Postponed: needs info 7 months ago
  • πŸ‡¬πŸ‡§United Kingdom guy_schneerson

    Stock does not currently use the AvailabilityManager and has its own system. It does however have an Empty (Always TRUE) implementation that would be good to implement and I'll be happy to look into it if it will solve this issue.

    However, it looks to me like the auto-add does not trigger the AvailabilityManager->check() so I can't see anything we can do. @rszrama do you have an idea of how to approach this or can the Auto Add use the AvailabilityManager or some other method to hook in?

Production build 0.69.0 2024