【问题标题】:Check if product already in wishlist检查产品是否已在愿望清单中
【发布时间】:2019-12-01 12:35:25
【问题描述】:

我只是想检查产品是否已添加到愿望清单中(仅限已登录的客户)

如果产品已经在愿望清单上,则将结果放在一个 twig 文件中

button color=red
else
button color=gray

甚至还得到上述结果,当用户点击“添加到愿望清单按钮”时,将给出一个实时结果,我的意思是按钮颜色 gray 将使用 JavaScript 更改为 red

我用的是最新版本 3.0.3.2 打开购物车版本

【问题讨论】:

    标签: javascript php ajax content-management-system opencart


    【解决方案1】:

    尝试使用此代码。 我已将其修改如下。这对我来说就像现在一样工作

    添加要对其执行操作的标签

    <li><a href="javascript:void(0);" class="btn_wishlist_alt" data-product-id=69><i class="fa fa-heart"></i> <span class="">{{ text_wishlist }}</span></a></li>
    

    现在在你的文件中添加一些 js 代码

    <script type="text/javascript">
          $(document).ready(function(){
            $('.btn_wishlist_alt').on('click', function(){
              //alert();
              var self = this;
              var pID = $(self).attr('data-product-id');
              $.ajax({
                url: 'index.php?route=product/product/wishlistcheck&product_id=' +  encodeURIComponent(pID),
                dataType: 'json',
                type: 'post',
                cache: false,
                contentType: false,
                processData: false,
                success: function(json) {
                  if(json['in_wishlist']){
                    //set color red
                  }else{
                    //set color blue
                  }
                }
              });
            });
          })
        </script>
    

    现在将此函数添加到您的控制器文件中

    public function wishlistcheck(){
    
            $json = array();
    
            if ($this->request->server['REQUEST_METHOD'] == 'POST') {
                $product_id = $this->request->get['product_id'];
                if ($this->customer->getId()) {
                    $this->load->model('account/wishlist');
                    $wishlist = $this->model_account_wishlist->getWishlist();
                    if(in_array($product_id, array_column($wishlist, 'product_id'))) {
                        $json['in_wishlist'] = true;
                    }else{
                        $json['in_wishlist'] = false;
                    }
                }
            }
            $this->response->addHeader('Content-Type: application/json');
            $this->response->setOutput(json_encode($json));
        }
    

    来了。就是这样。

    【讨论】:

    • 我使用的是 opencart 版本 3.0.3.1,所以这种方法可能在那里。?以及如何将其用于树枝文件。?
    • 是的,这对于 Opencart v3.X 是可能的。要实时更改树枝文件,您可以使用 ajax。
    • 您能提供一个示例文件吗?因为我不熟悉ajax。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-09
    相关资源
    最近更新 更多