Prestashop 1.7 product flags

Viewed 4243

I'm new here. I've got an issue with adding to the custom theme in PrestaShop 1.7 a new product flag which is Bestselling.

I've found that in ProductLazyArray.php are definitions for those flags, but this file is a core file and I'm not sure that I should modify that.

Can you help me with that one? Thank you in advance!

2 Answers

thanks for replying. I've figured that one out by adding hook into ProductLazyArray.php. It goes like:

        \Hook::exec('actionProductFlagsModifier', array (
        'flags' => & $flags,
        'product' => $this-> product,
    ));

And my module logic is like:

  public function getBestSellingProductsId() {
    $bestSellingProducts = ProductSale::getBestSalesLight((int)$this->langID, 0);
    $productsIdArray = array();
    foreach ($bestSellingProducts as $bestSellingProduct) {
        if (!empty($bestSellingProducts) && !in_array($bestSellingProduct['id_product'], $productsIdArray)) {
            $productsIdArray[] = $bestSellingProduct['id_product'];
        }
    }
    return $productsIdArray;
}

public function isBestSelling($arrayOfBestSellingIds = array(), $productId) {
    if (!empty($arrayOfBestSellingIds)) {
        if (in_array($productId, $arrayOfBestSellingIds)) {
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}

public function hookActionProductFlagsModifier($params)
{
    $bestSellings = $this->getBestSellingProductsId();
    $allProductsId = $params['product']['id_product'];
    if (!empty($allProductsId)) {
        if (in_array($allProductsId, $bestSellings)) {
            array_push($params['product'], $params['product']['is_best_seller'] = (int) in_array($allProductsId, $bestSellings));
        }
    }
    if (in_array('is_best_seller', $params['product'])
        && $this->isBestSelling($bestSellings, $allProductsId)) {
        $params['flags']['bestseller'] = array(
            'type' => 'bestseller',
            'label' => 'Hit'
        );
    }
    return $params;
}
Related