Skip to content

Commit

Permalink
Add hooks for create and delete with ID
Browse files Browse the repository at this point in the history
  • Loading branch information
puntope committed Feb 12, 2024
1 parent 34e1329 commit c7dc1af
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/Google/NotificationsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ class NotificationsService implements Service {
public const TOPIC_COUPON_CREATED = 'coupon.create';
public const TOPIC_COUPON_DELETED = 'coupon.delete';
public const TOPIC_COUPON_UPDATED = 'coupon.update';
public const TOPIC_SHIPPING_CREATED = 'shipping.create';
public const TOPIC_SHIPPING_UPDATED = 'shipping.update';
public const TOPIC_SHIPPING_DELETED = 'shipping.delete';

/**
* The url to send the notification
Expand Down
8 changes: 7 additions & 1 deletion src/Jobs/Notifications/ShippingNotificationJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,13 @@ public function get_name(): string {
* @param array $args Arguments with the item id and the topic
*/
protected function process_items( array $args ): void {
$this->notifications_service->notify( $this->topic );
if ( ! isset( $args[1] ) ) {
return;
}

$item = $args[0];
$topic = $args[1];
$this->notifications_service->notify( $topic, $item );
}

/**
Expand Down
42 changes: 35 additions & 7 deletions src/Shipping/SyncerHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Automattic\WooCommerce\GoogleListingsAndAds\Jobs\Notifications\ShippingNotificationJob;
use Automattic\WooCommerce\GoogleListingsAndAds\Jobs\UpdateShippingSettings;
use Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter\MerchantCenterService;
use WC_Data;

defined( 'ABSPATH' ) || exit;

Expand All @@ -32,6 +33,13 @@ class SyncerHooks implements Service, Registerable {
*/
protected $already_scheduled = false;

/**
* This property is used to flag shipping status after save for being used in the notifications.
*
* @var bool
*/
protected $has_created_shipping = false;

/**
* @var GoogleSettings
*/
Expand Down Expand Up @@ -86,18 +94,37 @@ public function register(): void {
$this->handle_update_shipping_settings();
};

$flag_zone = function ( WC_Data $instance ) {
$this->has_created_shipping = ! (bool) $instance->get_id();
};

$delete_zone = function ( int $id ) {
$this->handle_update_shipping_settings( NotificationsService::TOPIC_SHIPPING_DELETED, $id );
};

$zone_saved = function ( WC_Data $instance ) {

Check failure on line 105 in src/Shipping/SyncerHooks.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 spaces before closing parenthesis; 2 found
$this->handle_update_shipping_settings( $this->has_created_shipping ? NotificationsService::TOPIC_SHIPPING_CREATED : NotificationsService::TOPIC_SHIPPING_UPDATED, $instance->get_id() );
};

$update_method = function ( int $id, $method, $zone_id ) {

Check failure on line 109 in src/Shipping/SyncerHooks.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 spaces before closing parenthesis; 2 found
$this->handle_update_shipping_settings( NotificationsService::TOPIC_SHIPPING_UPDATED, $zone_id );
};

// After a shipping zone object is saved to database.
add_action( 'woocommerce_after_shipping_zone_object_save', $update_settings, 90 );
add_action( 'woocommerce_after_shipping_zone_object_save', $zone_saved, 90, 1 );

// Right before a shipping zone object is saved to database.
add_action( 'woocommerce_before_shipping_zone_object_save', $flag_zone, 90, 1 );

// After a shipping zone is deleted.
add_action( 'woocommerce_delete_shipping_zone', $update_settings, 90 );
add_action( 'woocommerce_delete_shipping_zone', $delete_zone, 90, 1 );

// After a shipping method is added to or deleted from a shipping zone.
add_action( 'woocommerce_shipping_zone_method_added', $update_settings, 90 );
add_action( 'woocommerce_shipping_zone_method_deleted', $update_settings, 90 );
add_action( 'woocommerce_shipping_zone_method_added', $update_method, 90, 3 );
add_action( 'woocommerce_shipping_zone_method_deleted', $update_method, 90, 3 );

// After a shipping method is enabled or disabled.
add_action( 'woocommerce_shipping_zone_method_status_toggled', $update_settings, 90 );
add_action( 'woocommerce_shipping_zone_method_status_toggled', $update_method, 90, 3 );

// After a shipping class is updated/deleted.
add_action( 'woocommerce_shipping_classes_save_class', $update_settings, 90 );
Expand Down Expand Up @@ -138,18 +165,19 @@ public function register(): void {
*
* @return void
*/
protected function handle_update_shipping_settings() {
protected function handle_update_shipping_settings( $topic = NotificationsService::TOPIC_SHIPPING_UPDATED, $item_id = null) {

Check failure on line 168 in src/Shipping/SyncerHooks.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 spaces before closing parenthesis; 0 found
// Bail if an event is already scheduled in the current request
if ( $this->already_scheduled ) {
return;
}

if ( $this->notifications_service->is_enabled() ) {
$this->shipping_notification_job->schedule();
$this->shipping_notification_job->schedule( [ $item_id, $topic ] );
} else {
$this->update_shipping_job->schedule();
}

$this->has_created_shipping = false;
$this->already_scheduled = true;

Check warning on line 181 in src/Shipping/SyncerHooks.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space
}
}

0 comments on commit c7dc1af

Please sign in to comment.