Jump to content

Crear un producto en PHP y añadirlo en una categoría


tgonzalez

Recommended Posts

Hola veréis intento añadir una serie de productos en la categoría "inicio", pero no me deja el código, hay que ponerlo de forma manual:

$unidadesPriCol=100;//$c[3];
                        //    echo "dividir es ".$c[3];

                        //echo "aqui llego";


                        @include 'config/config.inc.php';
                        /*include 'classes\shop\Shop.php';
                        include 'classes\shop\ShopGroup.php';
                        include 'classes\shop\ShopUrl.php';*/
                        @include 'init.php';
                        //limpio la caché
                        Tools::clearSmartyCache();
                        Tools::clearXMLCache();
                        Media::clearCache();
                        Tools::generateIndex();
                        //    echo "aqui llego";

                        $default_lang = Configuration::get('PS_LANG_DEFAULT');

                        //echo "def language";
                        ///CALCULO DE UNIDADES
                        /*$unidadesVenta=$d[9];
                        $dividir=$d[8];

                        $precio1=($dividir/100)/$unidadesVenta;*/
                        //echo "<br>$unidadesVenta unidad, Precio totales $dividir<br> unidades primera columna $unidadesPriCol<br> Precio $precio1";
                        ///$id_product,false,$lang_id
                    $product->price = getPriceCsv($refgen);
                    //if($r){
                        $product = new Product();
                        $insImg=2;
                        if($existe){
                            $product = new Product(intval($existe));
                            $insImg=1;
                        }
                        $nameIntermedio=eliminar_simbolos($d[2]);
                        if(strlen($nameIntermedio)<7){
                            throw new Exception("");
                        }
                        $product->name = [$default_lang => substr($nameIntermedio,0 , 127)];
                        $product->link_rewrite = Tools::str2url(substr(eliminar_simbolos($d[3]),0 , 120))."".rand(300, 20000);
                        $product->price = getPriceCsv($refgen);
                        $product->quantity = 70;
                            $product->active = 1;
                        $product->reference = $id;
                        $product->visibility = "both";
                        $product->show_price = 1;
                        $product->on_sale = 1;
                        $product->minimal_quantity = 1;
                        $depth= $d[11];
                    if(strcmp($d[10]."","")!=0){
                        $product->width = floatval ($d[10])/100;
                    }
                    if(strcmp($d[9]."","")!=0){
                        $product->height = floatval ($d[9])/100;
                    }
                    if(strcmp($d[11]."","")!=0){
                        $product->depth = floatval ($d[11])/100;

                    }
                    if(strcmp($d[7]."","")!=0){
                        $product->weight =floatval ($d[7])/100;
                    }
                    if(strcmp($d[6]."","")!=0){
                        $product ->meta_keywords =$d[6];
                    }

                        $product->available_for_order = 1; //true
                        $product->description=eliminar_simbolos($d[3]);
                        $product->description_short=eliminar_simbolos($d[2]);
    
                        //$product->id_category = 2;//[3,4]
                        
//$product->addToCategories(array(2));
   // $product->id_category_default = 2;
                        StockAvailable::setQuantity((int)$product->id, 0, $product->quantity, Context::getContext()->shop->id);

 

Un saludo 

 

sp_.png

Link to comment
Share on other sites

16 hours ago, gusman126 said:

$id_cat = 2;
            $product->addToCategories(array($id_cat));
            $product->id_category_default = (int)$id_cat;
            $product->addToCategories(array($id_cat));

Muchas gracias, ahora si lo añade a inicio, pero no aparece de forma instantanea en la tienda

Link to comment
Share on other sites

hace 30 minutos, tgonzalez dijo:

Muchas gracias, ahora si lo añade a inicio, pero no aparece de forma instantanea en la tienda

?¿ que quieres decir? 

Si quieres que aparezca en home, debes elegir el modulo correspondiente e indicar que se muestre la categoria de inicio.

 

  • Like 1
Link to comment
Share on other sites

15 minutes ago, gusman126 said:

?¿ que quieres decir? 

Si quieres que aparezca en home, debes elegir el modulo correspondiente e indicar que se muestre la categoria de inicio.

 

Hola gusman, quiero decir que los productos que añado, me aparecen en el backoffice, pero en el frontoffice, no me aparecen añadidos ¿de qué puede ser?

Un saludo

Link to comment
Share on other sites

hace 18 minutos, tgonzalez dijo:

Hola gusman, quiero decir que los productos que añado, me aparecen en el backoffice, pero en el frontoffice, no me aparecen añadidos ¿de qué puede ser?

Un saludo

Prueba a ver "previo" si te da un error de no tienes permiso, es porque no le has asignado correctamente la categoria por defecto.

Añade el codigo al final de todo, asigna la categoría antes y luego la predeterminada

 

  • Like 1
Link to comment
Share on other sites

Vale la solución era poner 

$product->quantity =(float) 70;

Ahora la duda que tengo, después de hacerlo todo correcto es, ¿Porqué una vez añadido el producto no me aparece en mi página shop? Aquí dejo mi código:

 

try{
                    //echo "<br><br>$refgen<br>";
if(getPriceCsv($refgen)){
                

                        $existe=checkExiste($refgen);


                    //        echo "No existe";
                        //echo $id."<br>";
                    //    $precioreseller=$d[29];
                    //    $codigoFabricante=$d[30];

                    //    $description=eliminar_simbolos($d[2]);
                        //$tipoDeUnidad=$d[2];

                        //    echo "Descripcion es $description";
                        $unidadesPriCol=100;//$c[3];
                        //    echo "dividir es ".$c[3];

                        //echo "aqui llego";


                        @include 'config/config.inc.php';
                        /*include 'classes\shop\Shop.php';
                        include 'classes\shop\ShopGroup.php';
                        include 'classes\shop\ShopUrl.php';*/
                        @include 'init.php';
                        //limpio la caché
                        Tools::clearSmartyCache();
                        Tools::clearXMLCache();
                        Media::clearCache();
                        Tools::generateIndex();
                        //    echo "aqui llego";

                        $default_lang = Configuration::get('PS_LANG_DEFAULT');

                        //echo "def language";
                        ///CALCULO DE UNIDADES
                        /*$unidadesVenta=$d[9];
                        $dividir=$d[8];

                        $precio1=($dividir/100)/$unidadesVenta;*/
                        //echo "<br>$unidadesVenta unidad, Precio totales $dividir<br> unidades primera columna $unidadesPriCol<br> Precio $precio1";
                        ///$id_product,false,$lang_id
                    $product->price = getPriceCsv($refgen);
                    //if($r){
                        $product = new Product();
                        $insImg=2;
                        if($existe){
                            $product = new Product(intval($existe));
                            $insImg=1;
                        }
                        $nameIntermedio=eliminar_simbolos($d[2]);
                        if(strlen($nameIntermedio)<7){
                            throw new Exception("");
                        }
                        $product->name = [$default_lang => substr($nameIntermedio,0 , 127)];
                        $product->link_rewrite = Tools::str2url(substr(eliminar_simbolos($d[3]),0 , 120))."".rand(300, 20000);
                        $product->price = getPriceCsv($refgen);
    $cantidad ="5.0";
                        $product->quantity = (float)$cantidad;
                            $product->active = 1;
    //$product->redirect_type = '404';
                        $product->reference = $id;
                        $product->visibility = "both";
                        $product->show_price = 1;
                        $product->on_sale = 1;
                        $product->minimal_quantity = 1;
                        $depth= $d[11];
                    if(strcmp($d[10]."","")!=0){
                        $product->width = floatval ($d[10])/100;
                    }
                    if(strcmp($d[9]."","")!=0){
                        $product->height = floatval ($d[9])/100;
                    }
                    if(strcmp($d[11]."","")!=0){
                        $product->depth = floatval ($d[11])/100;

                    }
                    if(strcmp($d[7]."","")!=0){
                        $product->weight =floatval ($d[7])/100;
                    }
                    if(strcmp($d[6]."","")!=0){
                        $product ->meta_keywords =$d[6];
                    }

                        $product->available_for_order = 1; //true
                        $product->description=eliminar_simbolos($d[3]);
                        $product->description_short=eliminar_simbolos($d[2]);
    
    
                        $id_cat = 2;
            $product->addToCategories(array($id_cat));
            $product->id_category_default = (int)$id_cat;
            $product->addToCategories(array($id_cat));
    //StockAvailable::setQuantity((int)$product->id, 0, $product->quantity, Context::getContext()->shop->id);
    //StockAvailable::setQuantity((int)$product->id, 0, (float)$cantidad)
                        StockAvailable::setQuantity((int)$product->id, 0, (float)$cantidad, Context::getContext()->shop->id);
                        $product->save();
    StockAvailable::setQuantity((int)$product->id, 0, (float)$cantidad, Context::getContext()->shop->id);
                        //if($insImg==1){
                            //$imagen1=$d[14];
                            //echo "<br>la imagen es".$imagen1." <br>!"; 
                            //$img = file_get_contents($d[14]);
                            //file_put_contents('imagen.jpg', $img);
                            $urlImg=str_replace(array("http:/"), array("http://"), $d[14]);
                            file_put_contents('imagen.jpg', file_get_contents($urlImg));
                            
    
                         
    
                        
                            //if(!isset(Product::getCover($product))){
                            $image = new Image();
                            $image->id_product = (int) $product->id;
                            $image->position = Image::getHighestPosition($product->id) + 1;
                            $image->cover =  true;
                            $image->add();
                            if (!copyImg($product->id, $image->id, 'imagen.jpg', 'products', !Tools::getValue('regenerate'))) {
                                $image->delete();
                            }
                                
                        //}
                        @unlink("imagen.jpg");
    
                    

                        echo "salvado<br>";
                        $precioInicial=$precio1;
                        $anterior=1;
                        //echo "roint 1";
                        /*
                        if(strcmp($d[10],"")!=0&&strcmp($d[11],"")!=0){

                            precioEspecial($product->id,$d[10],$product->price,$d[11]);


                        }

                        if(strcmp($d[12],"")!=0&&strcmp($d[13],"")!=0){
                            precioEspecial($product->id,$d[12],$product->price,$d[13]);;

                        }

                        if(strcmp($d[14],"")!=0 && strcmp($d[15],"")!=0){
                            precioEspecial($product->id,$d[14],$product->price,$d[15]);


                        }

                        if(strcmp($d[16],"")!=0 && strcmp($d[17],"")!=0){

                            precioEspecial($product->id,$d[16],$product->price,$d[17]);

                        }

                        if(strcmp($d[18],"")!=0 && strcmp($d[19],"")!=0){
                            precioEspecial($product->id,$d[18],$product->price,$d[19]);


                        }

                        if(strcmp($d[20],"")!=0 && strcmp($d[21],"")!=0){
                            precioEspecial($product->id,$d[20],$product->price,$d[21]);


                        }

                        if(strcmp($d[22],"")!=0 && strcmp($d[23],"")!=0){
                            precioEspecial($product->id,$d[22],$product->price,$d[23]);


                        }

                        if(strcmp($d[24],"")!=0 && strcmp($d[25],"")!=0){
                            precioEspecial($product->id,$d[24],$product->price,$d[25]);


                        }

                        if(strcmp($d[26],"")!=0 && strcmp($d[27],"")!=0){
                            precioEspecial($product->id,$d[26],$product->price,$d[27]);


                        }

    */
                        //echo "inserto imagen";
                        //$imagen =getImageCsv($id);
    /*
                        $arrImg=explode(" ",$imagen);
                        print_r($arrImg);
                        $imagen1=$arrImg[0];
                        $imagen2=$arrImg[1];
                        //echo "$imagen1";
    *//*
                        $imagen1=$d[14];
                    echo "<br>la imagen es".$imagen1." <br>!"; 
                            $img = file_get_contents($d[14]);
                            file_put_contents('imagen.jpg', $img);
                            $url='imagen.jpg';
                            echo "imagen";
                            //if($imagen1){
                            $image = new Image();
                            $image->id_product = (int) $product->id;
                            $image->position = Image::getHighestPosition($product->id) + 1;
                            $image->cover =  true;
                            $image->add();
                            if (!copyImg($product->id, $image->id, $url, 'products', !Tools::getValue('regenerate'))) {
                                $image->delete();
                            }
                                
                    //}

                        //echo "1";
                        
                        @unlink("imagen.jpg");*/

                        //echo "1";

                        sleep(0.5);

                    //}
                    }
                }catch(Exception $ex){
 

                }


            //}
 

    }
            $linea=$linea+1;
    }catch(Exception $ex){

    }

Link to comment
Share on other sites

hace 42 minutos, tgonzalez dijo:

StockAvailable::setQuantity($id_product, $id_attribute, $quantity, $id_shop = null);

 

en $id_atribute qué pongo?

Un saludo

0(cero) Si no tiene combinaciones

# si tiene combinaciones, debes añadir antes las combinaciones y leer el id al añadirlo

 

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...