Merge branch 'trunk' into PCP-2722-add-block-checkout-compatibility-to-advanced-card-processing

This commit is contained in:
Emili Castells Guasch 2024-06-03 09:37:44 +02:00
commit c973871980
8 changed files with 211 additions and 150 deletions

View file

@ -568,7 +568,7 @@ if(cartHasSubscriptionProducts(config.scriptData)) {
features.push('subscriptions');
}
if (block_enabled) {
if (block_enabled && config.enabled) {
if ((config.addPlaceOrderMethod || config.usePlaceOrder) && !config.scriptData.continuation) {
let descriptionElement = <div dangerouslySetInnerHTML={{__html: config.description}}></div>;
if (config.placeOrderButtonDescription) {

View file

@ -73,34 +73,34 @@ class GermanizedShipmentIntegration implements Integration {
add_action(
'woocommerce_gzd_shipment_status_shipped',
function( int $shipment_id, Shipment $shipment ) {
if ( ! apply_filters( 'woocommerce_paypal_payments_sync_gzd_tracking', true ) ) {
return;
}
$wc_order = $shipment->get_order();
if ( ! is_a( $wc_order, WC_Order::class ) ) {
return;
}
$paypal_order = ppcp_get_paypal_order( $wc_order );
$capture_id = $this->get_paypal_order_transaction_id( $paypal_order );
$wc_order_id = $wc_order->get_id();
$tracking_number = $shipment->get_tracking_id();
$carrier = $shipment->get_shipping_provider();
$items = array_map(
function ( ShipmentItem $item ): int {
return $item->get_order_item_id();
},
$shipment->get_items()
);
if ( ! $tracking_number || ! $carrier || ! $capture_id ) {
return;
}
try {
if ( ! apply_filters( 'woocommerce_paypal_payments_sync_gzd_tracking', true ) ) {
return;
}
$wc_order = $shipment->get_order();
if ( ! is_a( $wc_order, WC_Order::class ) ) {
return;
}
$paypal_order = ppcp_get_paypal_order( $wc_order );
$capture_id = $this->get_paypal_order_transaction_id( $paypal_order );
$wc_order_id = $wc_order->get_id();
$tracking_number = $shipment->get_tracking_id();
$carrier = $shipment->get_shipping_provider();
$items = array_map(
function ( ShipmentItem $item ): int {
return $item->get_order_item_id();
},
$shipment->get_items()
);
if ( ! $tracking_number || ! $carrier || ! $capture_id ) {
return;
}
$ppcp_shipment = $this->shipment_factory->create_shipment(
$wc_order_id,
$capture_id,
@ -118,7 +118,7 @@ class GermanizedShipmentIntegration implements Integration {
: $this->endpoint->add_tracking_information( $ppcp_shipment, $wc_order_id );
} catch ( Exception $exception ) {
$this->logger->error( "Couldn't sync tracking information: " . $exception->getMessage() );
return;
}
},
500,

View file

@ -76,25 +76,25 @@ class ShipStationIntegration implements Integration {
* @psalm-suppress MissingClosureParamType
*/
function( $wc_order, array $data ) {
if ( ! apply_filters( 'woocommerce_paypal_payments_sync_ship_station_tracking', true ) ) {
return;
}
if ( ! is_a( $wc_order, WC_Order::class ) ) {
return;
}
$paypal_order = ppcp_get_paypal_order( $wc_order );
$capture_id = $this->get_paypal_order_transaction_id( $paypal_order );
$order_id = $wc_order->get_id();
$tracking_number = $data['tracking_number'] ?? '';
$carrier = $data['carrier'] ?? '';
if ( ! $tracking_number || ! is_string( $tracking_number ) || ! $carrier || ! is_string( $carrier ) || ! $capture_id ) {
return;
}
try {
if ( ! apply_filters( 'woocommerce_paypal_payments_sync_ship_station_tracking', true ) ) {
return;
}
if ( ! is_a( $wc_order, WC_Order::class ) ) {
return;
}
$paypal_order = ppcp_get_paypal_order( $wc_order );
$capture_id = $this->get_paypal_order_transaction_id( $paypal_order );
$order_id = $wc_order->get_id();
$tracking_number = $data['tracking_number'] ?? '';
$carrier = $data['carrier'] ?? '';
if ( ! $tracking_number || ! is_string( $tracking_number ) || ! $carrier || ! is_string( $carrier ) || ! $capture_id ) {
return;
}
$ppcp_shipment = $this->shipment_factory->create_shipment(
$order_id,
$capture_id,
@ -112,7 +112,7 @@ class ShipStationIntegration implements Integration {
: $this->endpoint->add_tracking_information( $ppcp_shipment, $order_id );
} catch ( Exception $exception ) {
$this->logger->error( "Couldn't sync tracking information: " . $exception->getMessage() );
return;
}
},
500,

View file

@ -73,30 +73,35 @@ class ShipmentTrackingIntegration implements Integration {
add_action(
'wp_ajax_wc_shipment_tracking_save_form',
function() {
check_ajax_referer( 'create-tracking-item', 'security', true );
try {
check_ajax_referer( 'create-tracking-item', 'security', true );
if ( ! apply_filters( 'woocommerce_paypal_payments_sync_wc_shipment_tracking', true ) ) {
if ( ! apply_filters( 'woocommerce_paypal_payments_sync_wc_shipment_tracking', true ) ) {
return;
}
$order_id = (int) wc_clean( wp_unslash( $_POST['order_id'] ?? '' ) );
$wc_order = wc_get_order( $order_id );
if ( ! is_a( $wc_order, WC_Order::class ) ) {
return;
}
$paypal_order = ppcp_get_paypal_order( $wc_order );
$capture_id = $this->get_paypal_order_transaction_id( $paypal_order );
$tracking_number = wc_clean( wp_unslash( $_POST['tracking_number'] ?? '' ) );
$carrier = wc_clean( wp_unslash( $_POST['tracking_provider'] ?? '' ) );
$carrier_other = wc_clean( wp_unslash( $_POST['custom_tracking_provider'] ?? '' ) );
$carrier = $carrier ?: $carrier_other ?: '';
if ( ! $tracking_number || ! is_string( $tracking_number ) || ! $carrier || ! is_string( $carrier ) || ! $capture_id ) {
return;
}
$this->sync_tracking( $order_id, $capture_id, $tracking_number, $carrier );
} catch ( Exception $exception ) {
return;
}
$order_id = (int) wc_clean( wp_unslash( $_POST['order_id'] ?? '' ) );
$wc_order = wc_get_order( $order_id );
if ( ! is_a( $wc_order, WC_Order::class ) ) {
return;
}
$paypal_order = ppcp_get_paypal_order( $wc_order );
$capture_id = $this->get_paypal_order_transaction_id( $paypal_order );
$tracking_number = wc_clean( wp_unslash( $_POST['tracking_number'] ?? '' ) );
$carrier = wc_clean( wp_unslash( $_POST['tracking_provider'] ?? '' ) );
$carrier_other = wc_clean( wp_unslash( $_POST['custom_tracking_provider'] ?? '' ) );
$carrier = $carrier ?: $carrier_other ?: '';
if ( ! $tracking_number || ! is_string( $tracking_number ) || ! $carrier || ! is_string( $carrier ) || ! $capture_id ) {
return;
}
$this->sync_tracking( $order_id, $capture_id, $tracking_number, $carrier );
}
);
@ -106,34 +111,39 @@ class ShipmentTrackingIntegration implements Integration {
add_filter(
'woocommerce_rest_prepare_order_shipment_tracking',
function( WP_REST_Response $response, array $tracking_item, WP_REST_Request $request ): WP_REST_Response {
if ( ! apply_filters( 'woocommerce_paypal_payments_sync_wc_shipment_tracking', true ) ) {
try {
if ( ! apply_filters( 'woocommerce_paypal_payments_sync_wc_shipment_tracking', true ) ) {
return $response;
}
$callback = $request->get_attributes()['callback']['1'] ?? '';
if ( $callback !== 'create_item' ) {
return $response;
}
$order_id = $tracking_item['order_id'] ?? 0;
$wc_order = wc_get_order( $order_id );
if ( ! is_a( $wc_order, WC_Order::class ) ) {
return $response;
}
$paypal_order = ppcp_get_paypal_order( $wc_order );
$capture_id = $this->get_paypal_order_transaction_id( $paypal_order );
$tracking_number = $tracking_item['tracking_number'] ?? '';
$carrier = $tracking_item['tracking_provider'] ?? '';
$carrier_other = $tracking_item['custom_tracking_provider'] ?? '';
$carrier = $carrier ?: $carrier_other ?: '';
if ( ! $tracking_number || ! $carrier || ! $capture_id ) {
return $response;
}
$this->sync_tracking( $order_id, $capture_id, $tracking_number, $carrier );
} catch ( Exception $exception ) {
return $response;
}
$callback = $request->get_attributes()['callback']['1'] ?? '';
if ( $callback !== 'create_item' ) {
return $response;
}
$order_id = $tracking_item['order_id'] ?? 0;
$wc_order = wc_get_order( $order_id );
if ( ! is_a( $wc_order, WC_Order::class ) ) {
return $response;
}
$paypal_order = ppcp_get_paypal_order( $wc_order );
$capture_id = $this->get_paypal_order_transaction_id( $paypal_order );
$tracking_number = $tracking_item['tracking_number'] ?? '';
$carrier = $tracking_item['tracking_provider'] ?? '';
$carrier_other = $tracking_item['custom_tracking_provider'] ?? '';
$carrier = $carrier ?: $carrier_other ?: '';
if ( ! $tracking_number || ! $carrier || ! $capture_id ) {
return $response;
}
$this->sync_tracking( $order_id, $capture_id, $tracking_number, $carrier );
return $response;
},
10,

View file

@ -75,44 +75,48 @@ class WcShippingTaxIntegration implements Integration {
add_filter(
'rest_post_dispatch',
function( WP_HTTP_Response $response, WP_REST_Server $server, WP_REST_Request $request ): WP_HTTP_Response {
if ( ! apply_filters( 'woocommerce_paypal_payments_sync_wc_shipping_tax', true ) ) {
try {
if ( ! apply_filters( 'woocommerce_paypal_payments_sync_wc_shipping_tax', true ) ) {
return $response;
}
$params = $request->get_params();
$order_id = (int) ( $params['order_id'] ?? 0 );
$label_id = (int) ( $params['label_ids'] ?? 0 );
if ( ! $order_id || "/wc/v1/connect/label/{$order_id}/{$label_id}" !== $request->get_route() ) {
return $response;
}
$data = $response->get_data() ?? array();
$labels = $data['labels'] ?? array();
foreach ( $labels as $label ) {
$tracking_number = $label['tracking'] ?? '';
if ( ! $tracking_number ) {
continue;
}
$wc_order = wc_get_order( $order_id );
if ( ! is_a( $wc_order, WC_Order::class ) ) {
continue;
}
$paypal_order = ppcp_get_paypal_order( $wc_order );
$capture_id = $this->get_paypal_order_transaction_id( $paypal_order );
$carrier = $label['carrier_id'] ?? $label['service_name'] ?? '';
$items = array_map( 'intval', $label['product_ids'] ?? array() );
if ( ! $carrier || ! $capture_id ) {
continue;
}
$this->sync_tracking( $order_id, $capture_id, $tracking_number, $carrier, $items );
}
} catch ( Exception $exception ) {
return $response;
}
$params = $request->get_params();
$order_id = (int) ( $params['order_id'] ?? 0 );
$label_id = (int) ( $params['label_ids'] ?? 0 );
if ( ! $order_id || "/wc/v1/connect/label/{$order_id}/{$label_id}" !== $request->get_route() ) {
return $response;
}
$data = $response->get_data() ?? array();
$labels = $data['labels'] ?? array();
foreach ( $labels as $label ) {
$tracking_number = $label['tracking'] ?? '';
if ( ! $tracking_number ) {
continue;
}
$wc_order = wc_get_order( $order_id );
if ( ! is_a( $wc_order, WC_Order::class ) ) {
continue;
}
$paypal_order = ppcp_get_paypal_order( $wc_order );
$capture_id = $this->get_paypal_order_transaction_id( $paypal_order );
$carrier = $label['carrier_id'] ?? $label['service_name'] ?? '';
$items = array_map( 'intval', $label['product_ids'] ?? array() );
if ( ! $carrier || ! $capture_id ) {
continue;
}
$this->sync_tracking( $order_id, $capture_id, $tracking_number, $carrier, $items );
}
return $response;
},
10,

View file

@ -71,27 +71,27 @@ class YithShipmentIntegration implements Integration {
add_action(
'woocommerce_process_shop_order_meta',
function( int $order_id ) {
if ( ! apply_filters( 'woocommerce_paypal_payments_sync_ywot_tracking', true ) ) {
return;
}
$wc_order = wc_get_order( $order_id );
if ( ! is_a( $wc_order, WC_Order::class ) ) {
return;
}
$paypal_order = ppcp_get_paypal_order( $wc_order );
$capture_id = $this->get_paypal_order_transaction_id( $paypal_order );
// phpcs:ignore WordPress.Security.NonceVerification.Missing
$tracking_number = wc_clean( wp_unslash( $_POST['ywot_tracking_code'] ?? '' ) );
// phpcs:ignore WordPress.Security.NonceVerification.Missing
$carrier = wc_clean( wp_unslash( $_POST['ywot_carrier_name'] ?? '' ) );
if ( ! $tracking_number || ! is_string( $tracking_number ) || ! $carrier || ! is_string( $carrier ) || ! $capture_id ) {
return;
}
try {
if ( ! apply_filters( 'woocommerce_paypal_payments_sync_ywot_tracking', true ) ) {
return;
}
$wc_order = wc_get_order( $order_id );
if ( ! is_a( $wc_order, WC_Order::class ) ) {
return;
}
$paypal_order = ppcp_get_paypal_order( $wc_order );
$capture_id = $this->get_paypal_order_transaction_id( $paypal_order );
// phpcs:ignore WordPress.Security.NonceVerification.Missing
$tracking_number = wc_clean( wp_unslash( $_POST['ywot_tracking_code'] ?? '' ) );
// phpcs:ignore WordPress.Security.NonceVerification.Missing
$carrier = wc_clean( wp_unslash( $_POST['ywot_carrier_name'] ?? '' ) );
if ( ! $tracking_number || ! is_string( $tracking_number ) || ! $carrier || ! is_string( $carrier ) || ! $capture_id ) {
return;
}
$ppcp_shipment = $this->shipment_factory->create_shipment(
$order_id,
$capture_id,
@ -109,7 +109,7 @@ class YithShipmentIntegration implements Integration {
: $this->endpoint->add_tracking_information( $ppcp_shipment, $order_id );
} catch ( Exception $exception ) {
$this->logger->error( "Couldn't sync tracking information: " . $exception->getMessage() );
return;
}
},
500,

View file

@ -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 = () => {

View file

@ -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;
}