如何使用产品ID数组显示产品

问题描述 投票:-1回答:1

我需要通过用户传递的ID数组在主页上显示产品,我正在考虑将特色产品模块从类别修改为产品ID选择。

$my_ids = array(Configuration::get('SLOT_1'), 
Configuration::get('SLOT_2'), Configuration::get('SLOT_3'), 
Configuration::get('SLOT_4'), Configuration::get('SLOT_5'), 
Configuration::get('SLOT_6'), Configuration::get('SLOT_7'), 
Configuration::get('SLOT_8')
);

$myProducts = array();

foreach( $my_ids as $id_product ) {
    $myProducts[] = new Product($id_product);
}
php prestashop prestashop-1.7
1个回答
0
投票

尝试这些方面的东西:

    <?php

class Product
{
    private $products = ['prod1', 'prod2', 'prod3', 'prod4', 'prod5', 'prod6', 'prod7', 'prod8'];
    public $selectedProducts = [];

    public function __construct($id_product)
    {
        $key = explode('_', $id_product);
        $prod = $key[1];
        $this->selectedProducts[] = $this->products[$prod - 1];
    }

    public function getProducts()
    {
        return $this->selectedProducts;
    }
}

//$my_ids = array(Configuration::get('SLOT_1'),Configuration::get('SLOT_2'),Configuration::get('SLOT_3'),Configuration::get('SLOT_4'),Configuration::get('SLOT_5'),Configuration::get('SLOT_6'),Configuration::get('SLOT_7'),Configuration::get('SLOT_8'));
$my_ids = array('SLOT_1', 'SLOT_2', 'SLOT_3', 'SLOT_4', 'SLOT_5', 'SLOT_6', 'SLOT_7', 'SLOT_8');
$myProducts = array();

foreach ($my_ids as $id_product) {
    $product = new Product($id_product);
    $myProducts[] = $product->getProducts();
}

echo '<pre>';
print_r($myProducts);
echo '</pre>';
© www.soinside.com 2019 - 2024. All rights reserved.