Jump to content
  • разработка интернет магазинов на opencart
  • доработка интернет магазинов на opencart

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


kuripka2222
 Share

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>


 

Link to comment
Share on other sites


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>

 

Edited by kuripka2222
  • +1 1
Link to comment
Share on other sites


Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

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.