public function syncProductAndProductCategory()
 {
     $cronStartDateTime = UDate::now();
     $criteria = array("(sku != '' OR (mageId != '' AND mageId != 0))");
     $param = array();
     if (($lastUpdateDateTime = trim(SystemSettings::getSettings(SystemSettings::TYPE_PRODUCT_LAST_UPDATED))) !== '') {
         $criteria[] = "updated >= ?";
         $param[] = $lastUpdateDateTime;
     }
     $counter = Product::countByCriteria(implode(" and ", $criteria), $param);
     if ($counter > 0) {
         $this->_mySoapClient = $this->_connect();
         $this->syncAllProductCategory();
         $productArray = Product::findByCriteria(implode(" and ", $criteria), $param);
         foreach ($productArray as $product) {
             $linkedCategories = array();
             $product_categoryArray = Product_Category::getCategories($product);
             if (is_array($product_categoryArray) && count($product_categoryArray) > 0) {
                 $linkedCategories = array_map(create_function('$a', 'return $a->getCategory()->getMageId();'), $product_categoryArray);
             }
             /// if no category is found, set the default to 1 ///
             if (count($linkedCategories) <= 0) {
                 $linkedCategories = array(1);
             }
             $productData = $this->_generateProductData($product, $linkedCategories);
             if (($productMageId = trim($product->getMageId())) === '' || $productMageId === '0') {
                 $attributeSets = $this->_mySoapClient->catalogProductAttributeSetList($session);
                 $attributeSet = current($attributeSets);
                 $productType = 'simple';
                 $newMageId = $this->_mySoapClient->catalogProductCreate($this->_session, $productType, $attributeSet->set_id, trim($product->getSku()), $productData);
                 if (is_numeric($newMageId)) {
                     $product->setMageId($newMageId)->save();
                 } else {
                     $this->_handle_failed_product($product, $cronStartDateTime);
                 }
             } else {
                 // update product on magento
                 $updated = $this->_mySoapClient->catalogProductUpdate($this->_session, $productMageId, $productData);
                 if ($updated === false) {
                     $this->_handle_failed_product($product, $cronStartDateTime, "update");
                 }
             }
         }
     }
     SystemSettings::addSettings(SystemSettings::TYPE_PRODUCT_LAST_UPDATED, $cronStartDateTime);
     //Debug::inspect($products); die();
 }