如何在特色模块或opencart的特色产品上显示产品选项

时间:2013-11-07 15:25:49

标签: opencart

我正在使用精选模块将产品列入我的主页。但是,虽然产品选项在产品详细信息页面中正常工作,但它在主页(特色模块)中无法正常工作。最后一个产品的选项值将重复到所有产品列表。另外添加到购物车有一些问题。任何人都可以为此提供适当的解决方案吗?

代码如下:

控制器:

      $this->data['products'] = array();

      $products = explode(',', $this->config->get('featured_product'));     

      if (empty($setting['limit'])) {
         $setting['limit'] = 5;
      }

      $products = array_slice($products, 0, (int)$setting['limit']);

      foreach ($products as $product_id) {
         $product_info = $this->model_catalog_product->getProduct($product_id);

         if ($product_info) {
            if ($product_info['image']) {
               $image = $this->model_tool_image->resize($product_info['image'], $setting['image_width'], $setting['image_height']);
            } else {
               $image = false;
            }

            if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
               $price = $this->currency->format($this->tax->calculate($product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax')));
            } else {
               $price = false;
            }

            if ((float)$product_info['special']) {
               $special = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax')));
            } else {
               $special = false;
            }

            if ($this->config->get('config_review_status')) {
               $rating = $product_info['rating'];
            } else {
               $rating = false;
            }
            $this->data['products'][] = array(
               'product_id' => $product_info['product_id'],
               'thumb'       => $image,
               'name'        => $product_info['name'],
               'price'       => $price,
               'product_description'       => $product_info['product_description'],
               'special'     => $special,
               'rating'     => $rating,
               'reviews'    => sprintf($this->language->get('text_reviews'), (int)$product_info['reviews']),
               'href'        => $this->url->link('product/product', 'product_id=' . $product_info['product_id']),
            );   


         $this->data['options'] = array();
         //print_r($this->model_catalog_product->getProductOptions($this->request->get['product_id']));
         //die("");
         $option=$this->model_catalog_product->getProductOptions($product_id);
         //foreach ($this->model_catalog_product->getProductOptions($product_id) as $option) //{

               $option_value_data = array();

               foreach ($option['option_value'] as $option_value) {
                  if (!$option_value['subtract'] || ($option_value['quantity'] > 0)) {
                     if ((($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) && (float)$option_value['price']) {
                        $price = $this->currency->format($this->tax->calculate($option_value['price'], $product_info['tax_class_id'], $this->config->get('config_tax')));
                     } else {
                        $price = false;
                     }

                     $option_value_data[] = array(
                        'product_option_value_id' => $option_value['product_option_value_id'],
                        'option_value_id'         => $option_value['option_value_id'],
                        'name'                    => $option_value['name'],
                        'image'                   => $this->model_tool_image->resize($option_value['image'], 50, 50),
                        'price'                   => $price,
                        'price_prefix'            => $option_value['price_prefix']
                     );
                  }
               }

               $this->data['options'][] = array(
                  'product_option_id' => $option['product_option_id'],
                  'option_id'         => $option['option_id'],
                  'name'              => $option['name'],
                  'type'              => $option['type'],
                  'option_value'      => $option_value_data,
                  'required'          => $option['required']
               );               

         //}

      }
      }

视图:

<?php foreach ($products as $product) { ?>

<a href="<?php echo $product['href']; ?>"><img src="<?php echo $product['thumb']; ?>" alt="<?php echo $product['name']; ?>" /></a>   

<?php if ($options) {  ?>
      <div class="options" style="color: #457A33; float: left;line-height: 23px; margin-right: 20px; width:300px;">

        <?php foreach ($options as $option) { ?>
        <?php if ($option['type'] == 'select') { ?>
        <div id="option-<?php echo $option['product_option_id']; ?>" class="option" style="float:left; width:150px;">
          <?php if ($option['required']) { ?>
          <span class="required">*</span>
          <?php } ?>
          <b><?php echo $option['name']; ?>:</b><br />
          <select name="option[<?php echo $option['product_option_id']; ?>]">
            <option value=""><?php echo $text_select; ?></option>
            <?php foreach ($option['option_value'] as $option_value) { ?>
            <option value="<?php echo $option_value['product_option_value_id']; ?>"><?php echo $option_value['name']; ?>
            <?php if ($option_value['price']) { ?>
            (<?php echo $option_value['price_prefix']; ?><?php echo $option_value['price']; ?>)
            <?php } ?>
            </option>
            <?php } ?>
          </select>
          <br />
           <a href="#">What's this</a>
        </div>

        <?php } ?>
 <?php } ?>

1 个答案:

答案 0 :(得分:1)

必须将选项分配给具体产品 - 在$this->data['products'][] = array(...);之前进行填充和处理:

      $this->data['products'] = array();

      $products = explode(',', $this->config->get('featured_product'));     

      if (empty($setting['limit'])) {
         $setting['limit'] = 5;
      }

      $products = array_slice($products, 0, (int)$setting['limit']);

      foreach ($products as $product_id) {
         $product_info = $this->model_catalog_product->getProduct($product_id);

         if ($product_info) {
            if ($product_info['image']) {
               $image = $this->model_tool_image->resize($product_info['image'], $setting['image_width'], $setting['image_height']);
            } else {
               $image = false;
            }

            if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
               $price = $this->currency->format($this->tax->calculate($product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax')));
            } else {
               $price = false;
            }

            if ((float)$product_info['special']) {
               $special = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax')));
            } else {
               $special = false;
            }

            if ($this->config->get('config_review_status')) {
               $rating = $product_info['rating'];
            } else {
               $rating = false;
            }

            $options = array();

            foreach ($this->model_catalog_product->getProductOptions($product_id) as $option) {

               $option_value_data = array();

               foreach ($option['option_value'] as $option_value) {
                  if (!$option_value['subtract'] || ($option_value['quantity'] > 0)) {
                     if ((($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) && (float)$option_value['price']) {
                        $price = $this->currency->format($this->tax->calculate($option_value['price'], $product_info['tax_class_id'], $this->config->get('config_tax')));
                     } else {
                        $price = false;
                     }

                     $option_value_data[] = array(
                        'product_option_value_id' => $option_value['product_option_value_id'],
                        'option_value_id'         => $option_value['option_value_id'],
                        'name'                    => $option_value['name'],
                        'image'                   => $this->model_tool_image->resize($option_value['image'], 50, 50),
                        'price'                   => $price,
                        'price_prefix'            => $option_value['price_prefix']
                     );
                  }
               }

               $options[] = array(
                  'product_option_id' => $option['product_option_id'],
                  'option_id'         => $option['option_id'],
                  'name'              => $option['name'],
                  'type'              => $option['type'],
                  'option_value'      => $option_value_data,
                  'required'          => $option['required']
               );               
            }

            $this->data['products'][] = array(
               'product_id' => $product_info['product_id'],
               'thumb'       => $image,
               'name'        => $product_info['name'],
               'price'       => $price,
               'product_description'       => $product_info['product_description'],
               'special'     => $special,
               'rating'     => $rating,
               'reviews'    => sprintf($this->language->get('text_reviews'), (int)$product_info['reviews']),
               'href'        => $this->url->link('product/product', 'product_id=' . $product_info['product_id']),
               'options'     => $options,
            );   
      }

现在您应该分别在每个产品中加载选项。只需相应修改模板:

<?php foreach ($products as $product) { ?>

<a href="<?php echo $product['href']; ?>"><img src="<?php echo $product['thumb']; ?>" alt="<?php echo $product['name']; ?>" /></a>   

<?php if ($product['options']) {  ?>
      <div class="options" style="color: #457A33; float: left;line-height: 23px; margin-right: 20px; width:300px;">

        <?php foreach ($product['options'] as $option) { ?>
        <?php if ($option['type'] == 'select') { ?>
        <div id="option-<?php echo $option['product_option_id']; ?>" class="option" style="float:left; width:150px;">
          <?php if ($option['required']) { ?>
          <span class="required">*</span>
          <?php } ?>
          <b><?php echo $option['name']; ?>:</b><br />
          <select name="option[<?php echo $option['product_option_id']; ?>]">
            <option value=""><?php echo $text_select; ?></option>
            <?php foreach ($option['option_value'] as $option_value) { ?>
            <option value="<?php echo $option_value['product_option_value_id']; ?>"><?php echo $option_value['name']; ?>
            <?php if ($option_value['price']) { ?>
            (<?php echo $option_value['price_prefix']; ?><?php echo $option_value['price']; ?>)
            <?php } ?>
            </option>
            <?php } ?>
          </select>
          <br />
           <a href="#">What's this</a>
        </div>

        <?php } ?>
<?php } ?>