18
18
namespace Meta \Catalog \Observer \Product ;
19
19
20
20
use Exception ;
21
+ use GuzzleHttp \Exception \GuzzleException ;
22
+ use Magento \Catalog \Api \ProductRepositoryInterface ;
23
+ use Magento \Eav \Model \Entity \Attribute \Source \Boolean ;
24
+ use Magento \Framework \Exception \NoSuchEntityException ;
21
25
use Meta \BusinessExtension \Helper \FBEHelper ;
22
26
use Meta \BusinessExtension \Helper \GraphAPIAdapter ;
23
27
use Meta \BusinessExtension \Model \System \Config as SystemConfig ;
24
28
use Meta \Catalog \Model \Product \Feed \Method \BatchApi ;
25
- use Magento \Catalog \Model \Product ;
26
29
use Magento \Framework \Event \Observer ;
27
30
use Magento \Framework \Event \ObserverInterface ;
31
+ use Magento \Framework \Message \ManagerInterface ;
28
32
29
33
class SaveAfter implements ObserverInterface
30
34
{
@@ -36,34 +40,50 @@ class SaveAfter implements ObserverInterface
36
40
/**
37
41
* @var FBEHelper
38
42
*/
39
- protected $ fbeHelper ;
43
+ private $ fbeHelper ;
40
44
41
45
/**
42
46
* @var BatchApi
43
47
*/
44
- protected $ batchApi ;
48
+ private $ batchApi ;
45
49
46
50
/**
47
51
* @var GraphAPIAdapter
48
52
*/
49
- protected $ graphApiAdapter ;
53
+ private $ graphApiAdapter ;
54
+
55
+ /**
56
+ * @var ManagerInterface
57
+ */
58
+ private $ messageManager ;
59
+
60
+ /**
61
+ * @var ProductRepositoryInterface
62
+ */
63
+ private $ productRepo ;
50
64
51
65
/**
52
66
* @param SystemConfig $systemConfig
53
67
* @param FBEHelper $helper
54
68
* @param BatchApi $batchApi
55
69
* @param GraphAPIAdapter $graphApiAdapter
70
+ * @param ManagerInterface $messageManager
71
+ * @param ProductRepositoryInterface $productRepo
56
72
*/
57
73
public function __construct (
58
74
SystemConfig $ systemConfig ,
59
75
FBEHelper $ helper ,
60
76
BatchApi $ batchApi ,
61
- GraphAPIAdapter $ graphApiAdapter
77
+ GraphAPIAdapter $ graphApiAdapter ,
78
+ ManagerInterface $ messageManager ,
79
+ ProductRepositoryInterface $ productRepo
62
80
) {
63
81
$ this ->systemConfig = $ systemConfig ;
64
82
$ this ->fbeHelper = $ helper ;
65
83
$ this ->batchApi = $ batchApi ;
66
84
$ this ->graphApiAdapter = $ graphApiAdapter ;
85
+ $ this ->messageManager = $ messageManager ;
86
+ $ this ->productRepo = $ productRepo ;
67
87
}
68
88
69
89
/**
@@ -72,41 +92,69 @@ public function __construct(
72
92
* Call an API to product save from facebook catalog
73
93
* after save product from Magento
74
94
*
75
- * @todo Take into consideration current store scope
76
95
* @param Observer $observer
77
96
*/
78
97
public function execute (Observer $ observer )
79
98
{
80
- if (!($ this ->systemConfig ->isActiveExtension () && $ this ->systemConfig ->isActiveIncrementalProductUpdates ())) {
99
+ $ product = $ observer ->getEvent ()->getProduct ();
100
+ $ pid = $ product ->getId ();
101
+
102
+ if (!$ pid ) {
81
103
return ;
82
104
}
83
105
84
- /** @var Product $product */
85
- $ product = $ observer -> getEvent ()-> getProduct ();
86
- if (! $ product -> getId () ) {
87
- return ;
106
+ $ stores = $ this -> systemConfig -> getStoreManager ()-> getStores ( false , true );
107
+
108
+ foreach ( $ stores as $ store ) {
109
+ $ this -> updateProduct ( $ store -> getId (), $ pid ) ;
88
110
}
111
+ }
112
+
113
+ /**
114
+ * Process product update
115
+ *
116
+ * @param int $storeId
117
+ * @param int $productId
118
+ * @return void
119
+ */
120
+ private function updateProduct ($ storeId , $ productId ): void
121
+ {
122
+ $ isActive = $ this ->systemConfig ->isActiveExtension ($ storeId );
123
+ $ shouldIncrement = $ this ->systemConfig ->isActiveIncrementalProductUpdates ($ storeId );
89
124
90
- if ($ product -> getSendToFacebook () === \ Magento \ Eav \ Model \ Entity \ Attribute \ Source \Boolean:: VALUE_NO ) {
125
+ if (!( $ isActive && $ shouldIncrement ) ) {
91
126
return ;
92
127
}
93
128
94
- $ productStoreId = $ product ->getStoreId ();
95
- $ storeId = $ this ->fbeHelper ->getStore ()->getId ();
96
- $ product ->setStoreId ($ storeId );
129
+ try {
130
+ $ product = $ this ->productRepo ->getById ($ productId , false , $ storeId , true );
97
131
98
- // @todo implement error handling/logging for invalid access token and other non-happy path scenarios
99
- // @todo implement batch API status check
100
- // @todo implement async call
132
+ if (!$ product ->getSendToFacebook ()) {
133
+ return ;
134
+ }
135
+
136
+ // @todo implement error handling/logging for invalid access token and other non-happy path scenarios
137
+ // @todo implement batch API status check
138
+ // @todo implement async call
101
139
102
- try {
103
140
$ catalogId = $ this ->systemConfig ->getCatalogId ($ storeId );
104
141
$ requestData = $ this ->batchApi ->buildRequestForIndividualProduct ($ product );
105
142
$ this ->graphApiAdapter ->catalogBatchRequest ($ catalogId , [$ requestData ]);
143
+ } catch (NoSuchEntityException $ e ) {
144
+ $ this ->messageManager ->addErrorMessage (
145
+ 'Failed to update Meta for one or more stores. Please see Exception log for more detail. '
146
+ );
147
+ $ this ->fbeHelper ->logException ($ e );
148
+ return ;
149
+ } catch (GuzzleException $ e ) {
150
+ $ this ->messageManager ->addNoticeMessage (
151
+ 'Error sending Increment Update To Meta. Please check your Store Connection Settings. '
152
+ );
153
+ $ this ->fbeHelper ->logException ($ e );
154
+ return ;
106
155
} catch (Exception $ e ) {
107
156
$ this ->fbeHelper ->logException ($ e );
157
+ return ;
108
158
}
109
-
110
- $ product ->setStoreId ($ productStoreId );
111
159
}
112
160
}
0 commit comments