PrestaShop,为产品分配供应商

时间:2016-12-20 12:01:58

标签: php prestashop prestashop-1.5

我正在开发一个进口产品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);

}

结果是:

  • 产品已创建
  • 供应商已创建
  • 无供应商的产品

我哪里错了?

1 个答案:

答案 0 :(得分:1)

您必须添加新的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();
}