Search code examples
phpxmlexportprestashop

Problem exporting XML multiple values on PHP 7.2 & Prestashop 1.7


I'm trying to export products from Prestashop 1.7 to an XML. Everything works great, but the script exports a product multiple times, depending on the categories that belongs to. For example Product1 is in All products - Clothes - Men , in this situation will be exported 3 times in XML.

I tried to use array_unique based on product ID but it doesn`t work. Can someone, please, point me to the right direction?

The full code can be found here: https://pastebin.com/MQxfYSj2

On this part of the code i tried to use array_unique with no result:

    private function getProductFromArray($arrProduct){
    $objProduct = new Okazii_Connector_Product();

    $objProduct->ID = $arrProduct['id_product'];
    $objProduct->UniqueID = $arrProduct['id_product'];
    $objProduct->Title = $arrProduct['name'];
    if (mb_strlen($arrProduct['description']) > 3){
        $objProduct->Description = $arrProduct['description'];
    } else if (mb_strlen($arrProduct['description_short'])) {
        $objProduct->Description = $arrProduct['description_short'];
    } else {
        $objProduct->Description = $arrProduct['name'];
    }
    $objProduct->Amount = $arrProduct['quantity'];

    if ($objProduct->Amount == 0){
        $objProduct->Amount = $this->getAmountFromStock($arrProduct['id_product']);
    }
    $objProduct->Category = $this->getCategoryString($arrProduct['id_category']);
    $objProduct->Currency = $this->getCurrency();
    $objProduct->Price = $arrProduct['price'];

    if(!empty($arrProduct['available_now']) && $objProduct->Amount > 0)
    {
        $objProduct->InStock = $arrProduct['available_now'];
    }

    if(!empty($arrProduct['gtin']))
    {
        $objProduct->GTIN = $arrProduct['gtin'];
    }
    else if(!empty($arrProduct['ean13']))
    {
        $objProduct->GTIN = $arrProduct['ean13'];
    }
    else if(!empty($arrProduct['isbn']))
    {
        $objProduct->GTIN = $arrProduct['isbn'];
    }

    $this->setProductImages($objProduct);
    $this->setProductBrand($objProduct, $arrProduct['id_manufacturer']);

    return $objProduct;
}

I also tried on the top of the script to get only unique products based on product ID, not working as well.

Other usefull info: PHP 7.2, Prestashop 1.7.6.5


Solution

  • Try to create an attribute in the very beginning of the class like

    private $exportedProductsIds = [];

    and then after each iteration put exported product id into it just before the return

    array_push($this->exportedProductsIds, $objProduct->id);
    

    and then add a verification before run if the id is already in the $exportedProductsIds then skip the iteration

    if (!in_array($objProduct->id, $this->exportedProductsIds)) {
       //run your code
    }
    

    But I think the best place to put this code would be that one where you call getProductFromArray method from.

    I assume like this

    private function exportProducts($products)
    {
         foreach ($products as $product) {
             if (in_array($objProduct->id, $this->exportedProductsIds)) {
                 continue;
             }
             if ($this->getProductFromArray($arrProduct)) {
                 array_push($this->exportedProductsIds, $objProduct->id);
             }
         }
    }