franmille Posted February 18, 2013 Share Posted February 18, 2013 Buenas, Estoy preparando un Php que me crea un producto y me lo asocia a una determinada categoría, lo que me falta es poder asociarle a ese producto su imagen. Sabéis que código es el que útiliza Prestashop a la hora de subir una imagen a un producto? Un saludo Link to comment Share on other sites More sharing options...
franmille Posted February 18, 2013 Author Share Posted February 18, 2013 Buenas, el código es el siguiente: /** * Add or update a product image * * @param object $product Product object to add image */ public function addProductImage($product, $method = 'auto') { /* Updating an existing product image */ if ($id_image = ((int)(Tools::getValue('id_image')))) { $image = new Image($id_image); if (!Validate::isLoadedObject($image)) $this->_errors[] = Tools::displayError('An error occurred while loading object image.'); else { if (($cover = Tools::getValue('cover')) == 1) Image::deleteCover($product->id); $image->cover = $cover; $this->validateRules('Image'); $this->copyFromPost($image, 'image'); if (sizeof($this->_errors) OR !$image->update()) $this->_errors[] = Tools::displayError('An error occurred while updating image.'); elseif (isset($_FILES['image_product']['tmp_name']) AND $_FILES['image_product']['tmp_name'] != NULL) $this->copyImage($product->id, $image->id, $method); } } /* Adding a new product image */ elseif (isset($_FILES['image_product']['name']) && $_FILES['image_product']['name'] != NULL ) { if ($error = checkImageUploadError($_FILES['image_product'])) $this->_errors[] = $error; if (!sizeof($this->_errors) AND isset($_FILES['image_product']['tmp_name']) AND $_FILES['image_product']['tmp_name'] != NULL) { if (!Validate::isLoadedObject($product)) $this->_errors[] = Tools::displayError('Cannot add image because product add failed.'); elseif (substr($_FILES['image_product']['name'], -4) == '.zip') return $this->uploadImageZip($product); else { $image = new Image(); $image->id_product = (int)($product->id); $_POST['id_product'] = $image->id_product; $image->position = Image::getHighestPosition($product->id) + 1; if (($cover = Tools::getValue('cover')) == 1) Image::deleteCover($product->id); $image->cover = !$cover ? !sizeof($product->getImages(Configuration::get('PS_LANG_DEFAULT'))) : true; $this->validateRules('Image', 'image'); $this->copyFromPost($image, 'image'); if (!sizeof($this->_errors)) { if (!$image->add()) $this->_errors[] = Tools::displayError('Error while creating additional image'); else $this->copyImage($product->id, $image->id, $method); $id_image = $image->id; } } } } if (isset($image) AND Validate::isLoadedObject($image) AND !file_exists(_PS_PROD_IMG_DIR_.$image->getExistingImgPath().'.'.$image->image_format)) $image->delete(); if (sizeof($this->_errors)) return false; @unlink(_PS_TMP_IMG_DIR_.'/product_'.$product->id.'.jpg'); @unlink(_PS_TMP_IMG_DIR_.'/product_mini_'.$product->id.'.jpg'); return ((isset($id_image) AND is_int($id_image) AND $id_image) ? $id_image : true); } Esta en el /admin/tabs/AdminProducts.php Podéis decirme cuales son las variables que necesito para poder subir la imagen desde un php externo a Prestashop (Esta en /raiz/directorio/fichero.php) Muchas gracias Link to comment Share on other sites More sharing options...
Recommended Posts