0

I have a code to add a list of products to a table in my database, as you can see here. In my code below, you can see that it shouldn't add the product if id_product's (CODIGO) unique value is already at the table. (getSize == 0)

$collection = Mage::getModel('novapc_allnations/promotion')->getCollection();
$promo = Mage::getModel('novapc_allnations/promotion');

foreach ($ab as $product) {
    # Filtra a collection atual de promoções, se o ID do produto atual ja estiver la, não adiciona
    $collection = $collection->addFieldToFilter('id_product', $product['CODIGO']);
    if ($collection->getSize() == 0) {
        $promo->setData(array(
            'name'             => $product['DESCRICAO'],
            'id_product'       => $product['CODIGO'],
            'promo_price'      => $product['PRECOREVENDA'],
            'descrtec'         => $product['DESCRTEC'],
            'category'         => $product['CATEGORIA'],
            'sub_category'     => $product['SUBCATEGORIA'],
            'manufacturer'     => $product['FABRICANTE'],
            'department'       => $product['DEPARTAMENTO'],
            'partnumber'       => $product['PARTNUMBER'],
            'ean'              => $product['EAN'],
            'warranty'         => $product['GARANTIA'],
            'weight'           => $product['PESOKG'],
            'resale_price'     => $product['PRECOREVENDA'],
            'price_without_st' => $product['PRECOSEMST'],
            'expire_date'      => $product['DATAVALIDADEPRECO'],
            'available'        => $product['DISPONIVEL'],
            'pic'              => $product['URLFOTOPRODUTO'],
            'stock'            => $product['ESTOQUE'],
            'ncm'              => $product['NCM'],
            'width'            => $product['LARGURA'],
            'height'           => $product['ALTURA'],
            'depth'            => $product['PROFUNDIDADE'],
            'active'           => $product['ATIVO'],
            'subst_tributaria' => $product['SUBSTTRIBUTARIA'],
            'product_origin'   => $product['ORIGEMPRODUTO'],
            'available_stock'  => $product['ESTOQUEDISPONIVEL'],
            'updated_at'       => $product['TIMESTAMP']
        ));
        $promo->save();
    }
}

But when I manually delete some rows from the table, it tries to add all products in list again, and throw a duplicate error. Am I doing something wrong? Is there a better method than getSize to this? Thank you

1

You should move

$collection = Mage::getModel('novapc_allnations/promotion')->getCollection();

inside loop as when you apply addFieldToFilter , your collection is filtered as per the $product['CODIGO'] value and when next time in loop when you filter the collection again for $product['CODIGO'] your collection is empty so it does not return any result. However moving the getCollection() line inside the loop will slow down the process for large collections

  • What if I just leave $collection = Mage::getModel('novapc_allnations/promotion'); before the foreach and get the collection inside loop? Do you think it will work? – Stordon Aug 28 '17 at 11:41
  • Yes it should work.. give it a try – magento noob Aug 28 '17 at 11:43
  • great !! glad to help :) – magento noob Aug 28 '17 at 12:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.