Skip to content

Commit 4a1f1f8

Browse files
authored
Merge pull request #222 from woocommerce/fix/incorrect-issues-multiple-feeds
Use stored feed profile ID to retrieve catalog status and issues
2 parents 672fa37 + cee33f5 commit 4a1f1f8

4 files changed

Lines changed: 27 additions & 30 deletions

File tree

src/API/Base.php

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -455,32 +455,23 @@ public static function update_merchant_feed( $merchant_id, $feed_id, $args ) {
455455
* Get a specific merchant's feed using the given arguments.
456456
*
457457
* @param string $merchant_id The merchant ID the feed belongs to.
458-
* @param string $feed_id The ID of the feed to be updated.
458+
* @param string $feed_id The ID of the feed.
459459
*
460460
* @return mixed
461461
*/
462462
public static function get_merchant_feed( $merchant_id, $feed_id ) {
463-
$args = array( 'feed_profile' => $feed_id );
464-
465463
return self::make_request(
466-
add_query_arg( $args, 'catalogs/datasource/feed_report/' . $merchant_id . '/' ),
467-
'GET'
464+
"catalogs/datasource/feed_report/{$merchant_id}/",
465+
'GET',
466+
array(
467+
'feed_profile' => $feed_id,
468+
),
469+
'',
470+
MINUTE_IN_SECONDS
468471
);
469472
}
470473

471474

472-
/**
473-
* Request the feed report data from the API and return the response.
474-
*
475-
* @param string $merchant_id The ID of the merchant for the request.
476-
*
477-
* @return mixed
478-
*/
479-
public static function get_feed_report( $merchant_id ) {
480-
return self::make_request( 'catalogs/datasource/feed_report/' . $merchant_id . '/', 'GET', array(), '', MINUTE_IN_SECONDS );
481-
}
482-
483-
484475
/**
485476
* Request the managed map representing all of the error, recommendation, and status messages for catalogs.
486477
*

src/API/FeedIssues.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ public function __construct() {
5656
public function get_feed_issues( WP_REST_Request $request ) {
5757

5858
try {
59-
60-
if ( ! Pinterest\ProductSync::is_product_sync_enabled() || ! Pinterest\ProductSync::is_feed_registered() ) {
59+
$feed_id = Pinterest\ProductSync::get_registered_feed_id();
60+
if ( ! Pinterest\ProductSync::is_product_sync_enabled() || ! $feed_id ) {
6161
return array( 'lines' => array() );
6262
}
6363

@@ -67,7 +67,7 @@ public function get_feed_issues( WP_REST_Request $request ) {
6767
$per_page = $request->has_param( 'per_page' ) ? (int) $request->get_param( 'per_page' ) : 25;
6868

6969
if ( false === $issues_file_url ) {
70-
$workflow = self::get_last_feed_workflow();
70+
$workflow = self::get_feed_workflow( $feed_id );
7171

7272
if ( $workflow && isset( $workflow->s3_validation_url ) ) {
7373
$issues_file_url = $workflow->s3_validation_url;
@@ -271,14 +271,15 @@ private function save_feed_data_cache() {
271271
* Get the latest Workflow of the
272272
* active feed related to the last attempt to process and ingest our feed, for the Merchant saved in the settings.
273273
*
274+
* @param string $feed_id The ID of the feed.
275+
*
274276
* @return object
275277
*
276278
* @throws \Exception PHP Exception.
277279
*/
278-
private static function get_last_feed_workflow() {
279-
280+
private static function get_feed_workflow( $feed_id ) {
280281
$merchant_id = Pinterest_For_Woocommerce()::get_data( 'merchant_id' );
281-
$feed_report = $merchant_id ? Base::get_feed_report( $merchant_id ) : false;
282+
$feed_report = $merchant_id ? Base::get_merchant_feed( $merchant_id, $feed_id ) : false;
282283

283284
if ( ! $feed_report || 'success' !== $feed_report['status'] ) {
284285
throw new \Exception( esc_html__( 'Could not get feed report from Pinterest.', 'pinterest-for-woocommerce' ), 400 );

src/API/FeedState.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,13 +331,18 @@ private function add_feed_registration_state( $result ) {
331331
*/
332332
private function add_feed_sync_status( $result ) {
333333

334+
$feed_id = Pinterest\ProductSync::get_registered_feed_id();
335+
if ( ! $feed_id ) {
336+
throw new \Exception( esc_html__( 'Feed is not registered with Pinterest.', 'pinterest-for-woocommerce' ) );
337+
}
338+
334339
$merchant_id = Pinterest_For_Woocommerce()::get_data( 'merchant_id' );
335340
$extra_info = '';
336341

337342
try {
338343

339344
// Get feed ingestion status.
340-
$feed_report = $merchant_id ? Base::get_feed_report( $merchant_id ) : false;
345+
$feed_report = $merchant_id ? Base::get_merchant_feed( $merchant_id, $feed_id ) : false;
341346

342347
if ( ! $feed_report || 'success' !== $feed_report['status'] ) {
343348
throw new \Exception( esc_html__( 'Response error when trying to get feed report from Pinterest.', 'pinterest-for-woocommerce' ) );

src/ProductSync.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class ProductSync {
6262
*/
6363
public static function maybe_init() {
6464

65-
if ( ! self::is_product_sync_enabled() && ! self::is_feed_registered() ) {
65+
if ( ! self::is_product_sync_enabled() && ! self::get_registered_feed_id() ) {
6666
return;
6767
}
6868

@@ -198,11 +198,11 @@ public static function is_product_sync_enabled() {
198198

199199

200200
/**
201-
* Checks if the feature is enabled, and all requirements are met.
201+
* Returns the feed profile ID if it's registered. Returns `false` otherwise.
202202
*
203-
* @return boolean
203+
* @return string|boolean
204204
*/
205-
public static function is_feed_registered() {
205+
public static function get_registered_feed_id() {
206206
return Pinterest_For_Woocommerce()::get_data( 'feed_registered' ) ?? false;
207207
}
208208

@@ -239,7 +239,7 @@ public static function handle_feed_registration() {
239239
'locale' => str_replace( '_', '-', determine_locale() ),
240240
);
241241

242-
$registered = self::is_feed_registered();
242+
$registered = self::get_registered_feed_id();
243243

244244
if ( ! self::is_product_sync_enabled() ) {
245245
// Handle feed deregistration.
@@ -520,7 +520,7 @@ private static function register_feed( $feed_args ) {
520520
} else {
521521
$product_pin_feed_profile = $merchant['data']->product_pin_feed_profile;
522522
$product_pin_feed_profile_id = false;
523-
$prev_registered = self::is_feed_registered();
523+
$prev_registered = self::get_registered_feed_id();
524524
if ( false !== $prev_registered ) {
525525
try {
526526
$feed = API\Base::get_merchant_feed( $merchant['data']->id, $prev_registered );

0 commit comments

Comments
 (0)