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; } }
function disableProduct($sku) { $sku = trim($sku); $params = array(); $params['status'] = '2'; // '2' means Disable in magento $script = CatelogConnector::getConnector(B2BConnector::CONNECTOR_TYPE_CATELOG, SystemSettings::getSettings(SystemSettings::TYPE_B2B_SOAP_WSDL), SystemSettings::getSettings(SystemSettings::TYPE_B2B_SOAP_USER), SystemSettings::getSettings(SystemSettings::TYPE_B2B_SOAP_KEY))->updateProductInfo($sku, $params); echo 'product with sku "' . $sku . '" disabled in magento' . "\n"; }
/** * Creating the order item for an order * * @param Order $order * @param stdClass $itemObj * * @return Ambigous <Ambigous, OrderItem, BaseEntityAbstract> */ private function _createItem(Order $order, $itemObj) { $productXml = CatelogConnector::getConnector(B2BConnector::CONNECTOR_TYPE_CATELOG, $this->_getWSDL(), $this->_getApiUser(), $this->_getApiKey())->getProductInfo(trim($itemObj->sku)); $product = Product::create(trim($itemObj->sku), trim($itemObj->name), trim($itemObj->product_id)); if (($updateOptions = trim($itemObj->product_options)) !== '' && is_array($updateOptions = unserialize($updateOptions))) { if (isset($updateOptions['options'])) { $stringArray = array(); foreach ($updateOptions['options'] as $option) { $stringArray[] = '<b>' . trim($option['label']) . '</b>'; $stringArray[] = trim($option['print_value']); $stringArray[] = ''; } $updateOptions = '<br />' . implode('<br />', $stringArray); } else { $updateOptions = ''; } } return OrderItem::create($order, $product, trim($itemObj->price) * 1.1, trim($itemObj->qty_ordered), trim($itemObj->row_total) * 1.1, trim($itemObj->item_id), null, $product->getName() . $updateOptions); }
<?php require_once dirname(__FILE__) . '/../../bootstrap.php'; echo "Begin importProductCategories from magento MELB TIME: " . UDate::now(UDate::TIME_ZONE_MELB) . "\n"; Core::setUser(UserAccount::get(UserAccount::ID_SYSTEM_ACCOUNT)); CatelogConnector::getConnector(B2BConnector::CONNECTOR_TYPE_CATELOG, SystemSettings::getSettings(SystemSettings::TYPE_B2B_SOAP_WSDL), SystemSettings::getSettings(SystemSettings::TYPE_B2B_SOAP_USER), SystemSettings::getSettings(SystemSettings::TYPE_B2B_SOAP_KEY))->importProductCategories(); echo "Done importProductCategories from magento MELB TIME: " . UDate::now(UDate::TIME_ZONE_MELB) . "\n";
private function updateMagentoPrice($price) { $product = Product::getBySku($this->sku); if (!$product instanceof Product) { throw new Exception('Invalid Product passed in. "' . $product . '" given.'); } $connector = CatelogConnector::getConnector(B2BConnector::CONNECTOR_TYPE_CATELOG, SystemSettings::getSettings(SystemSettings::TYPE_B2B_SOAP_WSDL), SystemSettings::getSettings(SystemSettings::TYPE_B2B_SOAP_USER), SystemSettings::getSettings(SystemSettings::TYPE_B2B_SOAP_KEY)); if ($this->debug) { echo 'Connecting to Magento for Product ' . $product->getSku() . '(id=' . $product->getId() . ')' . "\n"; } $result = $connector->updateProductPrice($product->getSku(), $price); if ($result !== true && $this->debug === true) { echo print_r($result, true); } if ($result === true && $this->debug === true) { echo 'Magento Price Successfully updated to $' . $price . "\n"; } return $this; }
<?php ini_set('memory_limit', '1024M'); require_once dirname(__FILE__) . '/../../bootstrap.php'; Core::setUser(UserAccount::get(UserAccount::ID_SYSTEM_ACCOUNT)); echo "Begin downloadProductInfo from magento MELB TIME: " . UDate::now(UDate::TIME_ZONE_MELB) . "\n"; $newOnly = true; $debug = true; $script = CatelogConnector::getConnector(B2BConnector::CONNECTOR_TYPE_CATELOG, SystemSettings::getSettings(SystemSettings::TYPE_B2B_SOAP_WSDL), SystemSettings::getSettings(SystemSettings::TYPE_B2B_SOAP_USER), SystemSettings::getSettings(SystemSettings::TYPE_B2B_SOAP_KEY)); $script->downloadProductInfo($newOnly, $debug); echo "Done downloadProductInfo from magento MELB TIME: " . UDate::now(UDate::TIME_ZONE_MELB) . "\n";
function importNewProduct() { $debug = true; $newOnly = true; $script = CatelogConnector::getConnector(B2BConnector::CONNECTOR_TYPE_CATELOG, SystemSettings::getSettings(SystemSettings::TYPE_B2B_SOAP_WSDL), SystemSettings::getSettings(SystemSettings::TYPE_B2B_SOAP_USER), SystemSettings::getSettings(SystemSettings::TYPE_B2B_SOAP_KEY))->processDownloadedProductInfo($newOnly, $debug); }
public function processDownloadedProductInfo($newOnly = false, $debug = false) { $systemSetting = $newOnly === true ? SystemSettings::getByType(SystemSettings::TYPE_LAST_NEW_PRODUCT_PULL) : SystemSettings::getByType(SystemSettings::TYPE_LAST_PULL_PRODUCT_PULL); if (!$systemSetting instanceof SystemSettings) { throw new Exception('cannot get ' . ($newOnly === true ? 'TYPE_LAST_NEW_PRODUCT_PULL' : 'TYPE_LAST_PULL_PRODUCT_PULL') . ' in system setting'); } $cacheFile = $newOnly === true ? self::MAGE_PULL_NEW_FILE : self::MAGE_PULL_ALL_FILE; $contents = file($cacheFile); // handle extra long sku from magento, exceeding mysql sku length limit DaoMap::loadMap('Product'); $skuSizeLimit = DaoMap::$map['product']['sku']['size']; $totalCount = count($contents); if ($totalCount === 0) { if ($debug === true) { echo 'nothing from downloaded product info file ' . $cacheFile . '. exitting' . "\n"; } return $this; } $rowCount = 0; $sessionRotation = 10; foreach ($contents as $line) { try { if ($rowCount === 0 || $rowCount % $sessionRotation == 0) { $connector = CatelogConnector::getConnector(B2BConnector::CONNECTOR_TYPE_CATELOG, SystemSettings::getSettings(SystemSettings::TYPE_B2B_SOAP_WSDL), SystemSettings::getSettings(SystemSettings::TYPE_B2B_SOAP_USER), SystemSettings::getSettings(SystemSettings::TYPE_B2B_SOAP_KEY), true); } $pro = json_decode($line, true); if (!isset($pro['product_id']) || intval($pro['product_id']) === 0 || !isset($pro['sku']) || trim($pro['sku']) === '') { if ($debug === true) { echo 'invalid product_id from magento, skip current line' . PHP_EOL; } continue; } $mageId = $pro['product_id']; $created_at = $pro['created_at']; $updated_at = $pro['updated_at']; $sku = $pro['sku']; if (strlen($sku) > $skuSizeLimit) { if ($debug === true) { echo '***warnning***Magento product [' . $pro['product_id'] . ']' . $sku . ' created at ' . $created_at . ' updated at ' . $updated_at . ' sku length exceed system sku length limit of' . $skuSizeLimit . ', skipped' . "\n"; } continue; } if ($newOnly === true && ($product = Product::getBySku($sku)) instanceof Product) { if ($debug === true) { echo 'Product[' . $product->getId() . '] ' . $sku . ' already exist' . PHP_EOL; } $connector->removeLineFromFile($cacheFile, $line); continue; } if ($debug === true) { echo $rowCount . '/' . $totalCount . PHP_EOL; echo print_r($line, true) . PHP_EOL; } $attributeSetId = intval($pro['set']); $attributeSet = ProductAttributeSet::getByMageId($attributeSetId); if (!$attributeSet instanceof ProductAttributeSet) { if ($debug === true) { echo 'Magento product [' . $pro['product_id'] . ']' . $sku . ' created at ' . $created_at . ' updated at ' . $updated_at . 'magento attributeSetId ' . $attributeSet . ' cannot find a match in system ProductAttributeSet, skipped' . "\n"; } continue; } if ($debug === true) { echo "\n" . 'mageSetId:' . $attributeSetId . ' => systemSetId:' . $attributeSet->getId() . ', systemSetName:' . $attributeSet->getName() . "\n"; } $name = trim($pro['name']); $short_description = trim($pro['short_description']); if ($name === '') { if ($debug === true) { echo 'Magento product [' . $pro['product_id'] . ']' . $sku . ' created at ' . $created_at . ' updated at ' . $updated_at . 'has empty produ name from magento, I use short description "' . $short_description . '" for product name ' . $attributeSet . ' cannot find a match in system ProductAttributeSet, skipped' . "\n"; } } if ($short_description === '') { $short_description = $name; } $description = trim($pro['description']); if ($description === '') { $description = $short_description; } $weight = doubleval(trim($pro['weight'])); $statusId = trim($pro['status']); $systemStatusId = intval($statusId) === 2 ? ProductStatus::ID_DISABLED : ProductStatus::ID_ENABLED; $price = doubleval(trim($pro['price'])); $specialPrice = isset($pro['special_price']) ? trim($pro['special_price']) : ''; $specialPrice_From = isset($pro['special_from_date']) ? trim($pro['special_from_date']) : null; $specialPrice_To = isset($pro['special_to_date']) ? trim($pro['special_to_date']) : null; $transStarted = false; try { Dao::beginTransaction(); } catch (Exception $e) { $transStarted = true; } if (!($product = Product::getBySku($sku)) instanceof Product) { $product = Product::create($sku, $name); Log::logging(0, get_class($connector), '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) . '" 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($systemStatusId)->getName() . '"' . "\n"; echo "\t" . 'Manufacturer: name="' . $connector->getManufacturerName(trim($pro['manufacturer']))->getName() . '"' . "\n"; echo "\t" . 'Price: "' . $price . '"' . "\n"; echo "\t" . 'Weight: "' . $weight . '"' . "\n"; } $product->setName($name)->setMageId($mageId)->setAttributeSet($attributeSet)->setShortDescription($short_description); $connector->_updateFullDescription($product, $description); $product->setIsFromB2B(true)->setStatus(ProductStatus::get($systemStatusId))->setSellOnWeb(true)->setManufacturer($connector->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 (isset($pro['supplier']) && ($supplierName = trim($pro['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) { if ($debug === true) { echo 'Magento product [' . $pro['product_id'] . ']' . $sku . ' created at ' . $created_at . ' updated at ' . $updated_at . 'magento category id ' . $cateMageId . ' cannot find a match in system ProductCategory, skipped' . "\n"; } continue; } $product->addCategory($category); } } if ($transStarted === false) { Dao::commitTransaction(); $connector->removeLineFromFile($cacheFile, $line); $systemSetting->setValue($newOnly === true ? $created_at : $updated_at)->save(); if ($debug === true) { echo 'system setting ' . trim($systemSetting->getType()) . ' changed to ' . trim($newOnly === true ? $created_at : $updated_at) . PHP_EOL; } $rowCount++; } else { echo "\n" . '***ERROR***' . "transStarted === true, nothing is commited! \n"; } if ($rowCount === 0 || $rowCount % $sessionRotation == 0) { if ($debug === true) { echo 'session => ' . $connector->_session . PHP_EOL; } } } catch (Exception $ex) { if ($transStarted === false) { Dao::rollbackTransaction(); } echo "\n" . '***ERROR***' . $ex->getMessage() . "\n" . $ex->getTraceAsString() . "\n"; } } }