Multisite, validation of settings before saving, ICML catalog generation changed
This commit is contained in:
parent
ecd0dc03c0
commit
ee90b20a65
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
class Retailcrm_Retailcrm_Block_Adminhtml_System_Config_Backend_Api_Key extends Mage_Core_Model_Config_Data
|
||||
{
|
||||
protected function _beforeSave()
|
||||
{
|
||||
if ($this->isValueChanged()) {
|
||||
$api_url = $this->getFieldsetDataValue('api_url');
|
||||
$api_key = $this->getValue();
|
||||
|
||||
if (!$api_key) {
|
||||
Mage::throwException(Mage::helper('retailcrm')->__('Field API KEY could not be empty'));
|
||||
}
|
||||
|
||||
$api_client = new Retailcrm_Retailcrm_Model_ApiClient(
|
||||
$api_url,
|
||||
$api_key
|
||||
);
|
||||
|
||||
try {
|
||||
$response = $api_client->sitesList();
|
||||
} catch (Retailcrm_Retailcrm_Model_Exception_CurlException $curlException) {
|
||||
Mage::throwException(Mage::helper('retailcrm')->__($curlException->getMessage()));
|
||||
} catch (Retailcrm_Retailcrm_Model_Exception_InvalidJsonException $invalidJsonException) {
|
||||
Mage::throwException(Mage::helper('retailcrm')->__($invalidJsonException->getMessage()));
|
||||
} catch (\InvalidArgumentException $invalidArgumentException) {
|
||||
Mage::throwException(Mage::helper('retailcrm')->__($invalidArgumentException->getMessage()));
|
||||
}
|
||||
|
||||
if (isset($response['errorMsg'])) {
|
||||
Mage::throwException(Mage::helper('retailcrm')->__($response['errorMsg']));
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
class Retailcrm_Retailcrm_Block_Adminhtml_System_Config_Backend_Api_Url extends Mage_Core_Model_Config_Data
|
||||
{
|
||||
protected function _beforeSave()
|
||||
{
|
||||
if ($this->isValueChanged()) {
|
||||
$api_url = $this->getValue();
|
||||
$api_key = $this->getFieldsetDataValue('api_key');
|
||||
|
||||
if (!$api_url) {
|
||||
Mage::throwException(Mage::helper('retailcrm')->__('Field API URL could not be empty'));
|
||||
}
|
||||
|
||||
if (false === stripos($api_url, 'https://')) {
|
||||
$api_url = str_replace("http://", "https://", $api_url);
|
||||
$this->setValue($api_url);
|
||||
}
|
||||
|
||||
$api_client = new Retailcrm_Retailcrm_Model_ApiClient(
|
||||
$api_url,
|
||||
$api_key
|
||||
);
|
||||
|
||||
try {
|
||||
$response = $api_client->sitesList();
|
||||
} catch (Retailcrm_Retailcrm_Model_Exception_CurlException $curlException) {
|
||||
Mage::throwException(Mage::helper('retailcrm')->__($curlException->getMessage()));
|
||||
} catch (Retailcrm_Retailcrm_Model_Exception_InvalidJsonException $invalidJsonException) {
|
||||
Mage::throwException(Mage::helper('retailcrm')->__($invalidJsonException->getMessage()));
|
||||
} catch (\InvalidArgumentException $invalidArgumentException) {
|
||||
Mage::throwException(Mage::helper('retailcrm')->__($invalidArgumentException->getMessage()));
|
||||
}
|
||||
|
||||
if (isset($response['errorMsg'])) {
|
||||
Mage::throwException(Mage::helper('retailcrm')->__($response['errorMsg']));
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
@ -6,7 +6,7 @@ class Retailcrm_Retailcrm_Block_Adminhtml_System_Config_Form_Fieldset_Base exten
|
||||
protected $_apiKey;
|
||||
protected $_apiUrl;
|
||||
protected $_isCredentialCorrect;
|
||||
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
@ -14,30 +14,9 @@ class Retailcrm_Retailcrm_Block_Adminhtml_System_Config_Form_Fieldset_Base exten
|
||||
$this->_apiUrl = Mage::getStoreConfig('retailcrm/general/api_url');
|
||||
$this->_apiKey = Mage::getStoreConfig('retailcrm/general/api_key');
|
||||
$this->_isCredentialCorrect = false;
|
||||
|
||||
if (!empty($this->_apiUrl) && !empty($this->_apiKey)) {
|
||||
if (false === stripos($this->_apiUrl, 'https://')) {
|
||||
$this->_apiUrl = str_replace("http://", "https://", $this->_apiUrl);
|
||||
Mage::getModel('core/config')->saveConfig('retailcrm/general/api_url', $this->_apiUrl);
|
||||
}
|
||||
|
||||
$client = new Retailcrm_Retailcrm_Model_ApiClient(
|
||||
$this->_apiUrl,
|
||||
$this->_apiKey
|
||||
);
|
||||
|
||||
try {
|
||||
$response = $client->sitesList();
|
||||
} catch (Retailcrm_Retailcrm_Model_Exception_CurlException $e) {
|
||||
Mage::log($e->getMessage());
|
||||
}
|
||||
|
||||
if ($response->isSuccessful()) {
|
||||
$this->_isCredentialCorrect = true;
|
||||
|
||||
if($response['success'] != 1) {
|
||||
Mage::getModel('core/config')->saveConfig('retailcrm/general/api_url', '');
|
||||
}
|
||||
}
|
||||
$this->_isCredentialCorrect = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
class Retailcrm_Retailcrm_Block_Adminhtml_System_Config_Form_Fieldset_Site extends Retailcrm_Retailcrm_Block_Adminhtml_System_Config_Form_Fieldset_Base
|
||||
{
|
||||
public function render(Varien_Data_Form_Element_Abstract $element)
|
||||
{
|
||||
$html = $this->_getHeaderHtml($element);
|
||||
|
||||
if (!empty($this->_apiUrl) && !empty($this->_apiKey) && $this->_isCredentialCorrect) {
|
||||
$html .= $this->_getFieldHtml($element);
|
||||
} else {
|
||||
$html .= '<div style="margin-left: 15px;"><b><i>Please check your API Url & API Key</i></b></div>';
|
||||
}
|
||||
|
||||
$html .= $this->_getFooterHtml($element);
|
||||
return $html;
|
||||
}
|
||||
|
||||
protected function _getFieldRenderer()
|
||||
{
|
||||
if (empty($this->_fieldRenderer)) {
|
||||
$this->_fieldRenderer = Mage::getBlockSingleton('adminhtml/system_config_form_field');
|
||||
}
|
||||
|
||||
return $this->_fieldRenderer;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected function _getValues()
|
||||
{
|
||||
if (!empty($this->_apiUrl) && !empty($this->_apiKey) && $this->_isCredentialCorrect) {
|
||||
$client = new Retailcrm_Retailcrm_Model_ApiClient(
|
||||
$this->_apiUrl,
|
||||
$this->_apiKey
|
||||
);
|
||||
|
||||
try {
|
||||
$sites = $client->sitesList();
|
||||
} catch (Retailcrm_Retailcrm_Model_Exception_CurlException $e) {
|
||||
Mage::log($e->getMessage());
|
||||
}
|
||||
|
||||
if ($sites->isSuccessful()) {
|
||||
if (empty($this->_values)) {
|
||||
foreach ($sites['sites'] as $site) {
|
||||
$this->_values[] = array('label'=>Mage::helper('adminhtml')->__($site['name']), 'value'=>$site['code']);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->_values;
|
||||
}
|
||||
|
||||
protected function _getFieldHtml($fieldset)
|
||||
{
|
||||
$configData = $this->getConfigData();
|
||||
|
||||
$path = 'retailcrm/site/default';
|
||||
if (isset($configData[$path])) {
|
||||
$data = $configData[$path];
|
||||
$inherit = false;
|
||||
} else {
|
||||
$data = (int)(string)$this->getForm()->getConfigRoot()->descend($path);
|
||||
$inherit = true;
|
||||
}
|
||||
|
||||
$field = $fieldset->addField(
|
||||
'site_default', 'select',
|
||||
array(
|
||||
'name' => 'groups[site][fields][default][value]',
|
||||
'label' => 'Default',
|
||||
'value' => $data,
|
||||
'values' => $this->_getValues(),
|
||||
'inherit' => $inherit,
|
||||
'can_use_default_value' => 1,
|
||||
'can_use_website_value' => 1
|
||||
)
|
||||
)->setRenderer($this->_getFieldRenderer());
|
||||
|
||||
return $field->toHtml();
|
||||
}
|
||||
}
|
@ -0,0 +1,89 @@
|
||||
<?php
|
||||
class Retailcrm_Retailcrm_Block_Adminhtml_System_Config_Form_Fieldset_Sites extends Retailcrm_Retailcrm_Block_Adminhtml_System_Config_Form_Fieldset_Base
|
||||
{
|
||||
public function render(Varien_Data_Form_Element_Abstract $element)
|
||||
{
|
||||
$html = $this->_getHeaderHtml($element);
|
||||
|
||||
if (!empty($this->_apiUrl) && !empty($this->_apiKey) && $this->_isCredentialCorrect) {
|
||||
$websiteCode = Mage::app()->getRequest()->getParam('website');
|
||||
$website = Mage::app()->getWebsite()->load($websiteCode);
|
||||
|
||||
foreach ($website->getStoreCollection() as $store) {
|
||||
$html .= $this->_getFieldHtml($element, $store);
|
||||
}
|
||||
} else {
|
||||
$html .= '<div style="margin-left: 15px;"><b><i>Please check your API Url & API Key</i></b></div>';
|
||||
}
|
||||
|
||||
$html .= $this->_getFooterHtml($element);
|
||||
return $html;
|
||||
}
|
||||
|
||||
protected function _getFieldRenderer()
|
||||
{
|
||||
if (empty($this->_fieldRenderer)) {
|
||||
$this->_fieldRenderer = Mage::getBlockSingleton('adminhtml/system_config_form_field');
|
||||
}
|
||||
|
||||
return $this->_fieldRenderer;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected function _getValues()
|
||||
{
|
||||
if (!empty($this->_apiUrl) && !empty($this->_apiKey) && $this->_isCredentialCorrect) {
|
||||
$client = new Retailcrm_Retailcrm_Model_ApiClient(
|
||||
$this->_apiUrl,
|
||||
$this->_apiKey
|
||||
);
|
||||
|
||||
try {
|
||||
$sites = $client->sitesList();
|
||||
} catch (Retailcrm_Retailcrm_Model_Exception_CurlException $e) {
|
||||
Mage::log($e->getMessage());
|
||||
}
|
||||
|
||||
if ($sites->isSuccessful()) {
|
||||
if (empty($this->_values)) {
|
||||
foreach ($sites['sites'] as $site) {
|
||||
$this->_values[] = array('label'=>Mage::helper('adminhtml')->__($site['name']), 'value'=>$site['code']);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->_values;
|
||||
}
|
||||
|
||||
protected function _getFieldHtml($fieldset, $group)
|
||||
{
|
||||
$configData = $this->getConfigData();
|
||||
|
||||
$path = 'retailcrm/sites/' . $group->getCode();
|
||||
if (isset($configData[$path])) {
|
||||
$data = $configData[$path];
|
||||
$inherit = false;
|
||||
} else {
|
||||
$data = (int)(string)$this->getForm()->getConfigRoot()->descend($path);
|
||||
$inherit = true;
|
||||
}
|
||||
|
||||
$field = $fieldset->addField(
|
||||
'sites_' . $group->getCode(), 'select',
|
||||
array(
|
||||
'name' => 'groups[sites][fields][' . $group->getCode() . '][value]',
|
||||
'label' => $group->getName(),
|
||||
'value' => $data,
|
||||
'values' => $this->_getValues(),
|
||||
'inherit' => $inherit,
|
||||
'can_use_default_value' => 1,
|
||||
'can_use_website_value' => 1
|
||||
)
|
||||
)->setRenderer($this->_getFieldRenderer());
|
||||
|
||||
return $field->toHtml();
|
||||
}
|
||||
}
|
@ -37,7 +37,75 @@ class Retailcrm_Retailcrm_Helper_Data extends Mage_Core_Helper_Abstract
|
||||
*/
|
||||
const XML_API_KEY = 'retailcrm/general/api_key';
|
||||
|
||||
/**
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
const XML_SITES = 'retailcrm/sites/';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
const XML_SITE = 'retailcrm/site/default';
|
||||
|
||||
|
||||
/**
|
||||
* Get site code
|
||||
*
|
||||
* @param Mage_Core_Model_Store $store
|
||||
*
|
||||
* @return mixed|null
|
||||
*/
|
||||
public function getSite($store)
|
||||
{
|
||||
if (!$store instanceof Mage_Core_Model_Store
|
||||
&& is_int($store)
|
||||
) {
|
||||
$store = Mage::app()->getStore($store);
|
||||
}
|
||||
|
||||
$website = $store->getWebsite();
|
||||
$site = $website->getConfig(self::XML_SITES . $store->getCode());
|
||||
|
||||
if ($site) {
|
||||
return $site;
|
||||
} else {
|
||||
$site = Mage::getStoreConfig(self::XML_SITE);
|
||||
|
||||
if ($site) {
|
||||
return $site;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getMappingSites() {
|
||||
$sites = array();
|
||||
$webSites = Mage::app()->getWebsites();
|
||||
|
||||
foreach ($webSites as $webSite) {
|
||||
$storesFromSite = $webSite->getStores();
|
||||
|
||||
foreach ($storesFromSite as $store) {
|
||||
$config = $webSite->getConfig(self::XML_SITES . $store->getCode());
|
||||
|
||||
if ($config) {
|
||||
$sites[$config] = $store->getCode();
|
||||
}
|
||||
}
|
||||
|
||||
unset($storesFromSite);
|
||||
}
|
||||
|
||||
unset($webSites);
|
||||
|
||||
return $sites;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get api url
|
||||
*
|
||||
* @param Mage_Core_Model_Store $store store instance
|
||||
|
@ -31,6 +31,7 @@ class Retailcrm_Retailcrm_Model_Customer extends Retailcrm_Retailcrm_Model_Excha
|
||||
'lastName' => $data->getLastname(),
|
||||
'createdAt' => Mage::getSingleton('core/date')->date()
|
||||
);
|
||||
$this->_api->setSite(Mage::helper('retailcrm')->getSite($data->getStore()));
|
||||
$this->_api->customersEdit($customer);
|
||||
}
|
||||
|
||||
@ -44,7 +45,7 @@ class Retailcrm_Retailcrm_Model_Customer extends Retailcrm_Retailcrm_Model_Excha
|
||||
*/
|
||||
public function customersExport()
|
||||
{
|
||||
$customers = array();
|
||||
$customersSites = array();
|
||||
$customerCollection = Mage::getModel('customer/customer')
|
||||
->getCollection()
|
||||
->addAttributeToSelect('email')
|
||||
@ -57,19 +58,23 @@ class Retailcrm_Retailcrm_Model_Customer extends Retailcrm_Retailcrm_Model_Excha
|
||||
'firstName' => $customerData->getData('firstname'),
|
||||
'lastName' => $customerData->getData('lastname')
|
||||
);
|
||||
$customers[] = $customer;
|
||||
|
||||
$customersSites[$customerData->getStore()->getId()][] = $customer;
|
||||
}
|
||||
|
||||
|
||||
unset($customerCollection);
|
||||
$chunked = array_chunk($customers, 50);
|
||||
unset($customers);
|
||||
foreach ($chunked as $chunk) {
|
||||
$this->_api->customersUpload($chunk);
|
||||
time_nanosleep(0, 250000000);
|
||||
|
||||
foreach ($customersSites as $storeId => $customers) {
|
||||
$chunked = array_chunk($customers, 50);
|
||||
unset($customers);
|
||||
foreach ($chunked as $chunk) {
|
||||
$this->_api->customersUpload($chunk, Mage::helper('retailcrm')->getSite($storeId));
|
||||
time_nanosleep(0, 250000000);
|
||||
}
|
||||
|
||||
unset($chunked);
|
||||
}
|
||||
|
||||
unset($chunked);
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -6,9 +6,11 @@ class Retailcrm_Retailcrm_Model_Exchange
|
||||
protected $_apiUrl;
|
||||
protected $_config;
|
||||
protected $_api;
|
||||
|
||||
private $_helper;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->_helper = Mage::helper('retailcrm');
|
||||
$this->_apiUrl = Mage::getStoreConfig('retailcrm/general/api_url');
|
||||
$this->_apiKey = Mage::getStoreConfig('retailcrm/general/api_key');
|
||||
|
||||
@ -97,17 +99,17 @@ class Retailcrm_Retailcrm_Model_Exchange
|
||||
die();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $order
|
||||
*/
|
||||
private function doCreate($order)
|
||||
{
|
||||
Mage::log($order, null, 'retailcrmHistoriCreate.log', true);
|
||||
|
||||
|
||||
try {
|
||||
$response = $this->_api->ordersGet($order['id'], $by = 'id');
|
||||
|
||||
|
||||
if ($response->isSuccessful() && 200 === $response->getStatusCode()) {
|
||||
$order = $response->order;
|
||||
} else {
|
||||
@ -126,22 +128,32 @@ class Retailcrm_Retailcrm_Model_Exchange
|
||||
} catch (Retailcrm_Retailcrm_Model_Exception_CurlException $e) {
|
||||
Mage::log($e->getMessage());
|
||||
}
|
||||
|
||||
|
||||
// get references
|
||||
$this->_config = Mage::getStoreConfig('retailcrm');
|
||||
$payments = array_flip(array_filter($this->_config['payment']));
|
||||
$shippings = array_flip(array_filter($this->_config['shipping']));
|
||||
|
||||
|
||||
// get store
|
||||
$_sendConfirmation = '0';
|
||||
$storeId = Mage::app()->getStore()->getId();
|
||||
$sitesConfig = $this->_helper->getMappingSites();
|
||||
|
||||
if (!$sitesConfig) {
|
||||
$storeId = Mage::app()
|
||||
->getWebsite(true)
|
||||
->getDefaultGroup()
|
||||
->getDefaultStoreId();
|
||||
} else {
|
||||
$storeId = Mage::app()->getStore()->load($sitesConfig[$order['site']])->getId();
|
||||
}
|
||||
|
||||
$siteid = Mage::getModel('core/store')->load($storeId)->getWebsiteId();
|
||||
|
||||
|
||||
// search or create customer
|
||||
$customer = Mage::getSingleton('customer/customer');
|
||||
$customer->setWebsiteId($siteid);
|
||||
$customer->loadByEmail($order['email']);
|
||||
|
||||
|
||||
if (!is_numeric($customer->getId())) {
|
||||
$customer
|
||||
->setGroupId(1)
|
||||
@ -152,7 +164,7 @@ class Retailcrm_Retailcrm_Model_Exchange
|
||||
->setLastname($order['lastName'])
|
||||
->setMiddleName($order['patronymic'])
|
||||
->setPassword(uniqid());
|
||||
|
||||
|
||||
try {
|
||||
$customer->save();
|
||||
$customer->setConfirmation(null);
|
||||
@ -331,7 +343,7 @@ class Retailcrm_Retailcrm_Model_Exchange
|
||||
$response = $this->_api->ordersGet($order['id'], $by = 'id');
|
||||
|
||||
if ($response->isSuccessful() && 200 === $response->getStatusCode()) {
|
||||
$order = $response->order;
|
||||
$order = $response->order;
|
||||
} else {
|
||||
Mage::log(
|
||||
sprintf(
|
||||
@ -348,7 +360,7 @@ class Retailcrm_Retailcrm_Model_Exchange
|
||||
} catch (Retailcrm_Retailcrm_Model_Exception_CurlException $e) {
|
||||
Mage::log($e->getMessage());
|
||||
}
|
||||
|
||||
|
||||
// get references
|
||||
$this->_config = Mage::getStoreConfig('retailcrm');
|
||||
$payments = array_flip(array_filter($this->_config['payment']));
|
||||
@ -356,7 +368,17 @@ class Retailcrm_Retailcrm_Model_Exchange
|
||||
|
||||
// get store
|
||||
$_sendConfirmation = '0';
|
||||
$storeId = Mage::app()->getStore()->getId();
|
||||
$sitesConfig = $this->_helper->getMappingSites();
|
||||
|
||||
if (!$sitesConfig) {
|
||||
$storeId = Mage::app()
|
||||
->getWebsite(true)
|
||||
->getDefaultGroup()
|
||||
->getDefaultStoreId();
|
||||
} else {
|
||||
$storeId = Mage::app()->getStore()->load($sitesConfig[$order['site']])->getId();
|
||||
}
|
||||
|
||||
$siteid = Mage::getModel('core/store')->load($storeId)->getWebsiteId();
|
||||
|
||||
// search or create customer
|
||||
@ -504,7 +526,7 @@ class Retailcrm_Retailcrm_Model_Exchange
|
||||
$oldOrder = Mage::getModel('sales/order')->loadByIncrementId($originalId);
|
||||
$oldOrderArr = $oldOrder->getData();
|
||||
|
||||
if(!empty($oldOrderArr['original_increment_id'])) {
|
||||
if (!empty($oldOrderArr['original_increment_id'])) {
|
||||
$originalId = $oldOrderArr['original_increment_id'];
|
||||
}
|
||||
|
||||
|
@ -14,7 +14,7 @@ class Retailcrm_Retailcrm_Model_Icml
|
||||
$string = '<?xml version="1.0" encoding="UTF-8"?>
|
||||
<yml_catalog date="'.date('Y-m-d H:i:s').'">
|
||||
<shop>
|
||||
<name>'.Mage::app()->getStore($shop)->getName().'</name>
|
||||
<name>'.$shop->getName().'</name>
|
||||
<categories/>
|
||||
<offers/>
|
||||
</shop>
|
||||
@ -41,9 +41,8 @@ class Retailcrm_Retailcrm_Model_Icml
|
||||
|
||||
$this->_dd->saveXML();
|
||||
$baseDir = Mage::getBaseDir();
|
||||
$shopCode = Mage::app()->getStore($shop)->getCode();
|
||||
$shopCode = $shop->getCode();
|
||||
$this->_dd->save($baseDir . DS . 'retailcrm_' . $shopCode . '.xml');
|
||||
|
||||
}
|
||||
|
||||
private function addCategories()
|
||||
@ -102,7 +101,6 @@ class Retailcrm_Retailcrm_Model_Icml
|
||||
|
||||
$collection->addFieldToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
|
||||
$collection->addFieldToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
|
||||
$collection->addAttributeToFilter('type_id', array('eq' => 'simple'));
|
||||
|
||||
foreach ($collection as $product) {
|
||||
/** @var Mage_Catalog_Model_Product $product */
|
||||
@ -174,6 +172,7 @@ class Retailcrm_Retailcrm_Model_Icml
|
||||
$offers[] = $offer;
|
||||
|
||||
if($product->getTypeId() == 'configurable') {
|
||||
|
||||
/** @var Mage_Catalog_Model_Product_Type_Configurable $product */
|
||||
$associatedProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null, $product);
|
||||
|
||||
|
@ -59,8 +59,8 @@ class Retailcrm_Retailcrm_Model_Observer
|
||||
public function exportCatalog()
|
||||
{
|
||||
foreach (Mage::app()->getWebsites() as $website) {
|
||||
foreach ($website->getGroups() as $group) {
|
||||
Mage::getModel('retailcrm/icml')->generate((int)$group->getId());
|
||||
foreach ($website->getStores() as $store) {
|
||||
Mage::getModel('retailcrm/icml')->generate($store);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -25,17 +25,20 @@ class Retailcrm_Retailcrm_Model_Order extends Retailcrm_Retailcrm_Model_Exchange
|
||||
public function orderPay($orderId)
|
||||
{
|
||||
$order = Mage::getModel('sales/order')->load($orderId);
|
||||
|
||||
if((string)$order->getBaseGrandTotal() == (string)$order->getTotalPaid()){
|
||||
|
||||
if ((string)$order->getBaseGrandTotal() == (string)$order->getTotalPaid()) {
|
||||
$preparedOrder = array(
|
||||
'externalId' => $order->getRealOrderId(),//getId(),
|
||||
'paymentStatus' => 'paid',
|
||||
);
|
||||
$preparedOrder = Mage::helper('retailcrm')->filterRecursive($preparedOrder);
|
||||
|
||||
$helper = Mage::helper('retailcrm');
|
||||
$preparedOrder = $helper->filterRecursive($preparedOrder);
|
||||
$this->_api->setSite($helper->getSite($order->getStore()));
|
||||
$this->_api->ordersEdit($preparedOrder);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function orderStatusHistoryCheck($order)
|
||||
{
|
||||
$config = Mage::getModel(
|
||||
@ -48,19 +51,19 @@ class Retailcrm_Retailcrm_Model_Order extends Retailcrm_Retailcrm_Model_Exchange
|
||||
'externalId' => $order->getRealOrderId(),//getId(),
|
||||
'status' => $config->getMapping($order->getStatus(), 'status'),
|
||||
);
|
||||
|
||||
$comment = $order->getStatusHistoryCollection()->getData();
|
||||
|
||||
if(!empty($comment[0]['comment'])) {
|
||||
$preparedOrder['managerComment'] = $comment[0]['comment'];
|
||||
}
|
||||
|
||||
$preparedOrder = Mage::helper('retailcrm')->filterRecursive($preparedOrder);
|
||||
$this->_api->ordersEdit($preparedOrder);
|
||||
|
||||
|
||||
$comment = $order->getStatusHistoryCollection()->getData();
|
||||
|
||||
if(!empty($comment[0]['comment'])) {
|
||||
$preparedOrder['managerComment'] = $comment[0]['comment'];
|
||||
}
|
||||
|
||||
$helper = Mage::helper('retailcrm');
|
||||
$preparedOrder = $helper->filterRecursive($preparedOrder);
|
||||
$this->_api->setSite($helper->getSite($order->getStore()));
|
||||
$this->_api->ordersEdit($preparedOrder);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function orderUpdate($order)
|
||||
{
|
||||
$config = Mage::getModel(
|
||||
@ -73,16 +76,18 @@ class Retailcrm_Retailcrm_Model_Order extends Retailcrm_Retailcrm_Model_Exchange
|
||||
'externalId' => $order->getRealOrderId(),//getId(),
|
||||
'status' => $config->getMapping($order->getStatus(), 'status'),
|
||||
);
|
||||
if((float)$order->getBaseGrandTotal() == (float)$order->getTotalPaid()) {
|
||||
if ((float)$order->getBaseGrandTotal() == (float)$order->getTotalPaid()) {
|
||||
$preparedOrder['paymentStatus'] = 'paid';
|
||||
$preparedOrder = Mage::helper('retailcrm')->filterRecursive($preparedOrder);
|
||||
$helper = Mage::helper('retailcrm');
|
||||
$preparedOrder = $helper->filterRecursive($preparedOrder);
|
||||
$this->_api->setSite($helper->getSite($order->getStore()));
|
||||
$this->_api->ordersEdit($preparedOrder);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function orderCreate($order)
|
||||
{
|
||||
$helper = Mage::helper('retailcrm');
|
||||
$config = Mage::getModel('retailcrm/settings', ['storeId' => $order->getStoreId()]);
|
||||
$address = $order->getShippingAddress()->getData();
|
||||
$orderItems = $order->getItemsCollection()
|
||||
@ -127,7 +132,6 @@ class Retailcrm_Retailcrm_Model_Order extends Retailcrm_Retailcrm_Model_Exchange
|
||||
$shipping = $this->getShippingCode($order->getShippingMethod());
|
||||
|
||||
$preparedOrder = array(
|
||||
'site' => $order->getStore()->getCode(),
|
||||
'externalId' => $order->getRealOrderId(),
|
||||
'number' => $order->getRealOrderId(),
|
||||
'createdAt' => Mage::getModel('core/date')->date(),
|
||||
@ -164,17 +168,16 @@ class Retailcrm_Retailcrm_Model_Order extends Retailcrm_Retailcrm_Model_Exchange
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
if(trim($preparedOrder['delivery']['code']) == ''){
|
||||
|
||||
if (trim($preparedOrder['delivery']['code']) == ''){
|
||||
unset($preparedOrder['delivery']['code']);
|
||||
}
|
||||
|
||||
if(trim($preparedOrder['paymentType']) == ''){
|
||||
if (trim($preparedOrder['paymentType']) == ''){
|
||||
unset($preparedOrder['paymentType']);
|
||||
}
|
||||
|
||||
if(trim($preparedOrder['status']) == ''){
|
||||
if (trim($preparedOrder['status']) == ''){
|
||||
unset($preparedOrder['status']);
|
||||
}
|
||||
|
||||
@ -182,18 +185,18 @@ class Retailcrm_Retailcrm_Model_Order extends Retailcrm_Retailcrm_Model_Exchange
|
||||
$preparedCustomer = array(
|
||||
'externalId' => $order->getCustomerId()
|
||||
);
|
||||
|
||||
|
||||
if ($this->_api->customersCreate($preparedCustomer)) {
|
||||
$preparedOrder['customer']['externalId'] = $order->getCustomerId();
|
||||
}
|
||||
}
|
||||
|
||||
$preparedOrder = Mage::helper('retailcrm')->filterRecursive($preparedOrder);
|
||||
|
||||
|
||||
$preparedOrder = $helper->filterRecursive($preparedOrder);
|
||||
|
||||
Mage::log($preparedOrder, null, 'retailcrmCreatePreparedOrder.log', true);
|
||||
|
||||
|
||||
try {
|
||||
$response = $this->_api->ordersCreate($preparedOrder);
|
||||
$response = $this->_api->ordersCreate($preparedOrder, $helper->getSite($order->getStore()));
|
||||
if ($response->isSuccessful() && 201 === $response->getStatusCode()) {
|
||||
Mage::log($response->id);
|
||||
} else {
|
||||
@ -218,8 +221,8 @@ class Retailcrm_Retailcrm_Model_Order extends Retailcrm_Retailcrm_Model_Exchange
|
||||
{
|
||||
$config = Mage::getStoreConfig('retailcrm');
|
||||
$ordersId = explode(",", $config['load_order']['numberOrder']);
|
||||
$orders = array();
|
||||
|
||||
$ordersSites = array();
|
||||
|
||||
$ordersList = Mage::getResourceModel('sales/order_collection')
|
||||
->addAttributeToSelect('*')
|
||||
->joinAttribute('billing_firstname', 'order_address/firstname', 'billing_address_id', null, 'left')
|
||||
@ -247,20 +250,22 @@ class Retailcrm_Retailcrm_Model_Order extends Retailcrm_Retailcrm_Model_Exchange
|
||||
->setCurPage(1)
|
||||
->addAttributeToFilter('increment_id', $ordersId)
|
||||
->load();
|
||||
|
||||
|
||||
foreach ($ordersList as $order) {
|
||||
$orders[] = $this->prepareOrder($order);
|
||||
$ordersSites[$order->getStore()->getId()][] = $this->prepareOrder($order);
|
||||
}
|
||||
|
||||
$chunked = array_chunk($orders, 50);
|
||||
unset($orders);
|
||||
foreach ($chunked as $chunk) {
|
||||
$this->_api->ordersUpload($chunk);
|
||||
time_nanosleep(0, 250000000);
|
||||
|
||||
foreach ($ordersSites as $storeId => $orders) {
|
||||
$chunked = array_chunk($orders, 50);
|
||||
unset($orders);
|
||||
foreach ($chunked as $chunk) {
|
||||
$this->_api->ordersUpload($chunk, Mage::helper('retailcrm')->getSite($storeId));
|
||||
time_nanosleep(0, 250000000);
|
||||
}
|
||||
|
||||
unset($chunked);
|
||||
}
|
||||
|
||||
unset($chunked);
|
||||
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
@ -275,7 +280,7 @@ class Retailcrm_Retailcrm_Model_Order extends Retailcrm_Retailcrm_Model_Exchange
|
||||
*/
|
||||
public function ordersExport()
|
||||
{
|
||||
$orders = array();
|
||||
$ordersSites = array();
|
||||
$ordersList = Mage::getResourceModel('sales/order_collection')
|
||||
->addAttributeToSelect('*')
|
||||
->joinAttribute('billing_firstname', 'order_address/firstname', 'billing_address_id', null, 'left')
|
||||
@ -302,20 +307,22 @@ class Retailcrm_Retailcrm_Model_Order extends Retailcrm_Retailcrm_Model_Exchange
|
||||
->setPageSize(1000)
|
||||
->setCurPage(1)
|
||||
->load();
|
||||
|
||||
|
||||
foreach ($ordersList as $order) {
|
||||
$orders[] = $this->prepareOrder($order);
|
||||
$ordersSites[$order->getStore()->getId()][] = $this->prepareOrder($order);
|
||||
}
|
||||
|
||||
$chunked = array_chunk($orders, 50);
|
||||
unset($orders);
|
||||
foreach ($chunked as $chunk) {
|
||||
$this->_api->ordersUpload($chunk);
|
||||
time_nanosleep(0, 250000000);
|
||||
|
||||
foreach ($ordersSites as $storeId => $orders) {
|
||||
$chunked = array_chunk($orders, 50);
|
||||
unset($orders);
|
||||
foreach ($chunked as $chunk) {
|
||||
$this->_api->ordersUpload($chunk, Mage::helper('retailcrm')->getSite($storeId));
|
||||
time_nanosleep(0, 250000000);
|
||||
}
|
||||
|
||||
unset($chunked);
|
||||
}
|
||||
|
||||
unset($chunked);
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -323,7 +330,7 @@ class Retailcrm_Retailcrm_Model_Order extends Retailcrm_Retailcrm_Model_Exchange
|
||||
{
|
||||
$config = Mage::getModel('retailcrm/settings', ['storeId' => $order->getStoreId()]);
|
||||
$address = $order->getShippingAddress();
|
||||
|
||||
|
||||
$orderItems = $order->getItemsCollection()
|
||||
->addAttributeToSelect('*')
|
||||
->addAttributeToFilter('product_type', array('eq'=>'simple'))
|
||||
|
@ -57,6 +57,7 @@ SOFTWARE.
|
||||
<show_in_website>1</show_in_website>
|
||||
<show_in_store>1</show_in_store>
|
||||
<tooltip><![CDATA[https://<i><b>YourCrmName</b></i>.retailcrm.ru]]></tooltip>
|
||||
<backend_model>retailcrm_retailcrm_block_adminhtml_system_config_backend_api_url</backend_model>
|
||||
</api_url>
|
||||
<api_key translate="label comment">
|
||||
<label>API Key</label>
|
||||
@ -66,6 +67,7 @@ SOFTWARE.
|
||||
<show_in_website>1</show_in_website>
|
||||
<show_in_store>1</show_in_store>
|
||||
<tooltip><![CDATA[To generate an API Key, log in to RetailCRM then select Admin > Integration > API Keys]]></tooltip>
|
||||
<backend_model>retailcrm_retailcrm_block_adminhtml_system_config_backend_api_key</backend_model>
|
||||
</api_key>
|
||||
</fields>
|
||||
</general>
|
||||
@ -153,7 +155,26 @@ SOFTWARE.
|
||||
|
||||
</fields>
|
||||
</load_order>
|
||||
|
||||
<site translate="label comment" module="retailcrm">
|
||||
<expanded>1</expanded>
|
||||
<label>Site</label>
|
||||
<frontend_type>text</frontend_type>
|
||||
<show_in_default>1</show_in_default>
|
||||
<show_in_website>0</show_in_website>
|
||||
<show_in_store>0</show_in_store>
|
||||
<sort_order>16</sort_order>
|
||||
<frontend_model>retailcrm/adminhtml_system_config_form_fieldset_site</frontend_model>
|
||||
</site>
|
||||
<sites translate="label comment" module="retailcrm">
|
||||
<expanded>1</expanded>
|
||||
<label>Sites</label>
|
||||
<frontend_type>text</frontend_type>
|
||||
<show_in_default>0</show_in_default>
|
||||
<show_in_website>1</show_in_website>
|
||||
<show_in_store>1</show_in_store>
|
||||
<sort_order>17</sort_order>
|
||||
<frontend_model>retailcrm/adminhtml_system_config_form_fieldset_sites</frontend_model>
|
||||
</sites>
|
||||
</groups>
|
||||
</retailcrm>
|
||||
</sections>
|
||||
|
Loading…
Reference in New Issue
Block a user