diff --git a/Block/Adminhtml/System/Config/ResyncProducts.php b/Block/Adminhtml/System/Config/ResyncProducts.php new file mode 100644 index 00000000..3a20b83d --- /dev/null +++ b/Block/Adminhtml/System/Config/ResyncProducts.php @@ -0,0 +1,50 @@ +_helper = $helper; + parent::__construct($context, $data); + } + + protected function _construct() + { + parent::_construct(); + $this->setTemplate('system/config/resyncproducts.phtml'); + } + + protected function _getElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element) + { + $originalData = $element->getOriginalData(); + $this->addData( + [ + 'button_label' => __($originalData['button_label']), + 'html_id' => $element->getHtmlId(), + ] + ); + return $this->_toHtml(); + } +} diff --git a/Controller/Adminhtml/Ecommerce/ResyncProducts.php b/Controller/Adminhtml/Ecommerce/ResyncProducts.php new file mode 100644 index 00000000..9cc844c3 --- /dev/null +++ b/Controller/Adminhtml/Ecommerce/ResyncProducts.php @@ -0,0 +1,95 @@ + + * @copyright Ebizmarts (http://ebizmarts.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + * @date: 2/21/17 5:07 PM + * @file: ResetLocalErrors.php + */ + +namespace Ebizmarts\MailChimp\Controller\Adminhtml\Ecommerce; + +use Magento\Framework\Controller\Result\JsonFactory; +use Magento\Framework\Exception\ValidatorException; +use Symfony\Component\Config\Definition\Exception\Exception; + +class ResyncProducts extends \Magento\Backend\App\Action +{ + /** + * @var JsonFactory + */ + protected $resultJsonFactory; + /** + * @var \Ebizmarts\MailChimp\Helper\Data + */ + protected $helper; + /** + * @var \Magento\Store\Model\StoreManagerInterface + */ + protected $storeManager; + + /** + * ResetLocalErrors constructor. + * @param \Magento\Backend\App\Action\Context $context + * @param JsonFactory $resultJsonFactory + * @param \Magento\Store\Model\StoreManagerInterface $storeManagerInterface + * @param \Ebizmarts\MailChimp\Helper\Data $helper + */ + public function __construct( + \Magento\Backend\App\Action\Context $context, + JsonFactory $resultJsonFactory, + \Magento\Store\Model\StoreManagerInterface $storeManagerInterface, + \Ebizmarts\MailChimp\Helper\Data $helper + ) { + + parent::__construct($context); + $this->resultJsonFactory = $resultJsonFactory; + $this->helper = $helper; + $this->storeManager = $storeManagerInterface; + } + + public function execute() + { + $valid = 1; + $message = ''; + $params = $this->getRequest()->getParams(); + if (isset($params['website'])) { + $mailchimpStore = $this->helper->getConfigValue( + \Ebizmarts\MailChimp\Helper\Data::XML_MAILCHIMP_STORE, + $params['website'], + 'website' + ); + } elseif (isset($params['store'])) { + $mailchimpStore = $this->helper->getConfigValue( + \Ebizmarts\MailChimp\Helper\Data::XML_MAILCHIMP_STORE, + $params['store'], + 'store' + ); + } else { + $mailchimpStore = $this->helper->getConfigValue( + \Ebizmarts\MailChimp\Helper\Data::XML_MAILCHIMP_STORE, + $this->storeManager->getStore() + ); + } + + $resultJson = $this->resultJsonFactory->create(); + try { + $this->helper->resyncProducts($mailchimpStore); + } catch (ValidatorException $e) { + $valid = 0; + $message = $e->getMessage(); + } + return $resultJson->setData([ + 'valid' => (int)$valid, + 'message' => $message, + ]); + } + protected function _isAllowed() + { + return $this->_authorization->isAllowed('Ebizmarts_MailChimp::config_mailchimp'); + } +} diff --git a/Helper/Data.php b/Helper/Data.php index da0d25d6..9a6f5783 100755 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -1323,6 +1323,16 @@ public function resyncAllSubscribers($mailchimpList) $connection->update($tableName, ['mailchimp_sync_modified' => 1], "type = '" . self::IS_SUBSCRIBER . "' and mailchimp_store_id = '$mailchimpList'"); } + public function resyncProducts($mailchimpList) + { + $connection = $this->_mailChimpSyncE->getResource()->getConnection(); + $tableName = $this->_mailChimpSyncE->getResource()->getMainTable(); + $connection->update( + $tableName, + ['mailchimp_sync_modified' => 1], + "type = '" . self::IS_PRODUCT . "' and mailchimp_store_id = '$mailchimpList'" + ); + } public function decrypt($value) { return $this->_encryptor->decrypt($value); } diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index e5c83d69..59d239f3 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -44,7 +44,6 @@ 1 - Ebizmarts\MailChimp\Model\Config\Source\MonkeyStore @@ -208,6 +207,13 @@ 1 + + Resync all products + Ebizmarts\MailChimp\Block\Adminhtml\System\Config\ResyncProducts + + 1 + + diff --git a/view/adminhtml/templates/system/config/fieldset/hint.phtml b/view/adminhtml/templates/system/config/fieldset/hint.phtml index 917f151b..c002a9f5 100644 --- a/view/adminhtml/templates/system/config/fieldset/hint.phtml +++ b/view/adminhtml/templates/system/config/fieldset/hint.phtml @@ -16,7 +16,8 @@ "storeGridUrl": "getUrl('mailchimp/stores/index');?>", "createWebhookUrl": "getUrl('mailchimp/ecommerce/createWebhook');?>", "getInterestUrl": "getUrl('mailchimp/ecommerce/getInterest');?>", - "resyncSubscribersUrl": "getUrl('mailchimp/ecommerce/resyncSubscribers');?>"}}'> + "resyncSubscribersUrl": "getUrl('mailchimp/ecommerce/resyncSubscribers');?>", + "resyncProductsUrl": "getUrl('mailchimp/ecommerce/resyncProducts');?>"}}'>

@@ -26,7 +27,7 @@

getHasApiKey() ) : ?> -

Support Mailchimp4Magento, Subscribe for a free Mailchimp Account!

+

Support Mailchimp4Magento, Subscribe for a free Mailchimp Account!

diff --git a/view/adminhtml/templates/system/config/resyncproducts.phtml b/view/adminhtml/templates/system/config/resyncproducts.phtml new file mode 100644 index 00000000..e2e5c3c1 --- /dev/null +++ b/view/adminhtml/templates/system/config/resyncproducts.phtml @@ -0,0 +1,6 @@ +
+ + +
diff --git a/view/adminhtml/web/js/configapikey.js b/view/adminhtml/web/js/configapikey.js index 6caa5316..0ff0c9da 100644 --- a/view/adminhtml/web/js/configapikey.js +++ b/view/adminhtml/web/js/configapikey.js @@ -22,7 +22,8 @@ define( "storeGridUrl": "", "createWebhookUrl": "", "getInterestUrl": "", - "resyncSubscribersUrl": "" + "resyncSubscribersUrl": "", + "resyncProductsUrl": "" }, _init: function () { @@ -48,6 +49,10 @@ define( var mailchimpStoreId = $('#mailchimp_general_monkeystore').find(':selected').val(); self._resyncSubscribers(mailchimpStoreId); }); + $('#mailchimp_ecommerce_resync_products').click(function () { + var mailchimpStoreId = $('#mailchimp_general_monkeystore').find(':selected').val(); + self._resyncProducts(mailchimpStoreId); + }); }, _resyncSubscribers: function (mailchimpStoreId) { @@ -66,6 +71,22 @@ define( } }); }, + _resyncProducts: function (mailchimpStoreId) { + var resyncProductsUrl = this.options.resyncProductsUrl; + $.ajax({ + url: resyncProductsUrl, + data: {'form_key': window.FORM_KEY, 'mailchimpStoreId': mailchimpStoreId}, + type: 'GET', + dataType: 'json', + showLoader: true + }).done(function (data) { + if (data.valid == 0) { + alert({content: 'Error: can\'t resync your products'}); + } else if (data.valid == 1) { + alert({content: 'All products marked for resync'}); + } + }); + }, _createWebhook: function (apiKey, listId) { var createWebhookUrl = this.options.createWebhookUrl; $.ajax({