/** * create product * * @param string $sku The sku of product * @param string $name The name of product * @param array $categoryPaths The category paths of product (e.g. $categories = array(array('cate2', 'cate3'), array('cate4', 'cate5', 'cate6')); * @param string $mageProductId //TODO * @param string $isFromB2B * @param string $shortDescr * @param string $fullDescr * @param string $brandName * * @throws Exception * @return string * @soapmethod */ public function createProduct($sku, $name, $categoryPaths = array(), $mageProductId = '', $isFromB2B = false, $shortDescr = '', $fullDescr = '', $brandName = '') { $response = $this->_getResponse(UDate::now()); try { Dao::beginTransaction(); Core::setUser(UserAccount::get(UserAccount::ID_SYSTEM_ACCOUNT)); //TODO if (Product::getBySku(trim($sku)) instanceof Product) { throw new Exception('sku "' . $sku . '" already exists'); } $categories = array(); if (is_array($categoryPaths)) { foreach ($categoryPaths as $categoryPath) { $parentCategory = null; foreach ($categoryPath as $categoryName) { if (count($i = ProductCategory::getAllByCriteria('name = ?', array(trim($categoryName)), true, 1, 1)) > 0) { $categories[$i[0]->getId()] = $category = $i[0]; } else { $category = ProductCategory::create(trim($categoryName), trim($categoryName), $parentCategory); $categories[$category->getId()] = $category; } $parentCategory = $category; } } } // create product $product = Product::create(trim($sku), trim($name)); // TODO foreach ($categories as $category) { $product->addCategory($category); } $response['status'] = self::RESULT_CODE_SUCC; $this->addCData('product', json_encode($product->getJson()), $response); Dao::commitTransaction(); } catch (Exception $e) { Dao::rollbackTransaction(); $response['status'] = self::RESULT_CODE_FAIL; $this->addCData('error', $e->getMessage(), $response); } return trim($response->asXML()); }
/** * Get all children for a Product Category, including grand children, grand grand children etc * * @return array ProductCategory */ public function getAllChildrenIds(&$result = null, $starting = 0) { if ($starting === 0) { $starting = $this->getId(); $result = array(); } else { $result[] = $this->getId(); } $children = ProductCategory::getAllByCriteria('parentId = ?', array($this->getId())); foreach ($children as $child) { $child->getAllChildrenIds($result, $starting); } return array_unique($result); }
/** * create/update product via datafeed. * * @param array $params * * @return array */ private function _dataFeedImport($params) { try { Dao::beginTransaction(); $this->_runner->log('dataFeedImport: ', __CLASS__ . '::' . __FUNCTION__); $sku = $this->_getPram($params, 'sku', null, true); $name = $this->_getPram($params, 'name', null, true); $shortDesc = $this->_getPram($params, 'short_description', $name); $fullDesc = $this->_getPram($params, 'description', ''); $price = StringUtilsAbstract::getValueFromCurrency($this->_getPram($params, 'price', null, true)); $supplierName = $this->_getPram($params, 'supplier', null, true); $supplierCode = $this->_getPram($params, 'supplier_code', null, true); $supplier = $this->_getEntityByName($supplierName, 'Supplier'); if (!$supplier instanceof Supplier) { throw new Exception("invalid supplier:" . $supplierName); } $manufacturerId = $this->_getPram($params, 'manufacturer_id', null, true); $manufacturer = Manufacturer::get($manufacturerId); if (!$manufacturer instanceof Manufacturer) { throw new Exception("invalid Manufacturer:" . $manufacturerId); } $statusName = $this->_getPram($params, 'availability', null, true); $status = $this->_getEntityByName($statusName, 'ProductStatus'); if (!$status instanceof ProductStatus) { throw new Exception("invalid ProductStatus:" . $statusName); } $assetAccNo = $this->_getPram($params, 'assetAccNo', null); $revenueAccNo = $this->_getPram($params, 'revenueAccNo', null); $costAccNo = $this->_getPram($params, 'costAccNo', null); $categoryIds = $this->_getPram($params, 'category_ids', array()); $canSupplyQty = $this->_getPram($params, 'qty', 0); $weight = $this->_getPram($params, 'weight', 0); $images = $this->_getPram($params, 'images', array()); $showOnWeb = $this->_getPram($params, 'showonweb', true); $attributesetId = $this->_getPram($params, 'attributesetId', null); $canUpdate = false; //if we have this product already, then skip if (!($product = Product::getBySku($sku)) instanceof Product) { $this->_runner->log('new SKU(' . $sku . ') for import, creating ...', '', APIService::TAB); $product = Product::create($sku, $name, '', null, null, false, $shortDesc, $fullDesc, $manufacturer, $assetAccNo, $revenueAccNo, $costAccNo, null, null, true, $weight, $attributesetId); $this->log_product("NEW", "=== new === sku={$sku}, name={$name}, shortDesc={$shortDesc}, fullDesc={$fullDesc}, category=" . implode(', ', $categoryIds), '', APIService::TAB); $canUpdate = true; } else { //$this->log_product("UPDATE", "=== update === sku=$sku, name=$name, shortDesc=$shortDesc, fullDesc=$fullDesc, category=" . implode(', ', $categoryIds), '', APIService::TAB); //if there is no price matching rule for this product if (($rulesCount = intval(ProductPriceMatchRule::countByCriteria('active = 1 and productId = ?', array($product->getId())))) === 0) { $this->_runner->log('Found SKU(' . $sku . '): ', '', APIService::TAB); $fullAsset = Asset::getAsset($product->getFullDescAssetId()); $this->_runner->log('Finding asset for full description, assetId:' . ($fullAsset instanceof Asset ? $fullAsset->getAssetId() : ''), '', APIService::TAB . APIService::TAB); $fullAssetContent = ''; if ($fullAsset instanceof Asset) { $fullAssetContent = file_get_contents($fullAsset->getPath()); $this->_runner->log('Got full asset content before html_decode: <' . $fullAssetContent . '>', '', APIService::TAB . APIService::TAB); $fullAssetContent = trim(str_replace(' ', '', $fullAssetContent)); $this->_runner->log('Got full asset content after html_code: <' . $fullAssetContent . '>', '', APIService::TAB . APIService::TAB); } if ($fullAssetContent === '') { $this->_runner->log('GOT BLANK FULL DESD. Updating full description.', '', APIService::TAB . APIService::TAB . APIService::TAB); if ($fullAsset instanceof Asset) { Asset::removeAssets(array($fullAsset->getAssetId())); $this->_runner->log('REMOVED old empty asset for full description', '', APIService::TAB . APIService::TAB . APIService::TAB); } $fullAsset = Asset::registerAsset('full_description_for_product.txt', $fullDesc, Asset::TYPE_PRODUCT_DEC); $product->setFullDescAssetId($fullAsset->getAssetId())->save(); $this->_runner->log('Added a new full description with assetId: ' . $fullAsset->getAssetId(), '', APIService::TAB . APIService::TAB); $canUpdate = true; $this->log_product("UPDATE", "=== updating === sku={$sku} Found ", '', APIService::TAB); } else { $this->log_product("SKIP", "=== SKIP updating === sku={$sku} for full description not null", '', APIService::TAB); } } else { $this->_runner->log('SKIP updating. Found ProductPriceMatchRule count:' . $rulesCount, '', APIService::TAB); $this->log_product("SKIP", "=== SKIP updating === sku={$sku} Found ProductPriceMatchRule count:{$rulesCount}", '', APIService::TAB); } } $json = $product->getJson(); //only update categories and status when there is no pricematching rule or created new if ($canUpdate === true) { //short description, name, manufacturer $this->_runner->log('Updating the price to: ' . StringUtilsAbstract::getCurrency($price), '', APIService::TAB . APIService::TAB); $product->setShortDescription($shortDesc)->setName($name)->setManufacturer($manufacturer)->setWeight($weight)->setSellOnWeb($showOnWeb)->clearAllPrice()->addPrice(ProductPriceType::get(ProductPriceType::ID_RRP), $price); //show on web if (is_array($categoryIds) && count($categoryIds) > 0) { $this->_runner->log('Updating the categories: ' . implode(', ', $categoryIds), '', APIService::TAB . APIService::TAB); foreach ($categoryIds as $categoryId) { if (!($category = ProductCategory::get($categoryId)) instanceof ProductCategory) { continue; } if (count($ids = explode(ProductCategory::POSITION_SEPARATOR, trim($category->getPosition()))) > 0) { foreach (ProductCategory::getAllByCriteria('id in (' . implode(',', $ids) . ')') as $cate) { $product->addCategory($cate); $this->_runner->log('Updated Category ID: ' . $cate->getId(), '', APIService::TAB . APIService::TAB . APIService::TAB); } } } } //updating the images if (is_array($images) && count($images) > 0) { $this->_runner->log('Processing ' . count($images) . ' image(s) ...', '', APIService::TAB . APIService::TAB); $exisitingImgsKeys = array(); $this->_runner->log('Checking exsiting images...', '', APIService::TAB . APIService::TAB . APIService::TAB); $exisitingImgs = $product->getImages(); $this->_runner->log('Got ' . count($exisitingImgs) . ' exisiting image(s), keys: ', '', APIService::TAB . APIService::TAB . APIService::TAB . APIService::TAB); foreach ($exisitingImgs as $image) { if (($asset = Asset::getAsset($image->getImageAssetId())) instanceof Asset) { $imgKey = md5($asset->read()); $exisitingImgsKeys[] = $imgKey; $this->_runner->log($imgKey, '', APIService::TAB . APIService::TAB . APIService::TAB . APIService::TAB . APIService::TAB); } } $this->_runner->log('Checking ' . count($images) . ' new image(s) ...', '', APIService::TAB . APIService::TAB); foreach ($images as $image) { //if haven't got any content at all if (!isset($image['content'])) { $this->_runner->log('No Content, SKIP!', '', APIService::TAB . APIService::TAB . APIService::TAB); continue; } $newImageContent = base64_decode($image['content']); $newImgKey = md5($newImageContent); //if we've got the image already if (in_array($newImgKey, $exisitingImgsKeys)) { $this->_runner->log('Same Image Exists[' . $newImgKey . '], SKIP!', '', APIService::TAB . APIService::TAB . APIService::TAB); continue; } $asset = Asset::registerAsset($image['name'], $newImageContent, Asset::TYPE_PRODUCT_IMG); $this->_runner->log('Registered a new Asset [AssetID=' . $asset->getAssetId() . '].', '', APIService::TAB . APIService::TAB . APIService::TAB); $product->addImage($asset); $this->_runner->log('Added to product(SKU=' . $product->getSku() . ')', '', APIService::TAB . APIService::TAB . APIService::TAB); } } $product->setStatus($status); $this->_runner->log('Updated Status to: ' . $status->getName(), '', APIService::TAB . APIService::TAB); $product->addSupplier($supplier, $supplierCode, $canSupplyQty); $this->_runner->log('Updated Supplier(ID' . $supplier->getId() . ', name=' . $supplier->getName() . ') with code: ' . $supplierCode . 'canSupplyQty=' . $canSupplyQty, '', APIService::TAB . APIService::TAB); $json = $product->save()->getJson(); $this->_runner->log('Saved Product ID: ' . $product->getId(), '', APIService::TAB . APIService::TAB); } Dao::commitTransaction(); return $json; } catch (Exception $e) { Dao::rollbackTransaction(); throw $e; } }
/** * Getting the json for a product category * * @param ProductCategory $category * * @return multitype:multitype:NULL */ private function _getCategoryJson(ProductCategory $category) { $categoryJson = $category->getJson(); $children = array(); $categories = ProductCategory::getAllByCriteria('parentId = ?', array($category->getId())); foreach ($categories as $cate) { $children[] = $this->_getCategoryJson($cate); } $categoryJson['children'] = $children; return $categoryJson; }