Skip to content

Commit

Permalink
closes #1184 for magento 2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzaloebiz committed May 26, 2021
1 parent 20dbc70 commit 1c32162
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 4 deletions.
50 changes: 50 additions & 0 deletions Block/Adminhtml/System/Config/ResyncProducts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Created by PhpStorm.
* User: gonzalo
* Date: 3/12/18
* Time: 2:12 PM
*/
namespace Ebizmarts\MailChimp\Block\Adminhtml\System\Config;

class ResyncProducts extends \Magento\Config\Block\System\Config\Form\Field
{
/**
* @var \Ebizmarts\MailChimp\Helper\Data
*/
private $_helper;

/**
* ResetErrors constructor.
* @param \Magento\Backend\Block\Template\Context $context
* @param \Ebizmarts\MailChimp\Helper\Data $helper
* @param array $data
*/
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Ebizmarts\MailChimp\Helper\Data $helper,
array $data = []
) {

$this->_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();
}
}
95 changes: 95 additions & 0 deletions Controller/Adminhtml/Ecommerce/ResyncProducts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
/**
* mc-magento2 Magento Component
*
* @category Ebizmarts
* @package mc-magento2
* @author Ebizmarts Team <[email protected]>
* @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');
}
}
10 changes: 10 additions & 0 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
8 changes: 7 additions & 1 deletion etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
<field id="*/*/active">1</field>
</depends>
</field>

<field id="monkeystore" translate="label" type="select" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Mailchimp Store</label>
<source_model>Ebizmarts\MailChimp\Model\Config\Source\MonkeyStore</source_model>
Expand Down Expand Up @@ -208,6 +207,13 @@
<field id="*/*/active">1</field>
</depends>
</field>
<field id="resync_products" translate="button_label" type="button" sortOrder="130" showInDefault="1" showInWebsite="1" showInStore="1">
<button_label>Resync all products</button_label>
<frontend_model>Ebizmarts\MailChimp\Block\Adminhtml\System\Config\ResyncProducts</frontend_model>
<depends>
<field id="*/*/active">1</field>
</depends>
</field>
</group>
<group id="abandonedcart" translate="label" type="text" sortOrder="130" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Abandoned Cart Configuration</label>
Expand Down
5 changes: 3 additions & 2 deletions view/adminhtml/templates/system/config/fieldset/hint.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"storeGridUrl": "<?php echo $this->getUrl('mailchimp/stores/index');?>",
"createWebhookUrl": "<?php echo $this->getUrl('mailchimp/ecommerce/createWebhook');?>",
"getInterestUrl": "<?php echo $this->getUrl('mailchimp/ecommerce/getInterest');?>",
"resyncSubscribersUrl": "<?php echo $this->getUrl('mailchimp/ecommerce/resyncSubscribers');?>"}}'>
"resyncSubscribersUrl": "<?php echo $this->getUrl('mailchimp/ecommerce/resyncSubscribers');?>",
"resyncProductsUrl": "<?php echo $this->getUrl('mailchimp/ecommerce/resyncProducts');?>"}}'>
</div>
<div style="background:#EAF0EE;border:1px solid #CCCCCC;margin-bottom:10px;padding:11px 0 0 10px;">
<h4>
Expand All @@ -26,7 +27,7 @@
<p><?php echo __('Need help? See our ') ?><a href="http://wiki.ebizmarts.com/installation" target="_blank"><?php echo __('Wiki') ?></a><?php echo __(' or browse our ') ?><a href="http://ebizmarts.com/forums" target="_blank"><?php echo __('support forums') ?></a><?php echo __(' Got feedback? ') ?><a href="mailto:[email protected]?Subject=MailChimp For Magento Version <?php echo $this->getModuleVersion()?>"><?php echo __('Email us') ?></a></p>
<p><?php echo __('You can find more extension in our ') ?><a href="http://store.ebizmarts.com" target="_blank"><?php echo __('Store') ?></a></p>
<?php if (!$this->getHasApiKey() ) : ?>
<p><a style="padding-top: 5px;padding-bottom:5px;padding-left: 30px;padding-right: 30px;background-color:#D75F07 ;color:white;font-weight: bold" href="https://bit.ly/2KpDH5C" target="_blank">Support Mailchimp4Magento, Subscribe for a free Mailchimp Account!</a> </p>
<p><a style="padding-top: 5px;padding-bottom:5px;padding-left: 30px;padding-right: 30px;background-color:#D75F07 ;color:white;font-weight: bold" href="https://bit.ly/2KpDH5C" target="_blank">Support Mailchimp4Magento, Subscribe for a free Mailchimp Account!</a> </p>
<?php endif; ?>
</div>
</h4>
Expand Down
6 changes: 6 additions & 0 deletions view/adminhtml/templates/system/config/resyncproducts.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div class="actions actions-resync-products">
<div id="validation_result" class="message-validation hidden"></div>
<button class="action-resync-products" type="button" id="<?php echo $block->getHtmlId() ?>">
<span><?php echo $block->escapeHtml($block->getButtonLabel()) ?></span>
</button>
</div>
23 changes: 22 additions & 1 deletion view/adminhtml/web/js/configapikey.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ define(
"storeGridUrl": "",
"createWebhookUrl": "",
"getInterestUrl": "",
"resyncSubscribersUrl": ""
"resyncSubscribersUrl": "",
"resyncProductsUrl": ""
},

_init: function () {
Expand All @@ -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) {
Expand All @@ -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({
Expand Down

0 comments on commit 1c32162

Please sign in to comment.