mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-04 08:47:23 +08:00
Merge branch 'refs/heads/trunk' into modularity-module-migration
# Conflicts: # composer.lock # modules/ppcp-axo/src/AxoModule.php # modules/ppcp-blocks/src/BlocksModule.php # modules/ppcp-compat/src/CompatModule.php # modules/ppcp-googlepay/src/GooglepayModule.php # modules/ppcp-order-tracking/src/OrderTrackingModule.php # modules/ppcp-wc-subscriptions/src/WcSubscriptionsModule.php
This commit is contained in:
commit
4d9f23e315
301 changed files with 33875 additions and 13196 deletions
|
@ -1,102 +1,148 @@
|
|||
document.addEventListener(
|
||||
'DOMContentLoaded',
|
||||
() => {
|
||||
const variations = document.querySelector('.woocommerce_variations');
|
||||
const disableFields = (productId) => {
|
||||
if(variations) {
|
||||
const children = variations.children;
|
||||
for(let i=0; i < children.length; i++) {
|
||||
const variableId = children[i].querySelector('h3').getElementsByClassName('variable_post_id')[0].value
|
||||
if (parseInt(variableId) === productId) {
|
||||
children[i].querySelector('.woocommerce_variable_attributes')
|
||||
.getElementsByClassName('wc_input_subscription_period_interval')[0]
|
||||
.setAttribute('disabled', 'disabled');
|
||||
children[i].querySelector('.woocommerce_variable_attributes')
|
||||
.getElementsByClassName('wc_input_subscription_period')[0]
|
||||
.setAttribute('disabled', 'disabled');
|
||||
children[i].querySelector('.woocommerce_variable_attributes')
|
||||
.getElementsByClassName('wc_input_subscription_trial_length')[0]
|
||||
.setAttribute('disabled', 'disabled');
|
||||
children[i].querySelector('.woocommerce_variable_attributes')
|
||||
.getElementsByClassName('wc_input_subscription_trial_period')[0]
|
||||
.setAttribute('disabled', 'disabled');
|
||||
children[i].querySelector('.woocommerce_variable_attributes')
|
||||
.getElementsByClassName('wc_input_subscription_length')[0]
|
||||
.setAttribute('disabled', 'disabled');
|
||||
}
|
||||
}
|
||||
}
|
||||
document.addEventListener( 'DOMContentLoaded', () => {
|
||||
const variations = document.querySelector( '.woocommerce_variations' );
|
||||
const disableFields = ( productId ) => {
|
||||
if ( variations ) {
|
||||
const children = variations.children;
|
||||
for ( let i = 0; i < children.length; i++ ) {
|
||||
const variableId = children[ i ]
|
||||
.querySelector( 'h3' )
|
||||
.getElementsByClassName( 'variable_post_id' )[ 0 ].value;
|
||||
if ( parseInt( variableId ) === productId ) {
|
||||
children[ i ]
|
||||
.querySelector( '.woocommerce_variable_attributes' )
|
||||
.getElementsByClassName(
|
||||
'wc_input_subscription_period_interval'
|
||||
)[ 0 ]
|
||||
.setAttribute( 'disabled', 'disabled' );
|
||||
children[ i ]
|
||||
.querySelector( '.woocommerce_variable_attributes' )
|
||||
.getElementsByClassName(
|
||||
'wc_input_subscription_period'
|
||||
)[ 0 ]
|
||||
.setAttribute( 'disabled', 'disabled' );
|
||||
children[ i ]
|
||||
.querySelector( '.woocommerce_variable_attributes' )
|
||||
.getElementsByClassName(
|
||||
'wc_input_subscription_trial_length'
|
||||
)[ 0 ]
|
||||
.setAttribute( 'disabled', 'disabled' );
|
||||
children[ i ]
|
||||
.querySelector( '.woocommerce_variable_attributes' )
|
||||
.getElementsByClassName(
|
||||
'wc_input_subscription_trial_period'
|
||||
)[ 0 ]
|
||||
.setAttribute( 'disabled', 'disabled' );
|
||||
children[ i ]
|
||||
.querySelector( '.woocommerce_variable_attributes' )
|
||||
.getElementsByClassName(
|
||||
'wc_input_subscription_length'
|
||||
)[ 0 ]
|
||||
.setAttribute( 'disabled', 'disabled' );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const periodInterval = document.querySelector('#_subscription_period_interval');
|
||||
periodInterval.setAttribute('disabled', 'disabled');
|
||||
const periodInterval = document.querySelector(
|
||||
'#_subscription_period_interval'
|
||||
);
|
||||
periodInterval.setAttribute( 'disabled', 'disabled' );
|
||||
|
||||
const subscriptionPeriod = document.querySelector('#_subscription_period');
|
||||
subscriptionPeriod.setAttribute('disabled', 'disabled');
|
||||
const subscriptionPeriod = document.querySelector(
|
||||
'#_subscription_period'
|
||||
);
|
||||
subscriptionPeriod.setAttribute( 'disabled', 'disabled' );
|
||||
|
||||
const subscriptionLength = document.querySelector('._subscription_length_field');
|
||||
subscriptionLength.style.display = 'none';
|
||||
const subscriptionLength = document.querySelector(
|
||||
'._subscription_length_field'
|
||||
);
|
||||
subscriptionLength.style.display = 'none';
|
||||
|
||||
const subscriptionTrial = document.querySelector('._subscription_trial_length_field');
|
||||
subscriptionTrial.style.display = 'none';
|
||||
}
|
||||
const subscriptionTrial = document.querySelector(
|
||||
'._subscription_trial_length_field'
|
||||
);
|
||||
subscriptionTrial.style.display = 'none';
|
||||
|
||||
const setupProducts = () => {
|
||||
PayPalCommerceGatewayPayPalSubscriptionProducts?.forEach((product) => {
|
||||
if(product.product_connected === 'yes') {
|
||||
disableFields(product.product_id);
|
||||
}
|
||||
const soldIndividually = document.querySelector(
|
||||
'#_sold_individually'
|
||||
);
|
||||
soldIndividually.setAttribute( 'disabled', 'disabled' );
|
||||
};
|
||||
|
||||
const unlinkBtn = document.getElementById(`ppcp-unlink-sub-plan-${product.product_id}`);
|
||||
unlinkBtn?.addEventListener('click', (event)=>{
|
||||
event.preventDefault();
|
||||
unlinkBtn.disabled = true;
|
||||
const spinner = document.getElementById('spinner-unlink-plan');
|
||||
spinner.style.display = 'inline-block';
|
||||
const setupProducts = () => {
|
||||
PayPalCommerceGatewayPayPalSubscriptionProducts?.forEach(
|
||||
( product ) => {
|
||||
if ( product.product_connected === 'yes' ) {
|
||||
disableFields( product.product_id );
|
||||
}
|
||||
|
||||
fetch(product.ajax.deactivate_plan.endpoint, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
credentials: 'same-origin',
|
||||
body: JSON.stringify({
|
||||
nonce: product.ajax.deactivate_plan.nonce,
|
||||
plan_id: product.plan_id,
|
||||
product_id: product.product_id
|
||||
})
|
||||
}).then(function (res) {
|
||||
return res.json();
|
||||
}).then(function (data) {
|
||||
if (!data.success) {
|
||||
unlinkBtn.disabled = false;
|
||||
spinner.style.display = 'none';
|
||||
console.error(data);
|
||||
throw Error(data.data.message);
|
||||
}
|
||||
const unlinkBtn = document.getElementById(
|
||||
`ppcp-unlink-sub-plan-${ product.product_id }`
|
||||
);
|
||||
unlinkBtn?.addEventListener( 'click', ( event ) => {
|
||||
event.preventDefault();
|
||||
unlinkBtn.disabled = true;
|
||||
const spinner = document.getElementById(
|
||||
'spinner-unlink-plan'
|
||||
);
|
||||
spinner.style.display = 'inline-block';
|
||||
|
||||
const enableSubscription = document.getElementById('ppcp-enable-subscription');
|
||||
const product = document.getElementById('pcpp-product');
|
||||
const plan = document.getElementById('pcpp-plan');
|
||||
enableSubscription.style.display = 'none';
|
||||
product.style.display = 'none';
|
||||
plan.style.display = 'none';
|
||||
fetch( product.ajax.deactivate_plan.endpoint, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
credentials: 'same-origin',
|
||||
body: JSON.stringify( {
|
||||
nonce: product.ajax.deactivate_plan.nonce,
|
||||
plan_id: product.plan_id,
|
||||
product_id: product.product_id,
|
||||
} ),
|
||||
} )
|
||||
.then( function ( res ) {
|
||||
return res.json();
|
||||
} )
|
||||
.then( function ( data ) {
|
||||
if ( ! data.success ) {
|
||||
unlinkBtn.disabled = false;
|
||||
spinner.style.display = 'none';
|
||||
console.error( data );
|
||||
throw Error( data.data.message );
|
||||
}
|
||||
|
||||
const enable_subscription_product = document.getElementById('ppcp_enable_subscription_product');
|
||||
enable_subscription_product.disabled = true;
|
||||
const enableSubscription = document.getElementById(
|
||||
'ppcp-enable-subscription'
|
||||
);
|
||||
const product =
|
||||
document.getElementById( 'pcpp-product' );
|
||||
const plan = document.getElementById( 'pcpp-plan' );
|
||||
enableSubscription.style.display = 'none';
|
||||
product.style.display = 'none';
|
||||
plan.style.display = 'none';
|
||||
|
||||
const planUnlinked = document.getElementById('pcpp-plan-unlinked');
|
||||
planUnlinked.style.display = 'block';
|
||||
const enable_subscription_product =
|
||||
document.getElementById(
|
||||
'ppcp_enable_subscription_product'
|
||||
);
|
||||
enable_subscription_product.disabled = true;
|
||||
|
||||
setTimeout(() => {
|
||||
location.reload();
|
||||
}, 1000)
|
||||
});
|
||||
});
|
||||
})
|
||||
}
|
||||
const planUnlinked =
|
||||
document.getElementById( 'pcpp-plan-unlinked' );
|
||||
planUnlinked.style.display = 'block';
|
||||
|
||||
setupProducts();
|
||||
jQuery( '#woocommerce-product-data' ).on('woocommerce_variations_loaded', () => {
|
||||
setupProducts();
|
||||
});
|
||||
});
|
||||
setTimeout( () => {
|
||||
location.reload();
|
||||
}, 1000 );
|
||||
} );
|
||||
} );
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
setupProducts();
|
||||
jQuery( '#woocommerce-product-data' ).on(
|
||||
'woocommerce_variations_loaded',
|
||||
() => {
|
||||
setupProducts();
|
||||
}
|
||||
);
|
||||
} );
|
||||
|
|
|
@ -64,6 +64,14 @@ class PayPalSubscriptionsModule implements ServiceModule, ExtendingModule, Execu
|
|||
* @psalm-suppress MissingClosureParamType
|
||||
*/
|
||||
function( $product_id ) use ( $c ) {
|
||||
$subscriptions_helper = $c->get( 'wc-subscriptions.helper' );
|
||||
assert( $subscriptions_helper instanceof SubscriptionHelper );
|
||||
|
||||
$connect_subscription = wc_clean( wp_unslash( $_POST['_ppcp_enable_subscription_product'] ?? '' ) );
|
||||
if ( ! $subscriptions_helper->plugin_is_active() || $connect_subscription !== 'yes' ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$settings = $c->get( 'wcgateway.settings' );
|
||||
assert( $settings instanceof Settings );
|
||||
|
||||
|
@ -93,6 +101,60 @@ class PayPalSubscriptionsModule implements ServiceModule, ExtendingModule, Execu
|
|||
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 ) use ( $c ) {
|
||||
if ( WC()->cart->is_empty() ) {
|
||||
return $passed_validation;
|
||||
}
|
||||
|
||||
$product = wc_get_product( $product_id );
|
||||
|
||||
if ( ! ( is_a( $product, WC_Product::class ) ) ) {
|
||||
wc_add_notice( __( 'Cannot add this product to cart (invalid product).', 'woocommerce-paypal-payments' ), 'error' );
|
||||
return false;
|
||||
}
|
||||
|
||||
$settings = $c->get( 'wcgateway.settings' );
|
||||
assert( $settings instanceof Settings );
|
||||
|
||||
$subscriptions_mode = $settings->has( 'subscriptions_mode' ) ? $settings->get( 'subscriptions_mode' ) : '';
|
||||
$is_paypal_subscription = static function ( $product ) use ( $subscriptions_mode ): bool {
|
||||
return $product &&
|
||||
in_array( $product->get_type(), array( 'subscription', 'variable-subscription' ), true ) &&
|
||||
'subscriptions_api' === $subscriptions_mode &&
|
||||
$product->get_meta( '_ppcp_enable_subscription_product', true ) === 'yes';
|
||||
};
|
||||
|
||||
if ( $is_paypal_subscription( $product ) ) {
|
||||
if ( ! $product->get_sold_individually() ) {
|
||||
$product->set_sold_individually( true );
|
||||
$product->save();
|
||||
}
|
||||
|
||||
wc_add_notice( __( 'You cannot add a PayPal Subscription product to a cart with other items.', 'woocommerce-paypal-payments' ), 'error' );
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ( WC()->cart->get_cart() as $cart_item ) {
|
||||
$cart_product = wc_get_product( $cart_item['product_id'] );
|
||||
if ( $is_paypal_subscription( $cart_product ) ) {
|
||||
wc_add_notice( __( 'You can only have one PayPal Subscription product in your cart.', 'woocommerce-paypal-payments' ), 'error' );
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return $passed_validation;
|
||||
},
|
||||
10,
|
||||
2
|
||||
);
|
||||
|
||||
add_action(
|
||||
'woocommerce_save_product_variation',
|
||||
/**
|
||||
|
@ -662,12 +724,18 @@ class PayPalSubscriptionsModule implements ServiceModule, ExtendingModule, Execu
|
|||
// 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