Jump to content

Problem adding products programmatically


Paulo Brandão
 Share

Recommended Posts

Hello.

I'm creating a set of custom php scripts to create categories, products and combinations from an external database (with XML).

All the code is working, but there's a strange situation regarding the product creation (ou indexing???): the new product is created, all the combinations work as they should, but the product doesn't show in front office (search, cat browsing, included). BUT if I open the product in Admin and save it WITHOUT making any change, then... problem solved!!! The product show in front office, searches, etc.

Troubleshooting until now: 

- disable cache

- products with quantity != 0

- combinations with quantity != 0

- the previous two solutions plus using  StockAvailable::setQuantity($product_id, $id_product_attribute, $quantity);

- set the "available date"

- manual index rebuild

Nothing worked. Four days stalling in here!!!!

Any sugestions?

Code excerpts

Create new product:

function ap ($codigo, $descricao, $preco, $imagem, $categoria) {

    $defaultLanguage = new Language((int)(Configuration::get('PS_LANG_DEFAULT')));
    $lang_id = $defaultLanguage->id;    
    
    $product = new Product();
    $product->reference = 'S3F'.$codigo;
    $product->name = array ($lang_id => $codigo);
    $product->description = array ($lang_id => $descricao);
    $product->price = $preco;
    $product->active = 1;
    $product->category = $categoria;
    $product->id_category_default = $categoria;
    $product->link_rewrite = 'S3F'.$codigo;
    $product->save();
    
    $url_imagem = $imagem;
    
    $shops = Shop::getShops(true, NULL, true);
    $image = new Image();
    $image->id_product = $product->id;
    $image->position = Image::getHighestPosition($product->id) + 1;
    $image->cover = true; // or false;

    
    if (($image->validateFields(false, true)) === true &&
        ($image->validateFieldsLang(false, true)) === true && 
            $image->add()
            ) {
            $image->associateTo($shops);
            if (!copyImg($product->id, $image->id, $url_imagem, 'products', true)) {
                $image->delete();
            }
    }
    return ($product->id);
}

Create combinations

function add_attribute_to_product ($product_id, $attribute_id, $preco) {
    $product = new Product($product_id);

    $defaultLanguage = new Language((int)(Configuration::get('PS_LANG_DEFAULT')));
    $lang_id = $defaultLanguage->id;
    $combinations = $product->getAttributeCombinations($lang_id);
    $update=false;
    foreach ($combinations as $comb) {
        if ($comb['id_attribute'] == $attribute_id) {
            $update=true;
            $preco_a_manter = $comb['price'];
        }
    }
    if ($update) {
        // UPDATE ATTRIB - LATER
    } else {
        $price = $preco_a_manter; // impacto sobre o preço base
        $weight = 0; // peso
        $unit_impact = 0;
        $ecotax = 0;
        $id_images = 0;
        $reference = '';
        $ean13 = '';
        $default = '0';
        $location = NULL;
        $upc = NULL;
        $minimal_quantity = 1;
        $id_shop_list = array();
        $available_date = date("Y-m-d");
        $quantity = 99;
        $isbn = '';
        $low_stock_threshold = NULL;
        $low_stock_alert = false;
        $id_product_attribute = $product->addAttribute($price, $weight,    $unit_impact, $ecotax, $id_images, $reference, $ean13, $default,$location,$upc, 
                                                        $minimal_quantity, $id_shop_list, $available_date, $quantity, $isbn, $low_stock_threshold, $low_stock_alert
                                            );
        StockAvailable::setQuantity($product_id, $id_product_attribute, $quantity);
        $combinationAttributes=array();
        $combinationAttributes[] = $attribute_id; 
        $product->addAttributeCombinaison($id_product_attribute, $combinationAttributes);
    }
}

 

Share this post


Link to post
Share on other sites

  • 2 years later...

Hello!

I don't think you still experiencing the issue in 3 years since the post but the issue is still actual when I tried to import using about the same custom code so might be usefull for someone else. Nothing helped like manual reindex from the admin panel. After research of the admin product save in core I found the following line:

Search::indexation(false, $productId);

It helped me to avoid the issue and now the products are visible in search right after the programmatical creation of the product.

Thanks,
Michael

Share this post


Link to post
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
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More