PrestaShop, assign a supplier to a product

1.5k views Asked by At

I'm developing an import products cron. In my code I have:

if ($supplier = Supplier::getIdByName(trim($prodotto['Supplier']['Name']))) {
    $product->id_supplier = (int)$supplier;
} else {
    $supplier = new Supplier();
    $supplier->name = $prodotto['Supplier']['Name'];
    $supplier->active = true;
    $supplier->add();

    $product->id_supplier = (int)$supplier->id;
    $supplier->associateTo($product->id_shop_list);

}

The result is:

  • product created
  • supplier created
  • product without supplier

Where am I wrong?

1

There are 1 answers

0
marsaldev On BEST ANSWER

You have to add also a new ProductSupplier, after you saved te new product use this snippet of code (obviously, adapt it to your needs :)):

// Product supplier
if (isset($product->id_supplier) && property_exists($product, 'supplier_reference'))
{
    $id_product_supplier = ProductSupplier::getIdByProductAndSupplier((int)$product->id, 0, (int)$product->id_supplier);
    if ($id_product_supplier)
        $product_supplier = new ProductSupplier((int)$id_product_supplier);
    else
        $product_supplier = new ProductSupplier();

    $product_supplier->id_product = $product->id;
    $product_supplier->id_product_attribute = 0;
    $product_supplier->id_supplier = $product->id_supplier;
    $product_supplier->product_supplier_price_te = $product->wholesale_price;
    $product_supplier->product_supplier_reference = $product->supplier_reference;
    $product_supplier->save();
}