public static function generateProducts() { //read in xml document(products) $doc = simplexml_load_file(APPLICATION_PATH . '/../products.xml'); foreach ($doc->category as $cat) { $categoryObj = new Category(); $categoryObj->setUrlKey($cat->urlkey); $categoryObj->setName($cat->name); $categoryObj->save(); } foreach ($doc->product as $product) { $productObj = new Product(); $productObj->setName($product->name); $productObj->setPrice($product->price); $productObj->setSku($product->sku); $productObj->setDateCreated(date('d-m-Y H:i:s', time())); $productObj->setUrlKey($product->urlkey); $productObj->setProductImage($product->image); $productObj->setCategoryId($product->category); $productObj->save(); } }
/** * 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(); }