Example #1
0
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;
    }
}
Example #2
0
 /**
  * Importing the attributes from magento
  *
  * @return void|CatelogConnector
  */
 public function importProductAttributes()
 {
     $productAttributeSetIds = Dao::getResultsNative('select distinct pro_att_set.id from productattributeset pro_att_set where pro_att_set.isFromB2B = 1 and mageId <> 0', array(), PDO::FETCH_ASSOC);
     if (count($productAttributeSetIds) === 0) {
         return;
     }
     try {
         $transStarted = false;
         try {
             Dao::beginTransaction();
         } catch (Exception $e) {
             $transStarted = true;
         }
         foreach ($productAttributeSetIds as $productAttributeSetId) {
             $productAttributeSetId = $productAttributeSetId['id'];
             $productAttributeSet = ProductAttributeSet::get($productAttributeSetId);
             if (!$productAttributeSet instanceof ProductAttributeSet) {
                 continue;
             }
             $productAttributes = $this->getProductAttributeList($productAttributeSet->getMageId());
             if (count($productAttributes) === 0) {
                 continue;
             }
             foreach ($productAttributes as $productAttribute) {
                 $mageId = trim($productAttribute->attribute_id);
                 $code = isset($productAttribute->code) ? trim($productAttribute->code) : '';
                 $type = isset($productAttribute->type) ? trim($productAttribute->type) : '';
                 if (!isset($productAttribute->required)) {
                     $required = false;
                 } else {
                     $required = trim($productAttribute->required) === '1' || $required === true || trim($productAttribute->required) === 'true' ? true : false;
                 }
                 $scope = isset($productAttribute->scope) ? trim($productAttribute->scope) : '';
                 $description = isset($productAttribute->description) ? trim($productAttribute->description) : $code;
                 Log::logging(0, get_class($this), 'getting productAttribute from magento (mageId="' . $mageId . '")', self::LOG_TYPE, '', __FUNCTION__);
                 $productAttribute = ProductAttribute::getByMageId($mageId);
                 if (!$productAttribute instanceof ProductAttribute) {
                     Log::logging(0, get_class($this), 'found new ProductAttribute from magento(mageId="' . $mageId . '", mageAttributeSetId="' . $productAttributeSet->getMageId() . '", code="' . $code . '", type="' . $type . '", required="' . $required . '", scope="' . $scope . '")', self::LOG_TYPE, '', __FUNCTION__);
                     echo 'found new ProductAttribute from magento(mageId="' . $mageId . '", mageAttributeSetId="' . $productAttributeSet->getMageId() . '", code="' . $code . '", type="' . $type . '", required="' . $required . '", scope="' . $scope . '")' . "\n";
                     $productAttribute = ProductAttribute::create($code, $type, $required, $scope, $description, true, $mageId, $productAttributeSet->getMageId());
                 } else {
                     Log::logging(0, get_class($this), 'found existing ProductAttribute from magento(mageId="' . $mageId . '", mageAttributeSetId="' . $productAttributeSet->getMageId() . '", code="' . $code . '", ID=' . $productAttributeSet->getId() . ', type="' . $type . '", required="' . $required . '", scope="' . $scope . '")', self::LOG_TYPE, '', __FUNCTION__);
                     echo 'found existing ProductAttribute from magento(mageId="' . $mageId . '", mageAttributeSetId="' . $productAttributeSet->getMageId() . '", code="' . $code . '", ID=' . $productAttributeSet->getId() . ', type="' . $type . '", required="' . $required . '", scope="' . $scope . '")' . "\n";
                     $productAttribute->setCode($code)->setType($type)->setRequired($required)->setScope($scope)->setDescription($description)->setIsFromB2B(true)->setAttributeSetMageId($productAttributeSet->getId())->setActive(true)->save();
                 }
             }
         }
         if ($transStarted === false) {
             Dao::commitTransaction();
         }
     } catch (Exception $e) {
         if ($transStarted === false) {
             Dao::commitTransaction();
         }
         throw $e;
     }
     return $this;
 }
Example #3
0
 /**
  * Creating the product based on sku
  *
  * @param string $sku           	The sku of the product
  * @param string $name          	The name of the product
  * @param string $mageProductId 	The magento id of the product
  * @param int    $stockOnHand   	The total quantity on hand for this product
  * @param int    $stockOnOrder  	The total quantity on order from supplier for this product
  * @param int    $stockMinLevel 	The minimum stock level for this product
  * @param int    $stockReorderLevel	The reorder stock level for this product
  * @param bool   $isFromB2B     	Whether this product is created via B2B?
  * @param string $shortDescr    	The short description of the product
  * @param string $fullDescr     	The assetId of the full description asset of the product
  *
  * @return Ambigous <Product, Ambigous, NULL, BaseEntityAbstract>
  */
 public static function create($sku, $name, $mageProductId = '', $stockOnHand = null, $stockOnOrder = null, $isFromB2B = false, $shortDescr = '', $fullDescr = '', Manufacturer $manufacturer = null, $assetAccNo = null, $revenueAccNo = null, $costAccNo = null, $stockMinLevel = null, $stockReorderLevel = null, $manualDatafeed = false, $weight = null, $attributesetId = null)
 {
     if (!($product = self::getBySku($sku)) instanceof Product) {
         $product = new Product();
     }
     $product->setSku(trim($sku))->setName($name)->setManualDatafeed(intval($manualDatafeed) === 1);
     if (($mageProductId = trim($mageProductId)) !== "") {
         $product->setMageId($mageProductId);
     }
     if (trim($product->getId()) === '') {
         $product->setIsFromB2B($isFromB2B)->setShortDescription($shortDescr);
         if ($stockOnOrder !== null && is_numeric($stockOnOrder)) {
             $product->setStockOnOrder(intval($stockOnOrder));
         }
         if ($stockOnHand !== null && is_numeric($stockOnHand)) {
             $product->setStockOnHand(intval($stockOnHand));
         }
         if ($stockMinLevel !== null && is_numeric($stockMinLevel)) {
             $product->setStockMinLevel(intval($stockMinLevel));
         }
         if ($stockReorderLevel !== null && is_numeric($stockReorderLevel)) {
             $product->setStockReorderLevel(intval($stockReorderLevel));
         }
         if ($assetAccNo !== null && is_string($assetAccNo)) {
             $product->setAssetAccNo(trim($assetAccNo));
         }
         if ($revenueAccNo !== null && is_string($revenueAccNo)) {
             $product->setRevenueAccNo(trim($revenueAccNo));
         }
         if ($costAccNo !== null && is_string($costAccNo)) {
             $product->setCostAccNo(trim($costAccNo));
         }
         if ((${$fullDescr} = trim($fullDescr)) !== '') {
             $asset = Asset::registerAsset('full_desc_' . $sku, $fullDescr, Asset::TYPE_PRODUCT_DEC);
             $product->setFullDescAssetId(trim($asset->getAssetId()));
         }
         if ($manufacturer instanceof Manufacturer) {
             $product->setManufacturer($manufacturer);
         }
         if ($weight !== null) {
             $product->setWeight($weight);
         }
         if ($attributesetId !== null && is_string($attributesetId)) {
             $product->setAttributeSet(ProductAttributeSet::get($attributesetId));
         }
     }
     return $product->save();
 }