public function importProducts(Import $entity, $output) { $sql = ' SELECT p.*, o.nombre as opticName, m.nombre as brand, mo.nombre as model, s.nombre as category FROM `producto` AS p ' . ' LEFT JOIN optica AS o ON o.id = p.id_optica ' . ' LEFT JOIN servicio AS s ON s.id = p.id_servicio ' . ' LEFT JOIN marca AS m ON m.id_marca = p.id_marca ' . ' LEFT JOIN modelo AS mo ON mo.id_modelo = p.id_modelo ' . ' ORDER BY p.id ' . ' LIMIT ' . $entity->getLimitStart() . ', ' . $entity->getLimitEnd(); $link = mysqli_connect($entity->getServer(), $entity->getUsername(), $entity->getPassword(), $entity->getDbname()) or die('No se pudo conectar: ' . mysqli_error($link)); $resultado = $link->query($sql); $x = 0; if (mysqli_num_rows($resultado) > 0) { while ($fila = mysqli_fetch_assoc($resultado)) { $x++; print_r($x . '-' . utf8_encode($fila['nombre'])); echo PHP_EOL; $em = $this->container->get('doctrine')->getManager(); $optic = $em->getRepository('CoreBundle:Optic')->findOneByName(utf8_encode($fila['opticName'])); $category = $em->getRepository('EcommerceBundle:Category')->findOneByName(utf8_encode($fila['category'])); $brand = $em->getRepository('EcommerceBundle:Brand')->findOneByName(utf8_encode($fila['brand'])); $model = $em->getRepository('EcommerceBundle:BrandModel')->findOneByName(utf8_encode($fila['model'])); //Create Products if ($optic instanceof Optic && $fila['id'] != 746 && $fila['id'] != 747 && $fila['id'] != 748 && $fila['id'] != 749 && $fila['id'] != 750 && $fila['id'] != 751 && $fila['id'] != 752 && $fila['id'] != 753) { $product = new Product(); $product->setName(utf8_encode($fila['nombre'])); $product->setDescription(utf8_encode($fila['descripcion'])); if ($fila['precio_original'] != '') { $product->setInitPrice($fila['precio_original']); } else { $product->setInitPrice(0); } $product->setPrice($fila['precio']); if ($fila['tipo_precio'] != '') { $product->setPriceType($fila['tipo_precio']); } else { $product->setPriceType(0); } $product->setDiscount($fila['descuento']); $product->setDiscountType($fila['tipo_descuento']); if ($fila['stock'] != '') { $product->setStock($fila['stock']); } else { $product->setStock(0); } $product->setWeight($fila['kilos']); $product->setOutlet($fila['outlet']); $product->setPublic($fila['publicado']); if ($fila['recoger_tienda'] != '') { $product->setStorePickup($fila['recoger_tienda']); } else { $product->setStorePickup(0); } $product->setMetaTitle(utf8_encode($fila['meta_titulo'])); $product->setMetaDescription(utf8_encode($fila['meta_descripcion'])); $product->setMetaTags(utf8_encode($fila['meta_keywords'])); $product->setOptic($optic); $product->setBrand($brand); $product->setModel($model); $product->setCategory($category); $product->setActive(true); $product->setAvailable(true); $product->setHighlighted(false); $product->setFreeTransport(false); $slug = $this->slugify(utf8_encode($fila['nombre'] . '-' . $optic->getCity())); // $p = $em->getRepository('EcommerceBundle:Product')->findOneBySlug($slug); // if($p instanceof Product) $slug = $slug.'_'.rand(1000,9999); $product->setSlug($slug); $em->persist($product); $em->flush(); //image if ($fila['imagen'] != '') { $imagePath = ''; $dir = '/home/sebastian/www/src/Symfony/web/uploads/documents'; $absoluteImagePath = $dir . '/' . utf8_encode($fila['imagen']); if (file_exists($absoluteImagePath . '.jpeg')) { $imagePath = utf8_encode($fila['imagen']) . '.jpeg'; } if (file_exists($absoluteImagePath . '.jpg')) { $imagePath = utf8_encode($fila['imagen']) . '.jpg'; } if (file_exists($absoluteImagePath . '.gif')) { $imagePath = utf8_encode($fila['imagen']) . '.gif'; } if (file_exists($absoluteImagePath . '.png')) { $imagePath = utf8_encode($fila['imagen']) . '.png'; } //Thumb $imagePathThumb = ''; $absoluteImagePathThumb = $dir . '/' . utf8_encode($fila['imagen']) . '_260.jpg'; if (file_exists($absoluteImagePathThumb)) { $imagePathThumb = utf8_encode($fila['imagen']) . '_260.jpg'; } $imagePathThumb2 = ''; $absoluteImagePathThumb2 = $dir . '/' . utf8_encode($fila['imagen']) . '_142.jpg'; if (file_exists($absoluteImagePathThumb2)) { $imagePathThumb2 = utf8_encode($fila['imagen']) . '_142.jpg'; } if ($imagePath == '') { $dir = 'http://web.com/uploads/documents/'; $imagePath = $fila['imagen'] . '.' . $fila['extension_img']; } print_r($dir . '/' . $imagePath); echo PHP_EOL; @mkdir(__DIR__ . '/../../../../../web/uploads/images/product'); @mkdir(__DIR__ . '/../../../../../web/uploads/images/product/' . $product->getId()); @mkdir(__DIR__ . '/../../../../../web/uploads/images/product/' . $product->getId() . '/thumbnail'); copy($dir . '/' . $imagePath, __DIR__ . '/../../../../../web/uploads/images/product/' . $product->getId() . '/' . $imagePath); if ($imagePathThumb != '') { copy($dir . '/' . $imagePathThumb, __DIR__ . '/../../../../../web/uploads/images/product/' . $product->getId() . '/thumbnail/' . $imagePathThumb); } if ($imagePathThumb2 != '') { copy($dir . '/' . $imagePathThumb2, __DIR__ . '/../../../../../web/uploads/images/product/' . $product->getId() . '/thumbnail/' . $imagePathThumb2); } $image0 = new Image(); $image0->setPath($imagePath); $product->addImage($image0); $em->persist($image0); } $em->flush(); } } } }
/** * 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(); }