-
Notifications
You must be signed in to change notification settings - Fork 55
Adds product/save to message queue #144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 1 commit
5aba402
ff657f6
b60705e
a140aa6
43c53ee
905c644
682cc17
fcfe92b
b9c26fc
67d5e2d
d2b1ea9
dae44cb
2699a28
cd37f4d
440ebc8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| <?php | ||
|
|
||
| namespace Klaviyo\Reclaim\Cron; | ||
|
|
||
| use Klaviyo\Reclaim\Helper\Logger; | ||
| use Klaviyo\Reclaim\Model\SyncsFactory; | ||
| use Klaviyo\Reclaim\Model\ResourceModel\Products; | ||
| use Klaviyo\Reclaim\Model\ResourceModel\Products\CollectionFactory; | ||
|
|
||
| class ProductsTopic | ||
| { | ||
| /** | ||
| * Klaviyo Logger | ||
| * @var Logger | ||
| */ | ||
| protected $_klaviyoLogger; | ||
|
|
||
| /** | ||
| * Klaviyo Products Resource Model | ||
| * @var Products | ||
| */ | ||
| protected $_klProduct; | ||
|
|
||
| /** | ||
| * Klaviyo Products Collection | ||
| * @var CollectionFactory | ||
| */ | ||
| protected $_klProductCollectionFactory; | ||
|
|
||
| /** | ||
| * Klaviyo Syncs Model | ||
| * @var SyncsFactory | ||
| */ | ||
| protected $_klSyncFactory; | ||
|
|
||
| /** | ||
| * @param Logger $klaviyoLogger | ||
| * @param Products $klProduct | ||
| * @param SyncsFactory $klSyncFactory | ||
| * @param CollectionFactory $klProductCollectionFactory | ||
| */ | ||
| public function __construct( | ||
| Logger $klaviyoLogger, | ||
| Products $klProduct, | ||
| SyncsFactory $klSyncFactory, | ||
| CollectionFactory $klProductCollectionFactory | ||
| ) | ||
| { | ||
| $this->_klaviyoLogger = $klaviyoLogger; | ||
| $this->_klProduct = $klProduct; | ||
| $this->_klSyncFactory = $klSyncFactory; | ||
| $this->_klProductCollectionFactory = $klProductCollectionFactory; | ||
| } | ||
|
|
||
| public function queueKlProductsForSync() | ||
| { | ||
| $klProductsCollection = $this->_klProductCollectionFactory->create(); | ||
| $klProductsToSync = $klProductsCollection->getRowsForSync('NEW') | ||
| ->addFieldToSelect(['id','payload','status','topic', 'klaviyo_id']) | ||
| ->getData(); | ||
|
|
||
| if (empty($klProductsToSync)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| $idsToUpdate = []; | ||
|
|
||
| foreach ($klProductsToSync as $klProductToSync) | ||
| { | ||
| $klSync = $this->_klSyncFactory->create(); | ||
| $klSync->setData([ | ||
| 'payload'=> $klProductToSync['payload'], | ||
| 'topic'=> $klProductToSync['topic'], | ||
| 'klaviyo_id'=>$klProductToSync['klaviyo_id'], | ||
| 'status'=> 'NEW' | ||
| ]); | ||
| try { | ||
| $klSync->save(); | ||
| array_push($idsToUpdate, $klProductToSync['id']); | ||
| } catch (\Exception $e) { | ||
| $this->_klaviyoLogger->log(sprintf('Unable to move row: %s', $e)); | ||
| } | ||
| } | ||
|
|
||
| $klProductsCollection->updateRowStatus($idsToUpdate, 'MOVED'); | ||
| } | ||
|
|
||
| public function clean() | ||
| { | ||
| $klProductsCollection = $this->_klProductCollectionFactory->create(); | ||
| $idsToDelete = $klProductsCollection->getIdsToDelete('MOVED'); | ||
|
|
||
| $klProductsCollection->deleteRows($idsToDelete); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -33,14 +33,13 @@ public function __construct( | |||||||
|
|
||||||||
| /** | ||||||||
| * @param string $webhookType | ||||||||
| * @param array $data | ||||||||
| * @param string $data | ||||||||
siddwarkhedkar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||
| * @param string $klaviyoId | ||||||||
| * @return string | ||||||||
| * @throws Exception | ||||||||
| */ | ||||||||
| public function makeWebhookRequest($webhookType, $data, $klaviyoId=null) | ||||||||
| { | ||||||||
|
|
||||||||
| if (!$klaviyoId) { | ||||||||
| $klaviyoId = $this->_klaviyoScopeSetting->getPublicApiKey(); | ||||||||
| } | ||||||||
|
|
@@ -51,12 +50,12 @@ public function makeWebhookRequest($webhookType, $data, $klaviyoId=null) | |||||||
| CURLOPT_URL => $url, | ||||||||
| CURLOPT_RETURNTRANSFER => true, | ||||||||
| CURLOPT_CUSTOMREQUEST => 'POST', | ||||||||
| CURLOPT_POSTFIELDS => json_encode($data), | ||||||||
| CURLOPT_POSTFIELDS => $data, | ||||||||
| CURLOPT_USERAGENT => self::USER_AGENT, | ||||||||
| CURLOPT_HTTPHEADER => array( | ||||||||
| 'Content-Type: application/json', | ||||||||
| 'Magento-two-signature: ' . $this->createWebhookSecurity($data), | ||||||||
| 'Content-Length: '. strlen(json_encode($data)), | ||||||||
| 'Content-Length: '. strlen($data), | ||||||||
| 'Topic: ' . $webhookType | ||||||||
| ), | ||||||||
| ]); | ||||||||
|
|
@@ -66,24 +65,23 @@ public function makeWebhookRequest($webhookType, $data, $klaviyoId=null) | |||||||
| $err = curl_errno($curl); | ||||||||
|
|
||||||||
| if ($err) { | ||||||||
| $this->_klaviyoLogger->log(sprintf('Unable to send webhook to %s with data: %s', $url, json_encode($data))); | ||||||||
| $this->_klaviyoLogger->log(sprintf('Unable to send webhook to %s with data: %s', $url, $data)); | ||||||||
|
||||||||
| $this->_klaviyoLogger->log(sprintf('Unable to send webhook to %s with data: %s', $url, $data)); | |
| $this->_klaviyoLogger->log("Unable to send webhook to $url with data: $data"); |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we add a description to this method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this work?
* @param string $data json payload used to create hmac signature
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| * @param string data | |
| * Returns an HMAC signature for webhooks | |
| * @param string data |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| <?php | ||
|
|
||
| namespace Klaviyo\Reclaim\Observer; | ||
|
|
||
| use Exception; | ||
| use Klaviyo\Reclaim\Helper\ScopeSetting; | ||
| use Klaviyo\Reclaim\Helper\Webhook; | ||
| use Klaviyo\Reclaim\Helper\Logger; | ||
| use Klaviyo\Reclaim\Model\ProductsFactory; | ||
|
|
||
| use Magento\Catalog\Model\CategoryFactory; | ||
| use Magento\Catalog\Model\Product; | ||
| use Magento\CatalogInventory\Api\StockRegistryInterface; | ||
| use Magento\Framework\Event\Observer; | ||
| use Magento\Framework\Event\ObserverInterface; | ||
|
|
||
|
|
||
| class ProductSaveAfter implements ObserverInterface | ||
| { | ||
| /** | ||
| * Klaviyo scope setting helper | ||
| * @var ScopeSetting $klaviyoScopeSetting | ||
| */ | ||
| protected $_klaviyoScopeSetting; | ||
|
|
||
| /** | ||
| * Klaviyo logger helper | ||
| * @var \Klaviyo\Reclaim\Helper\Logger $klaviyoLogger | ||
| */ | ||
| protected $_klaviyoLogger; | ||
|
|
||
| /** | ||
| * @var Webhook $webhookHelper | ||
| */ | ||
| protected $_webhookHelper; | ||
| protected $_categoryFactory; | ||
| protected $_klProductFactory; | ||
| protected $_stockRegistry; | ||
| protected $product_category_names = []; | ||
siddwarkhedkar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * @param Webhook $webhookHelper | ||
| * @param ScopeSetting $klaviyoScopeSetting | ||
| * @param CategoryFactory $categoryFactory | ||
| * @param ProductsFactory $klProductFactory | ||
| * @param StockRegistryInterface $stockRegistry | ||
| * @param Logger $klaviyoLogger | ||
| */ | ||
| public function __construct( | ||
| Webhook $webhookHelper, | ||
| ScopeSetting $klaviyoScopeSetting, | ||
| CategoryFactory $categoryFactory, | ||
| ProductsFactory $klProductFactory, | ||
| StockRegistryInterface $stockRegistry, | ||
| Logger $klaviyoLogger | ||
| ) { | ||
| $this->_webhookHelper = $webhookHelper; | ||
| $this->_klaviyoScopeSetting = $klaviyoScopeSetting; | ||
| $this->_categoryFactory = $categoryFactory; | ||
| $this->_klProductFactory = $klProductFactory; | ||
| $this->_klaviyoLogger = $klaviyoLogger; | ||
| $this->_stockRegistry = $stockRegistry; | ||
| } | ||
|
|
||
| /** | ||
| * customer register event handler | ||
| * | ||
| * @param Observer $observer | ||
| * @return void | ||
| * @throws Exception | ||
| */ | ||
| public function execute(Observer $observer) | ||
| { | ||
| $product = $observer->getEvent()->getProduct(); | ||
| $storeIds = $product->getStoreIds(); | ||
| $storeIdKlaviyoMap = $this->_klaviyoScopeSetting->getStoreIdKlaviyoAccountSetMap($storeIds); | ||
|
|
||
| foreach ($storeIdKlaviyoMap as $klaviyoId => $storeIds) { | ||
| if (empty($storeIds)) { | ||
| continue; | ||
| } | ||
|
|
||
| if ($this->_klaviyoScopeSetting->getWebhookSecret() && $this->_klaviyoScopeSetting->getProductSaveAfterSetting($storeIds[0])) { | ||
siddwarkhedkar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| $normalizedProduct = $this->normalizeProduct($product); | ||
| $data = [ | ||
| "status"=>"NEW", | ||
| "topic"=>"product/save", | ||
| "klaviyo_id"=>$klaviyoId, | ||
| "payload"=>json_encode($normalizedProduct) | ||
| ]; | ||
| $klProduct = $this->_klProductFactory->create(); | ||
| $klProduct->setData($data); | ||
| $klProduct->save(); | ||
siddwarkhedkar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // $this->_klaviyoLogger->log( print_r("Created new row?", true)); | ||
jordanallain marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // $this->_webhookHelper->makeWebhookRequest('product/save', $normalizedProduct, $klaviyoId); | ||
jordanallain marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| private function normalizeProduct($product=null) | ||
| { | ||
| if ($product == null) { | ||
| return; | ||
| } | ||
|
|
||
| $product_id = $product->getId(); | ||
|
|
||
| // remove thumnbail image url? | ||
jordanallain marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| $product_info = array( | ||
| 'product' => array( | ||
| 'store_ids' => $product->getStoreIds(), | ||
| 'ID' => $product_id, | ||
| 'TypeID' => $product->getTypeId(), | ||
| 'Name' => $product->getName(), | ||
| 'qty' => $this->_stockRegistry->getStockItem($product_id)->getQty(), | ||
| 'Visibility' => $product->getVisibility(), | ||
| 'IsInStock' => $product->isInStock(), | ||
| 'Status' => $product->getStatus(), | ||
| 'CreatedAt' => $product->getCreatedAt(), | ||
| 'UpdatedAt' => $product->getUpdatedAt(), | ||
| 'FirstImageURL' => $product->getImage(), | ||
| 'ThumbnailImageURL' => $product->getThumbnail(), | ||
| 'metadata' => array( | ||
| 'price' => $product->getPrice(), | ||
| 'sku' => $product->getSku() | ||
| ), | ||
| 'categories' => [] | ||
| ) | ||
| ); | ||
|
|
||
| if ($product->getSpecialPrice()) { | ||
| $product_info['metadata']['special_price'] = $product->getSpecialPrice(); | ||
| $product_info['metadata']['special_from_date'] = $product->getSpecialFromDate(); | ||
| $product_info['metadata']['special_to_date'] = $product->getSpecialToDate(); | ||
| } | ||
|
|
||
| $product_category_ids = $product->getCategoryIds(); | ||
| $category_factory = $this->_categoryFactory->create(); | ||
| foreach ($product_category_ids as $category_id) { | ||
| $category = $category_factory->load($category_id); | ||
| $product_info['categories'][$category_id] = $category->getName(); | ||
| } | ||
|
|
||
| return $product_info; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.