Skip to content

Commit bd6d74e

Browse files
committed
fix: fallback to variation post parent id in woocommerce cache invalidation
1 parent 0997cac commit bd6d74e

2 files changed

Lines changed: 53 additions & 2 deletions

File tree

src/Subscriber/Compatibility/WooCommerceSubscriber.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,21 @@ public function clearCacheOnProductUpdate($productId)
111111
*/
112112
public function clearCacheOnProductVariationUpdate($variationId, $variation)
113113
{
114-
if (!is_object($variation) || !method_exists($variation, 'get_parent_id')) {
114+
$parentId = 0;
115+
116+
if (is_object($variation) && method_exists($variation, 'get_parent_id')) {
117+
$parentId = (int) $variation->get_parent_id();
118+
}
119+
120+
if ($parentId <= 0) {
121+
$parentId = (int) wp_get_post_parent_id((int) $variationId);
122+
}
123+
124+
if ($parentId <= 0) {
115125
return;
116126
}
117127

118-
$this->clearProductUrls($variation->get_parent_id());
128+
$this->clearProductUrls($parentId);
119129
}
120130

121131
/**

tests/Unit/Subscriber/Compatibility/WooCommerceSubscriberTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,47 @@ public function testClearCacheOnProductVariationUpdate()
166166
$this->createSubscriber('https://foo.com', '', false, ['invalidation_enabled' => true], $pageCacheClient)->clearCacheOnProductVariationUpdate(456, $variation);
167167
}
168168

169+
public function testClearCacheOnProductVariationUpdateFallsBackToVariationPostParentId()
170+
{
171+
$function_exists = $this->getFunctionMock($this->getNamespace(WooCommerceSubscriber::class), 'function_exists');
172+
$function_exists->expects($this->once())
173+
->with('wc_get_page_permalink')
174+
->willReturn(true);
175+
176+
$wp_get_post_parent_id = $this->getFunctionMock($this->getNamespace(WooCommerceSubscriber::class), 'wp_get_post_parent_id');
177+
$wp_get_post_parent_id->expects($this->once())
178+
->with(456)
179+
->willReturn(123);
180+
181+
$get_permalink = $this->getFunctionMock($this->getNamespace(WooCommerceSubscriber::class), 'get_permalink');
182+
$get_permalink->expects($this->once())
183+
->with(123)
184+
->willReturn('https://foo.com/product/bar/');
185+
186+
$wc_get_page_permalink = $this->getFunctionMock($this->getNamespace(WooCommerceSubscriber::class), 'wc_get_page_permalink');
187+
$wc_get_page_permalink->expects($this->once())
188+
->with('shop')
189+
->willReturn('https://foo.com/shop/');
190+
191+
$get_the_terms = $this->getFunctionMock($this->getNamespace(WooCommerceSubscriber::class), 'get_the_terms');
192+
$get_the_terms->expects($this->exactly(2))
193+
->willReturn([]);
194+
195+
$pageCacheClient = $this->getContentDeliveryNetworkPageCacheClientInterfaceMock();
196+
$pageCacheClient->expects($this->once())
197+
->method('clearUrls')
198+
->with($this->callback(function ($urls) {
199+
$this->assertSame([
200+
'https://foo.com/product/bar/',
201+
'https://foo.com/shop/*',
202+
], $urls->all());
203+
204+
return true;
205+
}));
206+
207+
$this->createSubscriber('https://foo.com', '', false, ['invalidation_enabled' => true], $pageCacheClient)->clearCacheOnProductVariationUpdate(456, new \stdClass());
208+
}
209+
169210
public function testDisableCheckImportFilePath()
170211
{
171212
$this->assertFalse($this->createSubscriber()->disableCheckImportFilePath());

0 commit comments

Comments
 (0)