Skip to content

Commit c12e2e1

Browse files
nrostrow-metaNoah Ostrowski
andauthored
Ratings and reviews feed upload cron job (#859)
* Setting up cron job for ratings and reviews feed upload * Casting store->getId as int * Magento -> magento for aggregator * Removing country from feed upload as there is no way in Magento to get the country directly associated with the particular review. --------- Co-authored-by: Noah Ostrowski <[email protected]>
1 parent 9c413e4 commit c12e2e1

File tree

3 files changed

+91
-5
lines changed

3 files changed

+91
-5
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Copyright (c) Meta Platforms, Inc. and affiliates.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
namespace Meta\Catalog\Cron;
22+
23+
use Meta\BusinessExtension\Helper\FBEHelper;
24+
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
25+
use Meta\Catalog\Model\Product\Feed\Method\RatingsAndReviewsFeedApi;
26+
27+
class RatingsAndReviewsFeedUploadCron
28+
{
29+
/**
30+
* @var RatingsAndReviewsFeedApi
31+
*/
32+
private RatingsAndReviewsFeedApi $ratingsAndReviewsFeedApi;
33+
34+
/**
35+
* @var SystemConfig
36+
*/
37+
private SystemConfig $systemConfig;
38+
39+
/**
40+
* @var FBEHelper
41+
*/
42+
private FBEHelper $fbeHelper;
43+
44+
/**
45+
* RatingsAndReviewsFeedUploadCron constructor
46+
*
47+
* @param RatingsAndReviewsFeedApi $ratingsAndReviewsFeedApi
48+
* @param SystemConfig $systemConfig
49+
* @param FBEHelper $fbeHelper
50+
*/
51+
public function __construct(
52+
RatingsAndReviewsFeedApi $ratingsAndReviewsFeedApi,
53+
SystemConfig $systemConfig,
54+
FBEHelper $fbeHelper
55+
) {
56+
$this->ratingsAndReviewsFeedApi = $ratingsAndReviewsFeedApi;
57+
$this->systemConfig = $systemConfig;
58+
$this->fbeHelper = $fbeHelper;
59+
}
60+
61+
/**
62+
* Sync ratings and reviews from Magento platform to Meta through feed upload API
63+
*
64+
* @return void
65+
*/
66+
public function execute()
67+
{
68+
foreach ($this->systemConfig->getAllFBEInstalledStores() as $store) {
69+
$storeId = (int)$store->getId();
70+
try {
71+
if ($this->systemConfig->isCatalogSyncEnabled($storeId)) {
72+
$this->ratingsAndReviewsFeedApi->execute($storeId);
73+
}
74+
} catch (\Throwable $e) {
75+
$this->fbeHelper->logExceptionImmediatelyToMeta(
76+
$e,
77+
[
78+
'store_id' => $storeId,
79+
'event' => 'ratings_and_reviews_sync',
80+
'event_type' => 'cron_job',
81+
'catalog_id' => $this->systemConfig->getCatalogId($storeId),
82+
]
83+
);
84+
}
85+
}
86+
}
87+
}

app/code/Meta/Catalog/Model/Product/Feed/Method/RatingsAndReviewsFeedApi.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public function execute(int $storeId): mixed
120120
$ratingsAndReviewsData = [];
121121

122122
// Fetch store data
123-
$ratingsAndReviewsData['aggregator'] = "Magento";
123+
$ratingsAndReviewsData['aggregator'] = "magento";
124124
$storeData = [
125125
'name' => $store->getName(),
126126
'id' => $this->systemConfig->getCommerceAccountId($storeId),
@@ -132,7 +132,6 @@ public function execute(int $storeId): mixed
132132
$reviewCollection = $this->reviewCollectionFactory->create()
133133
->addStoreFilter($storeId)
134134
->addStatusFilter(Review::STATUS_APPROVED);
135-
$countryCode = $store->getConfig('general/country/default');
136135
$reviews = [];
137136
foreach ($reviewCollection as $review) {
138137
$reviewData = [
@@ -141,7 +140,6 @@ public function execute(int $storeId): mixed
141140
'title' => $review->getTitle(),
142141
'content' => $review->getDetail(),
143142
'createdAt' => $review->getCreatedAt(),
144-
'country' => $countryCode,
145143
'reviewer' => [
146144
'name' => $review->getNickname(),
147145
'reviewerID' => $review->getCustomerId()
@@ -278,7 +276,6 @@ private function generateCsvString($ratingsAndReviewsData): string
278276
"title",
279277
"content",
280278
"created_at",
281-
"country",
282279
"reviewer.name",
283280
"reviewer.reviewerID",
284281
"product.name",
@@ -298,7 +295,6 @@ private function generateCsvString($ratingsAndReviewsData): string
298295
$review['title'],
299296
$review['content'],
300297
$review['createdAt'],
301-
$review['country'],
302298
$review['reviewer']['name'],
303299
$review['reviewer']['reviewerID'],
304300
$review['product']['name'],

app/code/Meta/Catalog/etc/crontab.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,8 @@
1919
<job name="facebook_automation_log_catalog_setup" instance="Meta\Catalog\Cron\LogCatalogSetup" method="execute">
2020
<schedule>0 4 * * 0</schedule>
2121
</job>
22+
<job name="facebook_automation_ratings_and_reviews_feed_upload_cron" instance="Meta\Catalog\Cron\RatingsAndReviewsFeedUploadCron" method="execute">
23+
<schedule>0 4 * * 6</schedule>
24+
</job>
2225
</group>
2326
</config>

0 commit comments

Comments
 (0)