mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
Merge pull request #2265 from woocommerce/PCP-2556-same-pay-pal-subscription-product-can-be-added-to-the-cart-multiple-times
Make PayPal Subscription products unique in cart (2556)
This commit is contained in:
commit
805866602e
2 changed files with 47 additions and 0 deletions
|
@ -38,6 +38,9 @@ document.addEventListener(
|
|||
|
||||
const subscriptionTrial = document.querySelector('._subscription_trial_length_field');
|
||||
subscriptionTrial.style.display = 'none';
|
||||
|
||||
const soldIndividually = document.querySelector( '#_sold_individually' );
|
||||
soldIndividually.setAttribute( 'disabled', 'disabled' );
|
||||
}
|
||||
|
||||
const setupProducts = () => {
|
||||
|
|
|
@ -87,6 +87,44 @@ class PayPalSubscriptionsModule implements ModuleInterface {
|
|||
12
|
||||
);
|
||||
|
||||
add_filter(
|
||||
'woocommerce_add_to_cart_validation',
|
||||
/**
|
||||
* Param types removed to avoid third-party issues.
|
||||
*
|
||||
* @psalm-suppress MissingClosureParamType
|
||||
*/
|
||||
static function ( $passed_validation, $product_id ) {
|
||||
if ( WC()->cart->is_empty() ) {
|
||||
return $passed_validation;
|
||||
}
|
||||
|
||||
$product = wc_get_product( $product_id );
|
||||
|
||||
if ( $product && $product->get_meta( '_ppcp_enable_subscription_product', true ) === 'yes' ) {
|
||||
if ( ! $product->get_sold_individually() ) {
|
||||
$product->set_sold_individually( true );
|
||||
$product->save();
|
||||
}
|
||||
|
||||
wc_add_notice( __( 'You cannot add a subscription product to a cart with other items.', 'woocommerce-paypal-payments' ), 'error' );
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
|
||||
$cart_product = wc_get_product( $cart_item['product_id'] );
|
||||
if ( $cart_product && $cart_product->get_meta( '_ppcp_enable_subscription_product', true ) === 'yes' ) {
|
||||
wc_add_notice( __( 'You can only have one subscription product in your cart.', 'woocommerce-paypal-payments' ), 'error' );
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return $passed_validation;
|
||||
},
|
||||
10,
|
||||
2
|
||||
);
|
||||
|
||||
add_action(
|
||||
'woocommerce_save_product_variation',
|
||||
/**
|
||||
|
@ -654,12 +692,18 @@ class PayPalSubscriptionsModule implements ModuleInterface {
|
|||
// phpcs:ignore WordPress.Security.NonceVerification
|
||||
$enable_subscription_product = wc_clean( wp_unslash( $_POST['_ppcp_enable_subscription_product'] ?? '' ) );
|
||||
$product->update_meta_data( '_ppcp_enable_subscription_product', $enable_subscription_product );
|
||||
|
||||
if ( ! $product->get_sold_individually() ) {
|
||||
$product->set_sold_individually( true );
|
||||
}
|
||||
|
||||
$product->save();
|
||||
|
||||
if ( ( $product->get_type() === 'subscription' || $product->get_type() === 'subscription_variation' ) && $enable_subscription_product === 'yes' ) {
|
||||
if ( $product->meta_exists( 'ppcp_subscription_product' ) && $product->meta_exists( 'ppcp_subscription_plan' ) ) {
|
||||
$subscriptions_api_handler->update_product( $product );
|
||||
$subscriptions_api_handler->update_plan( $product );
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue