Open Cart: как вывести опции товара в каталог

При разработке интернет-магазина на Open Cart иногда встает задача: в карточке товара в каталоге должны быть видны опции товара - например, доступные размеры одежды, цвета и т.п.

Итак, как вывести опции товара в каталог:

1) в controller/category.php добавить

			$options = array();

			foreach ($this->model_catalog_product->getProductOptions($result['product_id']) as $option) { 
				if ($option['type'] == 'radio') { 
					$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']
					);					
				} 
			}

2) перед $this->data['products'][] = array()
в $this->data['products'][] = array() добавить 'options' => $options

				$this->data['products'][] = array(
					'product_id'  => $result['product_id'],
						# OCFilter start
						'ocfilter_products_options' => $ocfilter_products_options[$result['product_id']],
						# OCFilter end
					'thumb'       => $image,
					'name'        => $result['name'],
					'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, 100) . '..',
					'price'       => $price,
					'special'     => $special,
					'tax'         => $tax,
					'rating'      => $result['rating'],
					'reviews'     => sprintf($this->language->get('text_reviews'), (int)$result['reviews']),
					'href'        => $this->url->link('product/product', 'path=' . $this->request->get['path'] . '&product_id=' . $result['product_id'] . $url),
					'options'     => $options
				);
			}

3) в category.tpl

      <?php if ($product['options']) { ?>
      <div class="options">
	<?php foreach ($product['options'] as $option) { ?>
	<?php if ($option['type'] == 'radio') { ?>
        <div id="option-<?php echo $option['product_option_id']; ?>" class="option">
          <?php if ($option['required']) { ?><span class="required">*</span><?php } ?><b><?php echo $option['name']; ?>:</b><br />

	  <?php foreach ($option['option_value'] as $option_value) { ?>
          <input type="radio" name="option[<?php echo $option['product_option_id']; ?>]" value="<?php echo $option_value['product_option_value_id']; ?>" id="option-value-<?php echo $option_value['product_option_value_id']; ?>" />
          <label for="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 } ?>
          </label>
          <br />
          <?php } //end foreach ?>
        </div>
        <br />
        <?php } // end if ($option['type'] ?>
        <?php } ?>
      </div>
      <?php } ?>

Оставить комментарий

Ваш e-mail не будет опубликован