Перейти до вмісту
Пошук в
  • Детальніше...
Шукати результати, які ...
Шукати результати в ...

Стиль кнопки "добавить в избранные", когда товар добавлен


kuripka2222

Recommended Posts

Здравствуйте, помогите сделать, чтобы стиль кнопки менялся при добавлении товара в избранные и сохранялся при обновлении страницы.

Нужно решение для Opencart 3

Нашел на форуме тему, но там в tpl, я попробовал перевести в twig, но ничего не вышло


Содержание решения с темы по Opencart 2

    
catalog\model\account\wishlist.php
    
    public function getAllWishlist() {
        if ($data = $this->getWishlist()) {
            $out = array();
            foreach ($data as $key => $value) {
                $out[] = $value['product_id'];
            }
            return $out;
        } else if (isset($this->session->data['wishlist'])) {
            return $this->session->data['wishlist'];
        }

    }
    
    


    
catalog\view\theme\default\template\product\category.tpl    


вместо:
<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
                  <?php if(isset($AllWishlist)) { ?>
                      <?php if(in_array($product['product_id'], $AllWishlist)) { ?>
                          style="color: red"
                      <?php } ?>
                  <?php } ?>

type="button" data-toggle="tooltip" title="<?php echo $button_wishlist; ?>" onclick="wishlist.add('<?php echo $product['product_id']; ?>'); ChangeColor(this);"><i class="fa fa-heart"></i></button>       
            
                
                
                <script type="text/javascript">
          function ChangeColor(Element) {
            Element.style.color = 'red';
          }
          </script>
          


catalog\controller\product\category.php    

перед      
          
$data['breadcrumbs'] = array();          

это:
$this->load->model('account/wishlist');
        $data['AllWishlist'] = $this->model_account_wishlist->getAllWishlist();    
        
        
        
catalog\controller\product\product.php


перед

if ($product_info) {
            $url = '';
            
            
это:


$this->load->model('account/wishlist');
        $zol_wish = $this->model_account_wishlist->getAllWishlist();
        if (isset($product_id) && isset($zol_wish)) {
          if (in_array($product_id, $zol_wish)) {
            $data['AllWishlist'] = true;
          }
          }
          
          
          
          
catalog\view\theme\default\template\product\product.tpl


вместо:
<button type="button" data-toggle="tooltip" class="btn btn-default" title="<?php echo $button_wishlist; ?>" onclick="wishlist.add('<?php echo $product_id; ?>');"><i class="fa fa-heart"></i></button>
          
это:          
      <button 
            <?php if(isset($AllWishlist)) { ?>
                    style="color: red" 
                  <?php } ?> 
                 type="button" data-toggle="tooltip" class="btn btn-default" title="<?php echo $button_wishlist; ?>" onclick="wishlist.add('<?php echo $product_id; ?>'); ChangeColor(this);"><i class="fa fa-heart"></i></button>
                 
           
            <script type="text/javascript">
          function ChangeColor(Element) {
            Element.style.color = 'red';
          }
          </script>


 

Надіслати
Поділитися на інших сайтах


31 минуту назад, dolyalexey сказал:

Поделитесь решением. Думаю для многих будет полезная функция. 

Самый первый пост прочитайте, там и есть решение)

Надіслати
Поділитися на інших сайтах

40 минут назад, dolyalexey сказал:

Поделитесь решением. Думаю для многих будет полезная функция. 

Решение точно такое же как и на opencart 2. Правильный перевод в twig и все супер. 

Решение: 

 

1. В файле /catalog/model/account/wishlist.php
-после строк: 
"	public function getTotalWishlist() {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "customer_wishlist WHERE customer_id = '" . (int)$this->customer->getId() . "'");

		return $query->row['total'];
	}"

добавить: 
	    public function getAllWishlist() {  
        if ($data = $this->getWishlist()) {
            $out = array();
            foreach ($data as $key => $value) {
                $out[] = $value['product_id'];
        }
            return $out;
        } else if (isset($this->session->data['wishlist'])) {
            return $this->session->data['wishlist'];
        }

    }

2. В файле catalog\view\theme\default\template\product\category.twig

Вместо кнопки "В закладки": <button type="button" data-toggle="tooltip" title="{{ button_wishlist }}" onclick="wishlist.add('{{ product.product_id }}');"><i class="fa fa-heart"></i></button>

Вставьте: 
<button
{% if (AllWishlist is defined) %} 
{% if (product['product_id'] in AllWishlist) %} 
  style="color: red"
{% endif %} 
{% endif %}
type="button" class="wishlist-buttons" data-toggle="tooltip" title="{{ button_wishlist }}" onclick="wishlist.add('{{ product.product_id }}'); ChangeColor(this);"><i class="fa fa-star"></i></button>      
<script type="text/javascript">
  function ChangeColor(Element) {
  Element.style.color = 'red';
  }
</script>


3. В файле catalog\controller\product\category.php    

Перед: $data['breadcrumbs'] = array();
Вставьте: $this->load->model('account/wishlist');
          $data['AllWishlist'] = $this->model_account_wishlist->getAllWishlist(); 

4. В файле catalog\controller\product\product.php
Перед: if ($product_info) {
			$url = '';

Вставьте: $this->load->model('account/wishlist');
        $zol_wish = $this->model_account_wishlist->getAllWishlist();
        if (isset($product_id) && isset($zol_wish)) {
          if (in_array($product_id, $zol_wish)) {
            $data['AllWishlist'] = true;
          }
          }

5. В файле catalog\view\theme\default\template\product\product.twig

Вместо: <button type="button" data-toggle="tooltip" class="btn btn-default" title="{{ button_wishlist }}" onclick="wishlist.add('{{ product_id }}');"><i class="fa fa-heart"></i></button>

Вставьте: 		  <button                   
		  {% if (AllWishlist is defined) %} 
          style="color: red"
          {% endif %}
		  type="button" class="btn btn-default" data-toggle="tooltip" title="{{ button_wishlist }}" onclick="wishlist.add('{{ product_id }}'); ChangeColor(this);"><i class="fa fa-heart"></i></button>
 <script type="text/javascript">
function ChangeColor(Element) {
            Element.style.color = 'red';
          }
          </script>

 

Змінено користувачем kuripka2222
  • +1 2
Надіслати
Поділитися на інших сайтах


Створіть аккаунт або увійдіть для коментування

Ви повинні бути користувачем, щоб залишити коментар

Створити обліковий запис

Зареєструйтеся для отримання облікового запису. Це просто!

Зареєструвати аккаунт

Вхід

Уже зареєстровані? Увійдіть тут.

Вхід зараз
  • Зараз на сторінці   0 користувачів

    • Ні користувачів, які переглядиють цю сторінку
×
×
  • Створити...

Important Information

На нашому сайті використовуються файли cookie і відбувається обробка деяких персональних даних користувачів, щоб поліпшити користувальницький інтерфейс. Щоб дізнатися для чого і які персональні дані ми обробляємо перейдіть за посиланням . Якщо Ви натиснете «Я даю згоду», це означає, що Ви розумієте і приймаєте всі умови, зазначені в цьому Повідомленні про конфіденційність.