Skip to content

Block Shipping, Products and Coupons when Sync Push is disabled #2810

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/API/Google/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@

namespace Automattic\WooCommerce\GoogleListingsAndAds\API\Google;

use Automattic\WooCommerce\GoogleListingsAndAds\API\WP\NotificationsService;
use Automattic\WooCommerce\GoogleListingsAndAds\DB\Query\ShippingRateQuery;
use Automattic\WooCommerce\GoogleListingsAndAds\DB\Query\ShippingTimeQuery;
use Automattic\WooCommerce\GoogleListingsAndAds\Internal\ContainerAwareTrait;
use Automattic\WooCommerce\GoogleListingsAndAds\Internal\Interfaces\ContainerAwareInterface;
use Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter\MerchantCenterService;
use Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter\TargetAudience;
use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsInterface;
use Automattic\WooCommerce\GoogleListingsAndAds\Proxies\WC;
Expand Down Expand Up @@ -65,7 +67,9 @@
* Sync the shipping settings with Google.
*/
public function sync_shipping() {
if ( ! $this->should_sync_shipping() ) {
/** @var MerchantCenterService $merchant_center */
$merchant_center = $this->container->get( MerchantCenterService::class );
if ( ! $this->should_sync_shipping() || ! $merchant_center->is_enabled_for_datatype( NotificationsService::DATATYPE_SHIPPING ) ) {

Check warning on line 72 in src/API/Google/Settings.php

View check run for this annotation

Codecov / codecov/patch

src/API/Google/Settings.php#L71-L72

Added lines #L71 - L72 were not covered by tests
return;
}

Expand Down
42 changes: 40 additions & 2 deletions src/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@
$this->response .= 'Failed to connect to Google.';
}

$notification_service = new NotificationsService( $this->container->get( MerchantCenterService::class ), $this->container->get( AccountService::class ) );
$notification_service->set_options_object( $options );

Check warning on line 140 in src/ConnectionTest.php

View check run for this annotation

Codecov / codecov/patch

src/ConnectionTest.php#L139-L140

Added lines #L139 - L140 were not covered by tests

?>
<div class="wrap">
<h2>Connection Test</h2>
Expand Down Expand Up @@ -572,7 +575,44 @@
<hr />

<h2 class="title">Product Sync</h2>
<table class="form-table" role="presentation">
<tr>
<th><label>Products MC PUSH:</label></th>
<td>
<p>

<code><?php echo $this->enabled_or_disabled( $notification_service->is_push_enabled_for_datatype( NotificationsService::DATATYPE_PRODUCT ) ); ?></code>

Check warning on line 584 in src/ConnectionTest.php

View check run for this annotation

Codecov / codecov/patch

src/ConnectionTest.php#L584

Added line #L584 was not covered by tests
</p>
</td>
</tr>
<tr>
<th><label>Coupons MC PUSH:</label></th>
<td>
<p>

<code><?php echo $this->enabled_or_disabled( $notification_service->is_push_enabled_for_datatype( NotificationsService::DATATYPE_COUPON ) ); ?></code>

Check warning on line 593 in src/ConnectionTest.php

View check run for this annotation

Codecov / codecov/patch

src/ConnectionTest.php#L593

Added line #L593 was not covered by tests
</p>
</td>
</tr>
<tr>
<th><label>Shipping MC PUSH:</label></th>
<td>
<p>

<code><?php echo $this->enabled_or_disabled( $notification_service->is_push_enabled_for_datatype( NotificationsService::DATATYPE_SHIPPING ) ); ?></code>

Check warning on line 602 in src/ConnectionTest.php

View check run for this annotation

Codecov / codecov/patch

src/ConnectionTest.php#L602

Added line #L602 was not covered by tests
</p>
</td>
</tr>
<tr>
<th><label>Settings MC PUSH:</label></th>
<td>
<p>

<code><?php echo $this->enabled_or_disabled( $notification_service->is_push_enabled_for_datatype( NotificationsService::DATATYPE_SETTINGS ) ); ?></code>

Check warning on line 611 in src/ConnectionTest.php

View check run for this annotation

Codecov / codecov/patch

src/ConnectionTest.php#L611

Added line #L611 was not covered by tests
</p>
</td>
</tr>
</table>
<form action="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>" method="GET">
<table class="form-table" role="presentation">
<tr>
Expand Down Expand Up @@ -673,8 +713,6 @@
<?php
$options = $this->container->get( OptionsInterface::class );
$wp_api_status = $options->get( OptionsInterface::WPCOM_REST_API_STATUS );
$notification_service = new NotificationsService( $this->container->get( MerchantCenterService::class ), $this->container->get( AccountService::class ) );
$notification_service->set_options_object( $options );
?>
<h2 class="title">Partner API Pull Integration</h2>
<form action="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>" method="GET">
Expand Down
20 changes: 18 additions & 2 deletions src/Coupon/CouponSyncer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
declare(strict_types = 1);
namespace Automattic\WooCommerce\GoogleListingsAndAds\Coupon;

use Automattic\WooCommerce\GoogleListingsAndAds\API\WP\NotificationsService;
use Automattic\WooCommerce\GoogleListingsAndAds\Google\DeleteCouponEntry;
use Automattic\WooCommerce\GoogleListingsAndAds\Google\GooglePromotionService;
use Automattic\WooCommerce\GoogleListingsAndAds\Google\InvalidCouponEntry;
Expand Down Expand Up @@ -479,13 +480,28 @@
if ( ! $this->merchant_center->should_push() ) {
do_action(
'woocommerce_gla_error',
'Cannot push any coupons because they are being fetched automatically.',
'Cannot push any coupons because your store is not ready for syncing.',

Check warning on line 483 in src/Coupon/CouponSyncer.php

View check run for this annotation

Codecov / codecov/patch

src/Coupon/CouponSyncer.php#L483

Added line #L483 was not covered by tests
__METHOD__
);

throw new CouponSyncerException(
__(
'Pushing Coupons will not run if the automatic data fetching is enabled. Please review your configuration in Google Listing and Ads settings.',
'Pushing coupons will not run if the store is not ready for syncing.',
'google-listings-and-ads'
)
);

Check warning on line 492 in src/Coupon/CouponSyncer.php

View check run for this annotation

Codecov / codecov/patch

src/Coupon/CouponSyncer.php#L489-L492

Added lines #L489 - L492 were not covered by tests
}

if ( ! $this->merchant_center->is_enabled_for_datatype( NotificationsService::DATATYPE_COUPON ) ) {
do_action(
'woocommerce_gla_error',
'Cannot push any coupons because the syncing feature has been disabled on your store.',
__METHOD__
);

throw new CouponSyncerException(
__(
'Pushing Coupons will not run if the PUSH Sync functionality has been disabled.',
'google-listings-and-ads'
)
);
Expand Down
15 changes: 15 additions & 0 deletions src/Coupon/SyncerHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@
$this->handle_update_coupon_notification( $coupon );
}

// Only proceed with coupon syncing if PUSH is enabled for this data type
if ( ! $this->merchant_center->is_enabled_for_datatype( NotificationsService::DATATYPE_COUPON ) ) {
return;

Check warning on line 203 in src/Coupon/SyncerHooks.php

View check run for this annotation

Codecov / codecov/patch

src/Coupon/SyncerHooks.php#L203

Added line #L203 was not covered by tests
}

// Schedule an update job if product sync is enabled.
if ( $this->coupon_helper->is_sync_ready( $coupon ) ) {
$this->coupon_helper->mark_as_pending( $coupon );
Expand Down Expand Up @@ -241,6 +246,11 @@
protected function handle_pre_delete_coupon( int $coupon_id ) {
$coupon = $this->wc->maybe_get_coupon( $coupon_id );

// Only proceed with coupon deletion if PUSH is enabled for this data type
if ( ! $this->merchant_center->is_enabled_for_datatype( NotificationsService::DATATYPE_COUPON ) ) {
return;

Check warning on line 251 in src/Coupon/SyncerHooks.php

View check run for this annotation

Codecov / codecov/patch

src/Coupon/SyncerHooks.php#L251

Added line #L251 was not covered by tests
}

if ( $coupon instanceof WC_Coupon &&
$this->coupon_helper->is_coupon_synced( $coupon ) ) {
$this->delete_requests_map[ $coupon_id ] = new DeleteCouponEntry(
Expand Down Expand Up @@ -281,6 +291,11 @@
$this->maybe_send_delete_notification( $coupon_id );
}

// Only proceed with coupon deletion if PUSH is enabled for this data type
if ( ! $this->merchant_center->is_enabled_for_datatype( NotificationsService::DATATYPE_COUPON ) ) {
return;

Check warning on line 296 in src/Coupon/SyncerHooks.php

View check run for this annotation

Codecov / codecov/patch

src/Coupon/SyncerHooks.php#L296

Added line #L296 was not covered by tests
}

if ( ! isset( $this->delete_requests_map[ $coupon_id ] ) ) {
return;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Jobs/AbstractCouponSyncerJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Automattic\WooCommerce\GoogleListingsAndAds\Jobs;

use Automattic\WooCommerce\GoogleListingsAndAds\ActionScheduler\ActionSchedulerInterface;
use Automattic\WooCommerce\GoogleListingsAndAds\API\WP\NotificationsService;
use Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter\MerchantCenterService;
use Automattic\WooCommerce\GoogleListingsAndAds\Coupon\CouponHelper;
use Automattic\WooCommerce\GoogleListingsAndAds\Coupon\CouponSyncer;
Expand Down Expand Up @@ -72,6 +73,6 @@
* @return bool Returns true if the job can be scheduled.
*/
public function can_schedule( $args = [] ): bool {
return ! $this->is_running( $args ) && $this->merchant_center->should_push();
return ! $this->is_running( $args ) && $this->merchant_center->is_enabled_for_datatype( NotificationsService::DATATYPE_COUPON );

Check warning on line 76 in src/Jobs/AbstractCouponSyncerJob.php

View check run for this annotation

Codecov / codecov/patch

src/Jobs/AbstractCouponSyncerJob.php#L76

Added line #L76 was not covered by tests
}
}
3 changes: 2 additions & 1 deletion src/Jobs/AbstractProductSyncerBatchedJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Automattic\WooCommerce\GoogleListingsAndAds\Jobs;

use Automattic\WooCommerce\GoogleListingsAndAds\ActionScheduler\ActionSchedulerInterface;
use Automattic\WooCommerce\GoogleListingsAndAds\API\WP\NotificationsService;
use Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter\MerchantCenterService;
use Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter\MerchantStatuses;
use Automattic\WooCommerce\GoogleListingsAndAds\Product\BatchProductHelper;
Expand Down Expand Up @@ -80,6 +81,6 @@ public function __construct(
* @return bool Returns true if the job can be scheduled.
*/
public function can_schedule( $args = [] ): bool {
return ! $this->is_running( $args ) && $this->merchant_center->should_push();
return ! $this->is_running( $args ) && $this->merchant_center->is_enabled_for_datatype( NotificationsService::DATATYPE_PRODUCT );
}
}
3 changes: 2 additions & 1 deletion src/Jobs/AbstractProductSyncerJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Automattic\WooCommerce\GoogleListingsAndAds\Jobs;

use Automattic\WooCommerce\GoogleListingsAndAds\ActionScheduler\ActionSchedulerInterface;
use Automattic\WooCommerce\GoogleListingsAndAds\API\WP\NotificationsService;
use Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter\MerchantCenterService;
use Automattic\WooCommerce\GoogleListingsAndAds\Product\ProductRepository;
use Automattic\WooCommerce\GoogleListingsAndAds\Product\ProductSyncer;
Expand Down Expand Up @@ -62,6 +63,6 @@ public function __construct(
* @return bool Returns true if the job can be scheduled.
*/
public function can_schedule( $args = [] ): bool {
return ! $this->is_running( $args ) && $this->merchant_center->should_push();
return ! $this->is_running( $args ) && $this->merchant_center->is_enabled_for_datatype( NotificationsService::DATATYPE_PRODUCT );
}
}
6 changes: 4 additions & 2 deletions src/Jobs/UpdateShippingSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Automattic\WooCommerce\GoogleListingsAndAds\ActionScheduler\ActionSchedulerInterface;
use Automattic\WooCommerce\GoogleListingsAndAds\API\Google\Settings as GoogleSettings;
use Automattic\WooCommerce\GoogleListingsAndAds\API\WP\NotificationsService;
use Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter\MerchantCenterService;

defined( 'ABSPATH' ) || exit;
Expand All @@ -21,6 +22,7 @@
* @since 2.1.0
*/
class UpdateShippingSettings extends AbstractActionSchedulerJob {

/**
* @var MerchantCenterService
*/
Expand Down Expand Up @@ -97,7 +99,7 @@ public function schedule( array $args = [] ) {
* @return bool
*/
protected function can_sync_shipping(): bool {
// Confirm that the Merchant Center account is connected and the user has chosen for the shipping rates to be synced from WooCommerce settings.
return $this->merchant_center->is_connected() && $this->google_settings->should_get_shipping_rates_from_woocommerce();
// Confirm that the Merchant Center account is connected, the user has chosen for the shipping rates to be synced from WooCommerce settings and the Push Sync is enabled for Shipping.
return $this->merchant_center->is_connected() && $this->google_settings->should_get_shipping_rates_from_woocommerce() && $this->merchant_center->is_enabled_for_datatype( NotificationsService::DATATYPE_SHIPPING );
}
}
22 changes: 19 additions & 3 deletions src/MerchantCenter/MerchantCenterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,33 @@ public function is_ready_for_syncing(): bool {
}

/**
* Whether we should push data into MC. Only if:
* - MC is ready for syncing {@see is_ready_for_syncing}
* - Notifications Service is not enabled
* Whether we should push data into MC. Only is MC is ready for syncing.
*
* @see is_ready_for_syncing
* @return bool
* @since 2.8.0
*/
public function should_push(): bool {
return $this->is_ready_for_syncing();
}

/**
* Whether push is enabled for a specific data type.
* This method checks if push synchronization is enabled for a specific data type
* (products, coupons, shipping, settings) in the Merchant Center.
*
* This differs from should_push() which checks if the Merchant Center is ready
* for syncing in general, while this method checks if a specific data type
* has been enabled for push operations.
*
* @param string $data_type The data type to check.
* @return bool True if push is enabled for the specified data type.
*/
public function is_enabled_for_datatype( string $data_type ): bool {
/** @var NotificationsService $notifications_service */
$notifications_service = $this->container->get( NotificationsService::class );
return $notifications_service->is_push_enabled_for_datatype( $data_type );
}

/**
* Get whether the country is supported by the Merchant Center.
Expand Down
20 changes: 18 additions & 2 deletions src/Product/ProductSyncer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Automattic\WooCommerce\GoogleListingsAndAds\Product;

use Automattic\WooCommerce\GoogleListingsAndAds\API\WP\NotificationsService;
use Automattic\WooCommerce\GoogleListingsAndAds\Google\BatchInvalidProductEntry;
use Automattic\WooCommerce\GoogleListingsAndAds\Google\BatchProductIDRequestEntry;
use Automattic\WooCommerce\GoogleListingsAndAds\Google\BatchProductRequestEntry;
Expand Down Expand Up @@ -365,13 +366,28 @@
if ( ! $this->merchant_center->should_push() ) {
do_action(
'woocommerce_gla_error',
'Cannot push any products because they are being fetched automatically.',
'Cannot push any products because your store is not ready for syncing.',

Check warning on line 369 in src/Product/ProductSyncer.php

View check run for this annotation

Codecov / codecov/patch

src/Product/ProductSyncer.php#L369

Added line #L369 was not covered by tests
__METHOD__
);

throw new ProductSyncerException(
__(
'Pushing products will not run if the automatic data fetching is enabled. Please review your configuration in Google Listing and Ads settings.',
'Pushing products will not run if the store is not ready for syncing.',
'google-listings-and-ads'
)
);

Check warning on line 378 in src/Product/ProductSyncer.php

View check run for this annotation

Codecov / codecov/patch

src/Product/ProductSyncer.php#L375-L378

Added lines #L375 - L378 were not covered by tests
}

if ( ! $this->merchant_center->is_enabled_for_datatype( NotificationsService::DATATYPE_PRODUCT ) ) {
do_action(
'woocommerce_gla_error',
'Cannot push any products because the syncing feature has been disabled on your store.',
__METHOD__
);

Check warning on line 386 in src/Product/ProductSyncer.php

View check run for this annotation

Codecov / codecov/patch

src/Product/ProductSyncer.php#L382-L386

Added lines #L382 - L386 were not covered by tests

throw new ProductSyncerException(
__(
'Pushing products will not run if the PUSH Sync functionality has been disabled.',

Check warning on line 390 in src/Product/ProductSyncer.php

View check run for this annotation

Codecov / codecov/patch

src/Product/ProductSyncer.php#L388-L390

Added lines #L388 - L390 were not covered by tests
'google-listings-and-ads'
)
);
Expand Down
15 changes: 15 additions & 0 deletions src/Product/SyncerHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@
continue;
}

// Only proceed with product syncing if PUSH is enabled for this data type
if ( ! $this->merchant_center->is_enabled_for_datatype( NotificationsService::DATATYPE_PRODUCT ) ) {
continue;

Check warning on line 230 in src/Product/SyncerHooks.php

View check run for this annotation

Codecov / codecov/patch

src/Product/SyncerHooks.php#L230

Added line #L230 was not covered by tests
}

// Schedule an update job if product sync is enabled.
if ( $this->product_helper->is_sync_ready( $product ) ) {
$this->product_helper->mark_as_pending( $product );
Expand Down Expand Up @@ -294,6 +299,11 @@
* @param int $product_id
*/
protected function handle_delete_product( int $product_id ) {
// Only proceed with product deletion if PUSH is enabled for this data type
if ( ! $this->merchant_center->is_enabled_for_datatype( NotificationsService::DATATYPE_PRODUCT ) ) {
return;

Check warning on line 304 in src/Product/SyncerHooks.php

View check run for this annotation

Codecov / codecov/patch

src/Product/SyncerHooks.php#L304

Added line #L304 was not covered by tests
}

if ( isset( $this->delete_requests_map[ $product_id ] ) ) {
$product_id_map = BatchProductIDRequestEntry::convert_to_id_map( $this->delete_requests_map[ $product_id ] )->get();
if ( ! empty( $product_id_map ) && ! $this->is_already_scheduled_to_delete( $product_id ) ) {
Expand Down Expand Up @@ -352,6 +362,11 @@
$this->maybe_send_delete_notification( $product_id );
}

// Only proceed with product syncing if PUSH is enabled for this data type
if ( ! $this->merchant_center->is_enabled_for_datatype( NotificationsService::DATATYPE_PRODUCT ) ) {
return;

Check warning on line 367 in src/Product/SyncerHooks.php

View check run for this annotation

Codecov / codecov/patch

src/Product/SyncerHooks.php#L367

Added line #L367 was not covered by tests
}

$product = $this->wc->maybe_get_product( $product_id );

// each variation is passed to this method separately so we don't need to delete the variable product
Expand Down
4 changes: 3 additions & 1 deletion src/Shipping/SyncerHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ protected function handle_update_shipping_settings() {
$this->job_repository->get( ShippingNotificationJob::class )->schedule( [ 'topic' => NotificationsService::TOPIC_SHIPPING_UPDATED ] );
}

$this->job_repository->get( UpdateShippingSettings::class )->schedule();
if ( $this->merchant_center->is_enabled_for_datatype( NotificationsService::DATATYPE_SHIPPING ) ) {
$this->job_repository->get( UpdateShippingSettings::class )->schedule();
}

$this->already_scheduled = true;
}
Expand Down
12 changes: 12 additions & 0 deletions tests/Unit/Coupon/CouponSyncerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ public function test_update_throws_exception_when_mc_is_blocked() {
->willReturn( true );
$merchant_center->expects( $this->once() )
->method( 'should_push' )
->willReturn( true );
$merchant_center->expects( $this->once() )
->method( 'is_enabled_for_datatype' )
->with( 'coupons' )
->willReturn( false );
$this->coupon_syncer = $this->get_coupon_syncer( [ 'merchant_center' => $merchant_center ] );

Expand All @@ -170,6 +174,10 @@ public function test_delete_throws_exception_when_mc_is_blocked() {
->willReturn( true );
$merchant_center->expects( $this->once() )
->method( 'should_push' )
->willReturn( true );
$merchant_center->expects( $this->once() )
->method( 'is_enabled_for_datatype' )
->with( 'coupons' )
->willReturn( false );
$this->coupon_syncer = $this->get_coupon_syncer( [ 'merchant_center' => $merchant_center ] );

Expand Down Expand Up @@ -234,6 +242,10 @@ public function setUp(): void {
$this->merchant_center->expects( $this->any() )
->method( 'is_promotion_supported_country' )
->willReturn( true );
$this->merchant_center->expects( $this->any() )
->method( 'is_enabled_for_datatype' )
->with( 'coupons' )
->willReturn( true );

$this->target_audience = $this->createMock( TargetAudience::class );
$this->target_audience->expects( $this->any() )
Expand Down
Loading