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

时间:2018-06-13 06:58:53

标签: php prestashop prestashop-1.7

我需要通过用户传递的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);
}

1 个答案:

答案 0 :(得分: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>';