Jump to content
REDDI

Добавить опции в Последние товары

Recommended Posts

Всем привет. Делаю сейчас вывод опций к товару в модуле "Последние товары". (latest)

внутри

 if ($results) {
            foreach ($results as $result) {

 

            }

}

 

я добавил код:
 

$data['options'] = array();

				foreach ($this->model_catalog_product->getProductOptions($result['product_id']) as $option) {
					$product_option_value_data = array();

					foreach ($option['product_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') ? 'P' : false), $this->session->data['currency']);
							} else {
								$price = false;
							}

							$product_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'                   => $option_value['image'] ? $this->model_tool_image->resize($option_value['image'], 50, 50) : '',
								'price'                   => $price,
								'price_prefix'            => $option_value['price_prefix']
							);
						}
					}

					$data['options'][] = array(
						'product_option_id'    => $option['product_option_id'],
						'product_option_value' => $product_option_value_data,
						'option_id'            => $option['option_id'],
						'name'                 => $option['name'],
						'type'                 => $option['type'],
						'value'                => $option['value'],
						'required'             => $option['required']
					);
				}

но что-то какойто трабл с ним. Выбивает ошибку

 

Notice: Undefined variable: product_info in /home/u93316/site.ru/catalog/controller/extension/module/latest.php on line 82Notice: Undefined variable: product_info in /home/u93316/site.ru/catalog/controller/extension/module/latest.php on line 82

 

а ведь я еще вывести даже не пытался :D подскажите пожалуйста где я допустил ошибку

Share this post


Link to post
Share on other sites

У вас в коде используется переменная $product_info с инфой о товаре. 

$price = $this->currency->format($this->tax->calculate($option_value['price'], $product_info['tax_class_id'], $this->config->get('config_tax') ? 'P' : false), $this->session->data['currency']);
					

Но в контроллере модуля latest.php данной переменной нету.

Там есть $results. Вам нужно перед строкой 

$data['options'] = array();

добавить строку 

$product_info = $this->model_catalog_product->getProduct($result['product_id']);

Или если вам не нужна стоимость то просто вместо 

 

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') ? 'P' : false), $this->session->data['currency']);
							} else {
								$price = false;
							}

напишите $price = false;

 

  • +1 1

Share this post


Link to post
Share on other sites
54 минуты назад, Serg9988 сказал:

У вас в коде используется переменная $product_info с инфой о товаре. 


$price = $this->currency->format($this->tax->calculate($option_value['price'], $product_info['tax_class_id'], $this->config->get('config_tax') ? 'P' : false), $this->session->data['currency']);
					

Но в контроллере модуля latest.php данной переменной нету.

Там есть $results. Вам нужно перед строкой 


$data['options'] = array();

добавить строку 


$product_info = $this->model_catalog_product->getProduct($result['product_id']);

Или если вам не нужна стоимость то просто вместо 

 


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') ? 'P' : false), $this->session->data['currency']);
							} else {
								$price = false;
							}

напишите $price = false;

 

подскажи, а выводить так же как и в product.tpl?  Я вот вывел, но ничего не вывелось)

 

 

Share this post


Link to post
Share on other sites
4 часа назад, Serg9988 сказал:

добавить строку 


$product_info = $this->model_catalog_product->getProduct($result['product_id']);

зачем? уже ведь есть данные товаров.  просто $product_info   заменить на $result

  • +1 1

Share this post


Link to post
Share on other sites
57 минут назад, fanatic сказал:

зачем? уже ведь есть данные товаров.  просто $product_info   заменить на $result

Да точно. Завтыкал.

  • +1 1

Share this post


Link to post
Share on other sites
4 часа назад, REDDI сказал:

подскажи, а выводить так же как и в product.tpl?  Я вот вывел, но ничего не вывелось)

 

 

ну по идеи почти такой же. Только в товаре там идет сразу $product['options'] а у вас выйдет сначала foreach ($products as $product) {

 

и уже тут получаете опции.

  • +1 1

Share this post


Link to post
Share on other sites
2 часа назад, Serg9988 сказал:

ну по идеи почти такой же. Только в товаре там идет сразу $product['options'] а у вас выйдет сначала foreach ($products as $product) {

 

и уже тут получаете опции.

так оно же в самом начале уже есть https://yadi.sk/i/ghJPKyAi3Uokyo

Share this post


Link to post
Share on other sites

контроллер

class ControllerExtensionModuleLatest extends Controller {

    public function index($setting) {
	$this->load->language('extension/module/latest');

	$data['heading_title'] = $this->language->get('heading_title');

	$data['text_tax'] = $this->language->get('text_tax');

	$data['button_cart'] = $this->language->get('button_cart');
	$data['button_wishlist'] = $this->language->get('button_wishlist');
	$data['button_compare'] = $this->language->get('button_compare');

	$this->load->model('catalog/product');

	$this->load->model('tool/image');

	$data['products'] = array();

	$filter_data = array(
	    'sort' => 'p.date_added',
	    'order' => 'DESC',
	    'start' => 0,
	    'limit' => $setting['limit']
	);

	$results = $this->model_catalog_product->getProducts($filter_data);

	if ($results) {
	    foreach ($results as $result) {

		if ($result['image']) {
		    $image = $this->model_tool_image->resize($result['image'], $setting['width'], $setting['height']);
		} else {
		    $image = $this->model_tool_image->resize('placeholder.png', $setting['width'], $setting['height']);
		}

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

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

		if ($this->config->get('config_tax')) {
		    $tax = $this->currency->format((float) $result['special'] ? $result['special'] : $result['price'], $this->session->data['currency']);
		} else {
		    $tax = false;
		}

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

		//add options
		$options = array();

		foreach ($this->model_catalog_product->getProductOptions($result['product_id']) as $option) {
		    $product_option_value_data = array();

		    foreach ($option['product_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'], $result['tax_class_id'], $this->config->get('config_tax') ? 'P' : false), $this->session->data['currency']);
			    } else {
				$price = false;
			    }

			    $product_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' => $option_value['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'],
			'product_option_value' => $product_option_value_data,
			'option_id' => $option['option_id'],
			'name' => $option['name'],
			'type' => $option['type'],
			'value' => $option['value'],
			'required' => $option['required']
		    );
		}
		//end add oprions

		$data['products'][] = array(
		    //add options
		    'options' => $options,
		    // end add options
		    'product_id' => $result['product_id'],
		    'thumb' => $image,
		    'name' => $result['name'],
		    'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get($this->config->get('config_theme') . '_product_description_length')) . '..',
		    'price' => $price,
		    'special' => $special,
		    'tax' => $tax,
		    'rating' => $rating,
		    'href' => $this->url->link('product/product', 'product_id=' . $result['product_id'])
		);
	    }

	    return $this->load->view('extension/module/latest', $data);
	}
    }

}

шаблон

<h3><?php echo $heading_title; ?></h3>
<div class="row">
  <?php foreach ($products as $product) { ?>
  <div class="product-layout col-lg-3 col-md-3 col-sm-6 col-xs-12">
    <div class="product-thumb transition">
      <div class="image"><a href="<?php echo $product['href']; ?>"><img src="<?php echo $product['thumb']; ?>" alt="<?php echo $product['name']; ?>" title="<?php echo $product['name']; ?>" class="img-responsive" /></a></div>
      <div class="caption">
        <h4><a href="<?php echo $product['href']; ?>"><?php echo $product['name']; ?></a></h4>
        <p><?php echo $product['description']; ?></p>
        <?php if ($product['rating']) { ?>
        <div class="rating">
          <?php for ($i = 1; $i <= 5; $i++) { ?>
          <?php if ($product['rating'] < $i) { ?>
          <span class="fa fa-stack"><i class="fa fa-star-o fa-stack-2x"></i></span>
          <?php } else { ?>
          <span class="fa fa-stack"><i class="fa fa-star fa-stack-2x"></i><i class="fa fa-star-o fa-stack-2x"></i></span>
          <?php } ?>
          <?php } ?>
        </div>
        <?php } ?>
	
	<!-- add oprions -->
	<?php if ($product['options']) { ?>
            <hr>
            <h3>Доступные опции</h3>
            <?php foreach ($product['options'] as $option) { ?>
            <?php if ($option['type'] == 'select') { ?>
            <div class="form-group<?php echo ($option['required'] ? ' required' : ''); ?>">
              <label class="control-label" for="input-option<?php echo $option['product_option_id']; ?>"><?php echo $option['name']; ?></label>
              <select name="option[<?php echo $option['product_option_id']; ?>]" id="input-option<?php echo $option['product_option_id']; ?>" class="form-control">
                <option value="">Выберите опцию</option>
                <?php foreach ($option['product_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>
            </div>
            <?php } ?>
            <?php if ($option['type'] == 'radio') { ?>
            <div class="form-group<?php echo ($option['required'] ? ' required' : ''); ?>">
              <label class="control-label"><?php echo $option['name']; ?></label>
              <div id="input-option<?php echo $option['product_option_id']; ?>">
                <?php foreach ($option['product_option_value'] as $option_value) { ?>
                <div class="radio">
                  <label>
                    <input type="radio" name="option[<?php echo $option['product_option_id']; ?>]" value="<?php echo $option_value['product_option_value_id']; ?>" />
                    <?php if ($option_value['image']) { ?>
                    <img src="<?php echo $option_value['image']; ?>" alt="<?php echo $option_value['name'] . ($option_value['price'] ? ' ' . $option_value['price_prefix'] . $option_value['price'] : ''); ?>" class="img-thumbnail" /> 
                    <?php } ?>                    
                    <?php echo $option_value['name']; ?>
                    <?php if ($option_value['price']) { ?>
                    (<?php echo $option_value['price_prefix']; ?><?php echo $option_value['price']; ?>)
                    <?php } ?>
                  </label>
                </div>
                <?php } ?>
              </div>
            </div>
            <?php } ?>
            <?php if ($option['type'] == 'checkbox') { ?>
            <div class="form-group<?php echo ($option['required'] ? ' required' : ''); ?>">
              <label class="control-label"><?php echo $option['name']; ?></label>
              <div id="input-option<?php echo $option['product_option_id']; ?>">
                <?php foreach ($option['product_option_value'] as $option_value) { ?>
                <div class="checkbox">
                  <label>
                    <input type="checkbox" name="option[<?php echo $option['product_option_id']; ?>][]" value="<?php echo $option_value['product_option_value_id']; ?>" />
                    <?php if ($option_value['image']) { ?>
                    <img src="<?php echo $option_value['image']; ?>" alt="<?php echo $option_value['name'] . ($option_value['price'] ? ' ' . $option_value['price_prefix'] . $option_value['price'] : ''); ?>" class="img-thumbnail" /> 
                    <?php } ?>
                    <?php echo $option_value['name']; ?>
                    <?php if ($option_value['price']) { ?>
                    (<?php echo $option_value['price_prefix']; ?><?php echo $option_value['price']; ?>)
                    <?php } ?>
                  </label>
                </div>
                <?php } ?>
              </div>
            </div>
            <?php } ?>
            <?php if ($option['type'] == 'text') { ?>
            <div class="form-group<?php echo ($option['required'] ? ' required' : ''); ?>">
              <label class="control-label" for="input-option<?php echo $option['product_option_id']; ?>"><?php echo $option['name']; ?></label>
              <input type="text" name="option[<?php echo $option['product_option_id']; ?>]" value="<?php echo $option['value']; ?>" placeholder="<?php echo $option['name']; ?>" id="input-option<?php echo $option['product_option_id']; ?>" class="form-control" />
            </div>
            <?php } ?>
            <?php if ($option['type'] == 'textarea') { ?>
            <div class="form-group<?php echo ($option['required'] ? ' required' : ''); ?>">
              <label class="control-label" for="input-option<?php echo $option['product_option_id']; ?>"><?php echo $option['name']; ?></label>
              <textarea name="option[<?php echo $option['product_option_id']; ?>]" rows="5" placeholder="<?php echo $option['name']; ?>" id="input-option<?php echo $option['product_option_id']; ?>" class="form-control"><?php echo $option['value']; ?></textarea>
            </div>
            <?php } ?>
            <?php if ($option['type'] == 'file') { ?>
            <div class="form-group<?php echo ($option['required'] ? ' required' : ''); ?>">
              <label class="control-label"><?php echo $option['name']; ?></label>
              <button type="button" id="button-upload<?php echo $option['product_option_id']; ?>" data-loading-text="<?php echo $text_loading; ?>" class="btn btn-default btn-block"><i class="fa fa-upload"></i> <?php echo $button_upload; ?></button>
              <input type="hidden" name="option[<?php echo $option['product_option_id']; ?>]" value="" id="input-option<?php echo $option['product_option_id']; ?>" />
            </div>
            <?php } ?>
            <?php if ($option['type'] == 'date') { ?>
            <div class="form-group<?php echo ($option['required'] ? ' required' : ''); ?>">
              <label class="control-label" for="input-option<?php echo $option['product_option_id']; ?>"><?php echo $option['name']; ?></label>
              <div class="input-group date">
                <input type="text" name="option[<?php echo $option['product_option_id']; ?>]" value="<?php echo $option['value']; ?>" data-date-format="YYYY-MM-DD" id="input-option<?php echo $option['product_option_id']; ?>" class="form-control" />
                <span class="input-group-btn">
                <button class="btn btn-default" type="button"><i class="fa fa-calendar"></i></button>
                </span></div>
            </div>
            <?php } ?>
            <?php if ($option['type'] == 'datetime') { ?>
            <div class="form-group<?php echo ($option['required'] ? ' required' : ''); ?>">
              <label class="control-label" for="input-option<?php echo $option['product_option_id']; ?>"><?php echo $option['name']; ?></label>
              <div class="input-group datetime">
                <input type="text" name="option[<?php echo $option['product_option_id']; ?>]" value="<?php echo $option['value']; ?>" data-date-format="YYYY-MM-DD HH:mm" id="input-option<?php echo $option['product_option_id']; ?>" class="form-control" />
                <span class="input-group-btn">
                <button type="button" class="btn btn-default"><i class="fa fa-calendar"></i></button>
                </span></div>
            </div>
            <?php } ?>
            <?php if ($option['type'] == 'time') { ?>
            <div class="form-group<?php echo ($option['required'] ? ' required' : ''); ?>">
              <label class="control-label" for="input-option<?php echo $option['product_option_id']; ?>"><?php echo $option['name']; ?></label>
              <div class="input-group time">
                <input type="text" name="option[<?php echo $option['product_option_id']; ?>]" value="<?php echo $option['value']; ?>" data-date-format="HH:mm" id="input-option<?php echo $option['product_option_id']; ?>" class="form-control" />
                <span class="input-group-btn">
                <button type="button" class="btn btn-default"><i class="fa fa-calendar"></i></button>
                </span></div>
            </div>
            <?php } ?>
            <?php } ?>
            <?php } ?>
	<!-- end add options -->
	
        <?php if ($product['price']) { ?>
        <p class="price">
          <?php if (!$product['special']) { ?>
          <?php echo $product['price']; ?>
          <?php } else { ?>
          <span class="price-new"><?php echo $product['special']; ?></span> <span class="price-old"><?php echo $product['price']; ?></span>
          <?php } ?>
          <?php if ($product['tax']) { ?>
          <span class="price-tax"><?php echo $text_tax; ?> <?php echo $product['tax']; ?></span>
          <?php } ?>
        </p>
        <?php } ?>
      </div>
      <div class="button-group">
        <button type="button" onclick="cart.add('<?php echo $product['product_id']; ?>');"><i class="fa fa-shopping-cart"></i> <span class="hidden-xs hidden-sm hidden-md"><?php echo $button_cart; ?></span></button>
        <button type="button" data-toggle="tooltip" title="<?php echo $button_wishlist; ?>" onclick="wishlist.add('<?php echo $product['product_id']; ?>');"><i class="fa fa-heart"></i></button>
        <button type="button" data-toggle="tooltip" title="<?php echo $button_compare; ?>" onclick="compare.add('<?php echo $product['product_id']; ?>');"><i class="fa fa-exchange"></i></button>
      </div>
    </div>
  </div>
  <?php } ?>
</div>

 

  • +1 1

Share this post


Link to post
Share on other sites
1 час назад, fanatic сказал:

контроллер

шаблон

 

ошибка 

Parse error: syntax error, unexpected ''product_id'' (T_CONSTANT_ENCAPSED_STRING), expecting ')' 

строка в которой ошибка

'product_id' => $result['product_id'],

 

не знаю причем тут она вобще

 

 

Edited by REDDI

Share this post


Link to post
Share on other sites

код рабочий, я  проверял. 

  • +1 1

Share this post


Link to post
Share on other sites
Только что, fanatic сказал:

код рабочий, я  проверял. 

подскажи, где искать проблему? А можно ли, что бы я тебе дал файл свой? По структуре он как твой почти

Share this post


Link to post
Share on other sites
4 минуты назад, fanatic сказал:

код рабочий, я  проверял. 

ааа ну да. Я вот взял это модуль https://opencartforum.com/files/tovari-iz-kategorii
мне помогли переделать под 2.3.0.3 (кому надо берите) https://yadi.sk/d/AF1f6CuG3Up3vi
и вывожу уже в нем опции. Но он точно такой же как lastest

Share this post


Link to post
Share on other sites
1 час назад, fanatic сказал:

код рабочий, я  проверял. 

а у тебя версия то 2.3.0.2.3 ?Я твой код вставил в latest и он не работает(

Share this post


Link to post
Share on other sites
37 минут назад, REDDI сказал:

а у тебя версия то 2.3.0.2.3 ?

да

  • +1 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
You are posting as a guest. If you have an account, please sign in.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×

Important Information

On our site, cookies are used and personal data is processed to improve the user interface. To find out what and what personal data we are processing, please go to the link. If you click "I agree," it means that you understand and accept all the conditions specified in this Privacy Notice.