function updateProduct($pro, $fileName, $line) { $clientScript = CatelogConnector::getConnector(B2BConnector::CONNECTOR_TYPE_CATELOG, getWSDL(), 'B2BUser', 'B2BUser'); try { $transStarted = false; try { Dao::beginTransaction(); } catch (Exception $e) { $transStarted = true; } $sku = trim($pro['sku']); $product = Product::getBySku($pro['sku']); $mageId = trim($pro['product_id']); $name = trim($pro['name']); $short_description = trim($pro['short_description']); $description = trim($pro['description']); $weight = trim($pro['weight']); $statusId = trim($pro['status']); $price = trim($pro['price']); $specialPrice = trim($pro['special_price']); $specialPrice_From = trim($pro['special_from_date']) === '' ? trim($pro['special_from_date']) : null; $specialPrice_To = trim($pro['special_to_date']) === '' ? trim($pro['special_to_date']) : null; $supplierName = trim($pro['supplier']); $attributeSet = ProductAttributeSet::get(trim($pro['attributeSetId'])); if (!$product instanceof Product) { $product = Product::create($sku, $name); } $asset = ($assetId = trim($product->getFullDescAssetId())) === '' || !($asset = Asset::getAsset($assetId)) instanceof Asset ? Asset::registerAsset('full_desc_' . $sku, $description, Asset::TYPE_PRODUCT_DEC) : $asset; $product->setName($name)->setMageId($mageId)->setAttributeSet($attributeSet)->setShortDescription($short_description)->setFullDescAssetId(trim($asset->getAssetId()))->setIsFromB2B(true)->setStatus(ProductStatus::get($statusId))->setSellOnWeb(true)->setManufacturer($clientScript->getManufacturerName(trim($pro['manufacturer'])))->save()->clearAllPrice()->addPrice(ProductPriceType::get(ProductPriceType::ID_RRP), $price)->addInfo(ProductInfoType::ID_WEIGHT, $weight); if ($specialPrice !== '') { $product->addPrice(ProductPriceType::get(ProductPriceType::ID_CASUAL_SPECIAL), $specialPrice, $specialPrice_From, $specialPrice_To); } if ($supplierName !== '') { $product->addSupplier(Supplier::create($supplierName, $supplierName, true)); } if (isset($pro['categories']) && count($pro['categories']) > 0) { $product->clearAllCategory(); foreach ($pro['categories'] as $cateMageId) { if (!($category = ProductCategory::getByMageId($cateMageId)) instanceof ProductCategory) { continue; } $product->addCategory($category); } } if ($transStarted === false) { Dao::commitTransaction(); } //TODO remove the file removeLineFromFile($fileName, $line); echo $product->getId() . " => done! \n"; } catch (Exception $ex) { if ($transStarted === false) { Dao::rollbackTransaction(); } throw $ex; } }
/** * (non-PHPdoc) * @see DetailsPageAbstract::saveItem() */ public function saveItem($sender, $param) { $results = $errors = array(); try { Dao::beginTransaction(); $product = !isset($param->CallbackParameter->id) ? new Product() : Product::get(trim($param->CallbackParameter->id)); if (!$product instanceof Product) { throw new Exception('Invalid Product passed in!'); } if (!($manufacturer = Manufacturer::get(trim($param->CallbackParameter->manufacturerId))) instanceof Manufacturer) { throw new Exception('Invalid Manufacturer/Brand!'); } if (!($status = ProductStatus::get(trim($param->CallbackParameter->statusId))) instanceof ProductStatus) { throw new Exception('Invalid Status!'); } $sku = trim($param->CallbackParameter->sku); if (!isset($param->CallbackParameter->id) && ($sku === '' || Product::getBySku($sku) instanceof Product)) { throw new Exception('Invalid SKU (' . $sku . ') passed in OR already exist.'); } $name = trim($param->CallbackParameter->name); $shortDescription = trim($param->CallbackParameter->shortDescription); $sellOnWeb = trim($param->CallbackParameter->sellOnWeb) === '1'; $weight = doubleval(trim($param->CallbackParameter->weight)); $product->setName($name)->setSku($sku)->setShortDescription($shortDescription)->setStatus($status)->setManufacturer($manufacturer)->setSellOnWeb($sellOnWeb)->setAsNewFromDate(trim($param->CallbackParameter->asNewFromDate))->setAsNewToDate(trim($param->CallbackParameter->asNewToDate))->setAssetAccNo(trim($param->CallbackParameter->assetAccNo))->setRevenueAccNo(trim($param->CallbackParameter->revenueAccNo))->setCostAccNo(trim($param->CallbackParameter->costAccNo))->setWeight($weight); if (trim($product->getId()) === '') { $product->setIsFromB2B(false); } $product->save(); $this->_updateFullDescription($product, $param)->_updateCategories($product, $param)->_uploadImages($product, $param)->_setSupplierCodes($product, $param)->_setBarcodes($product, $param)->_setPrices($product, $param)->_setLocation($product, $param); $product->save(); $results['url'] = '/product/' . $product->getId() . '.html'; $results['item'] = $product->getJson(); Dao::commitTransaction(); } catch (Exception $ex) { Dao::rollbackTransaction(); $errors[] = $ex->getMessage(); } $param->ResponseData = StringUtilsAbstract::getJson($results, $errors); }
/** * import all products * * @return CatelogConnector */ public function importProducts() { if (!($systemSetting = SystemSettings::getByType(SystemSettings::TYPE_LAST_NEW_PRODUCT_PULL)) instanceof SystemSettings) { throw new Exception('cannot get LAST_NEW_PRODUCT_PULL in system setting'); } $fromDate = $systemSetting->getValue(); $products = $this->getProductList($fromDate); if (count($products) === 0) { echo 'nothing from magento. exitting' . "\n"; return $this; } try { $transStarted = false; try { Dao::beginTransaction(); } catch (Exception $e) { $transStarted = true; } foreach ($products as $pro) { $mageId = trim($pro->product_id); $sku = trim($pro->sku); $pro = $this->getProductInfo($sku, $this->getInfoAttributes()); $created_at = trim($pro->created_at); $updated_at = trim($pro->updated_at); $product_id = trim($pro->product_id); if (is_null($pro) || !isset($pro->additional_attributes)) { continue; } // handle extra long sku from magento, exceeding mysql sku length limit DaoMap::loadMap('Product'); $skuSizeLimit = DaoMap::$map['product']['sku']['size']; if (strlen($sku) > $skuSizeLimit) { echo 'Product ' . $sku . '(id=' . $product->getId() . ', magento Product Creation Time=' . trim($pro->created_at) . ') magento sku length exceed system sku length limit of' . $skuSizeLimit . ', skipped' . "\n"; continue; } $additionAttrs = $this->_getAttributeFromAdditionAttr($pro->additional_attributes); $name = trim($additionAttrs['name']); $short_description = trim($additionAttrs['short_description']); $description = trim($additionAttrs['description']); $weight = trim($additionAttrs['weight']); $statusId = trim($additionAttrs['status']); $price = trim($additionAttrs['price']); $specialPrice = isset($additionAttrs['special_price']) ? trim($additionAttrs['special_price']) : ''; $specialPrice_From = isset($additionAttrs['special_from_date']) ? trim($additionAttrs['special_from_date']) : null; $specialPrice_To = isset($additionAttrs['special_to_date']) ? trim($additionAttrs['special_to_date']) : null; if (!($product = Product::getBySku($sku)) instanceof Product) { $product = Product::create($sku, $name); Log::logging(0, get_class($this), 'Found New Product from Magento with sku="' . trim($sku) . '" and name="' . $name . '", created_at="' . $created_at, self::LOG_TYPE, '', __FUNCTION__); echo 'Found New Product from Magento with sku="' . trim($sku) . '" and name="' . $name . '", created_at="' . $created_at . ', updated_at' . $updated_at . "\n"; } elseif (Product::getBySku($sku) instanceof Product) { $product = Product::getBySku($sku); echo 'Found Existing Product from Magento with sku="' . trim($sku) . '" and name="' . $name . '", created_at="' . $created_at . ', updated_at' . $updated_at . '"' . "\n"; echo "\t" . 'Name: "' . $name . '"' . "\n"; echo "\t" . 'MageId: "' . $mageId . '"' . "\n"; echo "\t" . 'Short Description: "' . $short_description . '"' . "\n"; echo "\t" . 'Full Description: "' . $description . '"' . "\n"; echo "\t" . 'Status: "' . ProductStatus::get($statusId) . '"' . "\n"; echo "\t" . 'Manufacturer: id=' . $this->getManufacturerName(trim($additionAttrs['manufacturer']))->getId() . ', name="' . $this->getManufacturerName(trim($additionAttrs['manufacturer']))->getName() . '"' . "\n"; echo "\t" . 'Price: "' . $price . '"' . "\n"; echo "\t" . 'Weight: "' . $weight . '"' . "\n"; } $asset = ($assetId = trim($product->getFullDescAssetId())) === '' || !($asset = Asset::getAsset($assetId)) instanceof Asset ? Asset::registerAsset('full_desc_' . $sku, $description, Asset::TYPE_PRODUCT_DEC) : $asset; $product->setName($name)->setMageId($mageId)->setShortDescription($short_description)->setFullDescAssetId(trim($asset->getAssetId()))->setIsFromB2B(true)->setStatus(ProductStatus::get($statusId))->setSellOnWeb(true)->setManufacturer($this->getManufacturerName(trim($additionAttrs['manufacturer'])))->save()->clearAllPrice()->addPrice(ProductPriceType::get(ProductPriceType::ID_RRP), $price)->addInfo(ProductInfoType::ID_WEIGHT, $weight); if ($specialPrice !== '') { $product->addPrice(ProductPriceType::get(ProductPriceType::ID_CASUAL_SPECIAL), $specialPrice, $specialPrice_From, $specialPrice_To); } if (isset($additionAttrs['supplier']) && ($supplierName = trim($additionAttrs['supplier'])) !== '') { $product->addSupplier(Supplier::create($supplierName, $supplierName, true)); } if (isset($pro->categories) && count($pro->categories) > 0) { $product->clearAllCategory(); foreach ($pro->category_ids as $cateMageId) { if (!($category = ProductCategory::getByMageId($cateMageId)) instanceof ProductCategory) { continue; } $product->addCategory($category); } } } $systemSetting->setValue($updated_at)->save(); if ($transStarted === false) { Dao::commitTransaction(); } } catch (Exception $ex) { if ($transStarted === false) { Dao::rollbackTransaction(); } throw $ex; } return $this; }