PrestaShop:如何从我的产品中获取ProductListingLazyArray

我对PrestaShop来说还很陌生-抱歉,如果我问基本的问题

我目前正在使用一个模块,该模块应显示您在后端选择的产品,作为默认产品模板中的其他部分-例如“强烈推荐的产品”

我完成了整个后端部分,并获得了ID作为所选产品的数组。

正如我提到的,我想使用默认模板,这些模板在全新安装后可用,我发现的内容放在themes\classic\templates\catalog\_partials\products.tpl内。

现在我最大的问题是:我无法获得应有的数据……

如果我调试例如默认搜索行为中显示的产品(也使用此模板),我看到类似

object(PrestaShop\PrestaShop\Adapter\Presenter\Product\ProductListingLazyArray)#323 (11) { ["imageRetriever":"Pr .....

但是当我得到我的产品时

new Product($productId,true);

它不是ProductListingLazyArray ...它只是一个包含产品的数组...而且我在前端看不到任何东西(当然我没有,因为例如{$product.id_product}在我的数组中看起来不是这样的...

您有什么想法可以将我的产品“转换”为ProductListingLazyArray吗? 还是我的想法错了?

谢谢大家!

iCMS 回答:PrestaShop:如何从我的产品中获取ProductListingLazyArray

解决方案
我只是“伪造”搜索并检查数据是否在我的数组中:

/**
 * creates relevant product information for frontend output
 * 
 * @param array $allSelectedProductIds array with all id's of the selected products 
 * @param int $languageId language id of the shop you are in
 * 
 * @return array all product information we need for our frontend rendering
 */
public function getFrontendProductInformation($allSelectedProductIds,$languageId)
{
    // set default category Home
    $category = new Category((int)2);

    // create new product search proider
    $searchProvider = new CategoryProductSearchProvider(
        $this->context->getTranslator(),$category
    );

    // set actual context
    $context = new ProductSearchContext($this->context);

    // create new search query
    $query = new ProductSearchQuery();
    $query->setResultsPerPage(PHP_INT_MAX)->setPage(1);
    $query->setSortOrder(new SortOrder('product','position','asc'));

    $result = $searchProvider->runQuery(
        $context,$query
    );

    // Product handling - to get relevant data
    $assembler = new ProductAssembler($this->context);
    $presenterFactory = new ProductPresenterFactory($this->context);
    $presentationSettings = $presenterFactory->getPresentationSettings();
    $presenter = new ProductListingPresenter(
        new ImageRetriever(
            $this->context->link
        ),$this->context->link,new PriceFormatter(),new ProductColorsRetriever(),$this->context->getTranslator()
    );

    $products = array();
    foreach ($result->getProducts() as $rawProduct) {
        $productId = $rawProduct['id_product'];
        if(in_array($productId,$allSelectedProductIds)) {
            $product = $presenter->present(
                $presentationSettings,$assembler->assembleProduct($rawProduct),$this->context->language
            );
            array_push($products,$product);
        }
    }

    return $products;
}
本文链接:https://www.f2er.com/1948307.html

大家都在问