diff --git a/plugins/woocommerce/changelog/pr-26179 b/plugins/woocommerce/changelog/pr-26179 new file mode 100644 index 00000000000..b23d2255475 --- /dev/null +++ b/plugins/woocommerce/changelog/pr-26179 @@ -0,0 +1,4 @@ +Significance: patch +Type: tweak + +Add filter `woocommerce_cart_item_is_purchasable` to allow purchasing non-purchasable items. diff --git a/plugins/woocommerce/includes/class-wc-cart-session.php b/plugins/woocommerce/includes/class-wc-cart-session.php index b6ea044b9d1..cfc6bc24284 100644 --- a/plugins/woocommerce/includes/class-wc-cart-session.php +++ b/plugins/woocommerce/includes/class-wc-cart-session.php @@ -133,15 +133,35 @@ final class WC_Cart_Session { * * @since 3.6.0 * - * @param bool $remove_cart_item_from_session If true, the item will not be added to the cart. Default: false. - * @param string $key Cart item key. - * @param array $values Cart item values e.g. quantity and product_id. + * @param bool $remove_cart_item_from_session If true, the item will not be added to the cart. Default: false. + * @param string $key Cart item key. + * @param array $values Cart item values e.g. quantity and product_id. + * @param WC_Product $product The product being added to the cart. */ - if ( apply_filters( 'woocommerce_pre_remove_cart_item_from_session', false, $key, $values ) ) { + if ( apply_filters( 'woocommerce_pre_remove_cart_item_from_session', false, $key, $values, $product ) ) { $update_cart_session = true; - do_action( 'woocommerce_remove_cart_item_from_session', $key, $values ); + /** + * Fires when cart item is removed from the session. + * + * @since 3.6.0 + * + * @param string $key Cart item key. + * @param array $values Cart item values e.g. quantity and product_id. + * @param WC_Product $product The product being added to the cart. + */ + do_action( 'woocommerce_remove_cart_item_from_session', $key, $values, $product ); - } elseif ( ! $product->is_purchasable() ) { + /** + * Allow 3rd parties to override this item's is_purchasable() result with cart item data. + * + * @param bool $is_purchasable If false, the item will not be added to the cart. Default: product's is_purchasable() status. + * @param string $key Cart item key. + * @param array $values Cart item values e.g. quantity and product_id. + * @param WC_Product $product The product being added to the cart. + * + * @since 7.0.0 + */ + } elseif ( ! apply_filters( 'woocommerce_cart_item_is_purchasable', $product->is_purchasable(), $key, $values, $product ) ) { $update_cart_session = true; /* translators: %s: product name */ $message = sprintf( __( '%s has been removed from your cart because it can no longer be purchased. Please contact us if you need assistance.', 'woocommerce' ), $product->get_name() );