Advertisement
Guest User

Untitled

a guest
Jun 30th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.60 KB | None | 0 0
  1. <?php
  2.  
  3. class Polcode_Crm_Model_Data_Import extends Polcode_Crm_Model_Loggable {
  4.  
  5.     /**
  6.      * @var Polcode_Crm_Model_Proxy_Interface
  7.      */
  8.     protected $_proxy;
  9.  
  10.     public function setProxy(Polcode_Crm_Model_Proxy_Interface $proxy) {
  11.         $this->_proxy = $proxy;
  12.         return $this;
  13.     }
  14.  
  15.     public function sync() {
  16.         $this->log('customers');
  17.  
  18.         $this->_syncCustomers();
  19.  
  20.         $this->log('products');
  21.  
  22.         $this->_syncProducts();
  23.     }
  24.  
  25.     protected function _syncCustomers() {
  26.         /* @var getCustomers Polcode_Crm_Model_Proxy_Sugar */
  27.         $customers = $this->_proxy->getCustomers();
  28.  
  29.         foreach ($customers as $customer) {
  30.             $this->saveCrmCustomer($customer);
  31.         }
  32.     }
  33.  
  34.     protected function _syncProducts() {
  35.         /* @var getProducts Polcode_Crm_Model_Proxy_Sugar */
  36.         $products = $this->_proxy->getProducts();
  37.  
  38.         foreach ($products as $product) {
  39.             $this->saveCrmProduct($product);
  40.         }
  41.     }
  42.  
  43.     public function saveCrmCustomer(Varien_Data $customer) {
  44.         if (empty($customer->getEmail())) {
  45.             throw new Exception('Email customer is required, set customer"s id: ' . $customer->id . ' email');
  46.         } elseif ($this->checkCustomerExist($customer->getEmail())) {
  47.             $this->log('Customer id: ' . $customer->id . ' already exist in system');
  48.         } else {
  49.             Mage::getModel('customer/customer')
  50.                     ->setWebsiteId(Mage::app()->getWebsite()->getId())
  51.                     ->setStoreId(Mage::app()->getStore()->getStoreId())
  52.                     ->addData($customer->getData())
  53.                     ->save();
  54.         }
  55.     }
  56.  
  57.     private function parseString($string) {
  58.         return explode(' ', $string);
  59.     }
  60.  
  61.     public function checkCustomerExist($email) {
  62.         $customer = Mage::getModel('customer/customer');
  63.         $collectionCount = $customer->getCollection()
  64.                 ->addFieldToFilter('email', $email)
  65.                 ->count();
  66.  
  67.         return $collectionCount;
  68.     }
  69.  
  70.     public function saveCrmProduct(Varien_Data $product) {
  71.         Mage::getModel('catalog/product')
  72.                 ->setAttributeSetId(4)
  73.                 ->setTypeId('simple')
  74.                 ->addData($product->getData())
  75.                 ->save();
  76.     }
  77.  
  78.     public function checkProductExist($sku) {
  79.         $customer = Mage::getModel('catalog/product');
  80.         $collectionCount = $customer->getCollection()
  81.                 ->addFieldToFilter('sku', $sku)
  82.                 ->count();
  83.  
  84.         return $collectionCount;
  85.     }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement