Example #1
0
 /**
  * @param CategoryMap $categoryMap
  * @throws \Exception
  */
 public function insertWithRelatedCategory(CategoryMap $categoryMap)
 {
     $this->getPdo()->beginTransaction();
     $this->getPdo()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     try {
         $productId = $this->insert();
         $categoryMap->setVirtuemartProductId($productId);
         $filled = [];
         foreach ($categoryMap->toArray() as $column => $value) {
             if ($value !== null) {
                 $filled[$column] = $value;
             }
         }
         $columnString = implode(',', array_keys($filled));
         $valueString = implode(',', array_fill(0, count($filled), '?'));
         $prepareQuery = $this->getPdo()->prepare("INSERT INTO " . CategoryManager::TABLE_NAME . " ({$columnString}) VALUES ({$valueString})");
         if (!$prepareQuery->execute(array_values($filled))) {
             throw new \Exception('Cannot execute query');
         }
         $this->getPdo()->commit();
     } catch (\Exception $ex) {
         $this->getPdo()->rollBack();
         throw $ex;
     }
 }
Example #2
0
 public function parseAttribute($fileName, $attributeName)
 {
     try {
         $reader = new XMLReader();
         $file = file_get_contents($fileName);
         $reader->xml($file);
         $product = new ProductMap();
         $productManager = new ProductManager($product);
         $existProducts = $productManager->selectAll();
         $ids = [];
         foreach ($existProducts as $productItem) {
             $ids[] = $productItem['product_sku'];
         }
         while ($reader->read() && $reader->name !== $attributeName) {
         }
         $i = -1;
         while ($reader->name === $attributeName) {
             ++$i;
             $element = new SimpleXMLElement($reader->readOuterXml());
             if (in_array(strtoupper($element->vendor), ProductManager::$ignoreVendors)) {
                 continue;
             }
             if (!in_array($element->vendorCode, $ids)) {
                 $product->setProductSku($element->vendorCode);
                 $product->setPublished();
                 $category = new CategoryMap();
                 $category->setVirtuemartCategoryId(CategoryManager::NEW_PRODUCT_CATEGORY_ID);
                 $productManager->insertWithRelatedCategory($category);
             } else {
                 $key = array_search($element->vendorCode, $ids);
                 unset($ids[$key]);
             }
             $reader->next($attributeName);
         }
         if (!empty($ids)) {
             foreach ($ids as $id) {
                 $product->setProductSku($id);
                 $product->setPublished();
                 $productManager->update();
             }
         }
     } catch (\Exception $ex) {
         throw $ex;
     }
 }