Skip to content

Commit

Permalink
Disable create and update for variations
Browse files Browse the repository at this point in the history
  • Loading branch information
puntope committed Jul 9, 2024
1 parent 422f3f6 commit 0159630
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 14 deletions.
8 changes: 6 additions & 2 deletions src/Product/ProductHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ public function is_ready_to_notify( WC_Product $product ): bool {
* @return bool
*/
public function should_trigger_create_notification( $product ): bool {
return $this->is_ready_to_notify( $product ) && ! $this->has_notified_creation( $product );
return ! $product instanceof WC_Product_Variation && $this->is_ready_to_notify( $product ) && ! $this->has_notified_creation( $product );
}

/**
Expand All @@ -434,7 +434,7 @@ public function should_trigger_create_notification( $product ): bool {
* @return bool
*/
public function should_trigger_update_notification( $product ): bool {
return $this->is_ready_to_notify( $product ) && $this->has_notified_creation( $product );
return ! $product instanceof WC_Product_Variation && $this->is_ready_to_notify( $product ) && $this->has_notified_creation( $product );
}

/**
Expand All @@ -458,6 +458,10 @@ public function should_trigger_delete_notification( $product ): bool {
* @return bool
*/
public function has_notified_creation( WC_Product $product ): bool {
if ( $product instanceof WC_Product_Variation ) {
return $this->has_notified_creation( $this->maybe_swap_for_parent( $product ) );
}

$valid_has_notified_creation_statuses = [
NotificationStatus::NOTIFICATION_CREATED,
NotificationStatus::NOTIFICATION_UPDATED,
Expand Down
13 changes: 6 additions & 7 deletions src/Product/SyncerHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,13 +281,6 @@ protected function handle_update_product_notification( WC_Product $product ) {
'topic' => NotificationsService::TOPIC_PRODUCT_CREATED,
]
);

// Variations are not handled when the parent is created.
if ( $product instanceof WC_Product_Variable ) {
foreach ( $product->get_available_variations( 'objects' ) as $variation ) {
$this->handle_update_product_notification( $variation );
}
}
} elseif ( $this->product_helper->should_trigger_update_notification( $product ) ) {
$this->product_helper->set_notification_status( $product, NotificationStatus::NOTIFICATION_PENDING_UPDATE );
$this->product_notification_job->schedule(
Expand All @@ -298,6 +291,12 @@ protected function handle_update_product_notification( WC_Product $product ) {
);
} elseif ( $this->product_helper->should_trigger_delete_notification( $product ) ) {
$this->schedule_delete_notification( $product );
// Schedule variation deletion when the parent is deleted.
if ( $product instanceof WC_Product_Variable ) {
foreach ( $product->get_available_variations( 'objects' ) as $variation ) {
$this->handle_update_product_notification( $variation );
}
}
}
}

Expand Down
89 changes: 88 additions & 1 deletion tests/Unit/Product/ProductHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1156,7 +1156,17 @@ public function test_should_trigger_create_notification() {
$this->assertFalse( $this->product_helper->should_trigger_create_notification( $product ) );
}

public function test_should_trigger_update_notification() {
public function test_should_trigger_create_notification_if_variation() {
/**
* @var \WC_Product_Variable $product
*/
$product = WC_Helper_Product::create_variation_product();
$variation = $product->get_available_variations( 'objects' );
$this->assertTrue( $this->product_helper->should_trigger_create_notification( $product ) );
$this->assertFalse( $this->product_helper->should_trigger_create_notification( $variation[0] ) );
}

public function test_should_not_trigger_update_notification() {
/**
* @var WC_Product $product
*/
Expand All @@ -1173,6 +1183,17 @@ public function test_should_trigger_update_notification() {
$this->assertFalse( $this->product_helper->should_trigger_update_notification( $product ) );
}

public function test_should_not_trigger_update_notification_if_variation() {
/**
* @var \WC_Product_Variable $product
*/
$product = WC_Helper_Product::create_variation_product();
$this->product_helper->set_notification_status( $product, NotificationStatus::NOTIFICATION_CREATED );
$variation = $product->get_available_variations( 'objects' );
$this->assertTrue( $this->product_helper->should_trigger_update_notification( $product ) );
$this->assertFalse( $this->product_helper->should_trigger_update_notification( $variation[0] ) );
}

public function test_should_trigger_delete_notification() {
/**
* @var WC_Product $product
Expand All @@ -1189,6 +1210,72 @@ public function test_should_trigger_delete_notification() {
$this->assertFalse( $this->product_helper->should_trigger_delete_notification( $product ) );
}

public function test_should_trigger_delete_notification_if_variation() {
/**
* @var \WC_Product_Variable $product
*/
$product = WC_Helper_Product::create_variation_product();
$this->product_helper->set_notification_status( $product, NotificationStatus::NOTIFICATION_CREATED );
$variation = $product->get_available_variations( 'objects' );

$product->set_status( 'draft' );
$product->save();
$this->assertTrue( $this->product_helper->should_trigger_delete_notification( $product ) );
$this->assertTrue( $this->product_helper->should_trigger_delete_notification( $variation[0] ) );
}

public function test_has_notified_creation() {
/**
* @var WC_Product $product
*/
$product = $this->get_notification_ready_product( WC_Helper_Product::create_simple_product() );
$this->assertFalse( $this->product_helper->has_notified_creation( $product ) );
$this->product_helper->set_notification_status( $product, NotificationStatus::NOTIFICATION_CREATED );
$this->assertTrue( $this->product_helper->has_notified_creation( $product ) );
$this->product_helper->set_notification_status( $product, NotificationStatus::NOTIFICATION_UPDATED );
$this->assertTrue( $this->product_helper->has_notified_creation( $product ) );
$this->product_helper->set_notification_status( $product, NotificationStatus::NOTIFICATION_PENDING_UPDATE );
$this->assertTrue( $this->product_helper->has_notified_creation( $product ) );
$this->product_helper->set_notification_status( $product, NotificationStatus::NOTIFICATION_PENDING_DELETE );
$this->assertTrue( $this->product_helper->has_notified_creation( $product ) );
$this->product_helper->set_notification_status( $product, NotificationStatus::NOTIFICATION_DELETED );
$this->assertFalse( $this->product_helper->has_notified_creation( $product ) );

$google_product = $this->generate_google_product_mock();
$this->product_helper->mark_as_synced( $product, $google_product );
$this->assertTrue( $this->product_helper->has_notified_creation( $product ) );
}

public function test_has_notified_creation_variations() {
/**
* @var \WC_Product_Variable $product
*/
$product = WC_Helper_Product::create_variation_product();
$variations = $product->get_available_variations( 'objects' );

$this->assertFalse( $this->product_helper->has_notified_creation( $product ) );
$this->assertFalse( $this->product_helper->has_notified_creation( $variations[0] ) );
$this->product_helper->set_notification_status( $product, NotificationStatus::NOTIFICATION_CREATED );
$this->assertTrue( $this->product_helper->has_notified_creation( $product ) );
$this->assertTrue( $this->product_helper->has_notified_creation( $variations[0] ) );
$this->product_helper->set_notification_status( $product, NotificationStatus::NOTIFICATION_UPDATED );
$this->assertTrue( $this->product_helper->has_notified_creation( $product ) );
$this->assertTrue( $this->product_helper->has_notified_creation( $variations[0] ) );
$this->product_helper->set_notification_status( $product, NotificationStatus::NOTIFICATION_PENDING_UPDATE );
$this->assertTrue( $this->product_helper->has_notified_creation( $product ) );
$this->assertTrue( $this->product_helper->has_notified_creation( $variations[0] ) );
$this->product_helper->set_notification_status( $product, NotificationStatus::NOTIFICATION_PENDING_DELETE );
$this->assertTrue( $this->product_helper->has_notified_creation( $product ) );
$this->assertTrue( $this->product_helper->has_notified_creation( $variations[0] ) );
$this->product_helper->set_notification_status( $product, NotificationStatus::NOTIFICATION_DELETED );
$this->assertFalse( $this->product_helper->has_notified_creation( $product ) );
$this->assertFalse( $this->product_helper->has_notified_creation( $variations[0] ) );

$google_product = $this->generate_google_product_mock();
$this->product_helper->mark_as_synced( $product, $google_product );
$this->assertTrue( $this->product_helper->has_notified_creation( $product ) );
$this->assertTrue( $this->product_helper->has_notified_creation( $variations[0] ) );
}

public function test_get_offer_id() {
$this->assertEquals( $this->product_helper->get_offer_id( 1 ), 'gla_1' );
Expand Down
29 changes: 25 additions & 4 deletions tests/Unit/Product/SyncerHooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,24 +428,45 @@ public function test_unshow_unsynced_created_product_triggers_notification_delet
$product->save();
}

public function test_create_variable_product_triggers_notifications_for_variable_and_variations() {
public function test_create_variable_product_triggers_notifications_for_variable_only() {
$this->set_mc_and_notifications( true, true );
$variable_product = $this->create_variation_product( null, [ 'status' => 'draft' ] );
$this->product_notification_job->expects( $this->exactly( 1 ) )
->method( 'schedule' )
->with(
$this->equalTo(
[
'item_id' => $variable_product->get_id(),
'topic' => NotificationsService::TOPIC_PRODUCT_CREATED,
]
)
);

$variable_product->set_status( 'publish' );
$variable_product->save();
}

public function test_unshow_variable_product_schedules_delete_notifications_for_parent_and_variations() {
$this->set_mc_and_notifications( true, true );
$variable_product = $this->create_variation_product( null, [ 'status' => 'draft' ] );
$ids = array_merge( [ $variable_product->get_id() ], $variable_product->get_children() );
$matcher = $this->exactly( count( $ids ) );
$this->product_helper->set_notification_status( $variable_product, NotificationStatus::NOTIFICATION_CREATED );

$this->product_notification_job->expects( $matcher )
->method( 'schedule' )
->with(
$this->callback(
function ( $args ) use ( $ids, $matcher ) {
$this->assertEquals( $args['item_id'], $ids[ $matcher->getInvocationCount() - 1 ] );
$this->assertEquals( $args['topic'], NotificationsService::TOPIC_PRODUCT_CREATED );
return true;
$this->assertEquals( $args['item_id'], $ids[ $matcher->getInvocationCount() - 1 ] );
$this->assertEquals( $args['topic'], NotificationsService::TOPIC_PRODUCT_DELETED );
return true;
}
)
);

$variable_product->set_status( 'publish' );
$variable_product->add_meta_data( '_wc_gla_visibility', ChannelVisibility::DONT_SYNC_AND_SHOW, true );
$variable_product->save();
}

Expand Down

0 comments on commit 0159630

Please sign in to comment.