PrestaShop,为产品分配供应商

PrestaShop, assign a supplier to a product

我正在开发一个进口产品 cron。 在我的代码中我有:

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);

}

结果是:

我哪里错了?

您还必须添加一个新的 ProductSupplier,在您保存新产品后使用这段代码(显然,根据您的需要进行调整 :)):

// 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();
}