【问题标题】:Linking modal to a button将模态链接到按钮
【发布时间】:2021-02-18 09:07:17
【问题描述】:

https://www.fermento24.com - https://www.fermento24.com/collections/all

使用 Shopify 完成网站。由于在主页上它使用网站完成的标准材料,它会自动链接到它的模态 ajax。

同时,收集页面(第二个链接)是由一个显示过滤器和东西的应用程序生成的。我想做的是将购买按钮链接到此模式弹出窗口,而不是将我带到购物车页面。这个不知道怎么过,是不是应该在modal中做修改,还是在收藏页面模板中做修改?

【问题讨论】:

    标签: ajax shopify liquid


    【解决方案1】:

    您可以尝试下面列出的代码,可能需要根据您的主题进行一些更新。

    需要注意:您需要在产品添加到购物车后检查并创建一种方法来更新迷你购物车。

    window.onload = function(){
       // disable the default behavior
       $('.boost-pfs-addtocart-btn').each(function(index, button){
         var $this = $(button);
         $this.closest('form').on('submit', function(e){ 
           e.preventDefault();
         });
       });
       // create new logic once the button clicked
       $(document).on('click', '.boost-pfs-addtocart-btn', function(){
         var form = $(this).closest('form');
         $.post('/cart/add.js', form.serialize(), function(data, status){
           if( status == 'success' ){
             // show popup once added to cart
             var model = $(".ajax-success-modal");
             model.fadeIn(500);
             var item    = form.parents('.boost-pfs-filter-product-item-inner')[0]
             var imgSrc  = $(item).find('.boost-pfs-filter-product-item-main-image').attr('src');
             var pTitle  = $(item).find('.boost-pfs-filter-product-item-title').text();
             var CPrice  = $(item).find('.boost-pfs-filter-product-item-regular-price').text();
             model.find(".ajax-product-image").attr("src", imgSrc);
             model.find(".added-to-wishlist").hide();
             model.find(".added-to-cart").show();
             model.find(".ajax-product-title").text(pTitle);
             model.find(".ajax_price").text(CPrice);
             model.find(".ajax_qty").text(1);
             // updating the cart counter in header
             $.get("/cart.js", function(data){
              $('#cartCount,.ajax_qtyA').text('').text(data.item_count);
              $('.ajax_cartTotal').text(Shopify.formatMoney(data.total_price, window.money_format));
             }, 'json');
           }
         }, 'json');
       });
    }
    

    请将任何 JS 文件中的代码或</body> 结尾之前的代码放入<script></script> 标记中,如果将其放在正文标记之前。

    注意:还要确保在代码运行之前加载jQuery,否则代码会因为依赖jQuery lib而失败。

    【讨论】:

    • 你可以在这里看到:0101t4k1zrkoj5vd-26803830855.shopifypreview.com 我已经在最后添加了代码,在正文之前,我认为脚本是在 jQuery 运行之后加载的(它应该就在头部) ,但似乎对新逻辑没有反应
    • 对不起,有一个代码拼写错误,更新答案,您需要复制并重试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-03
    • 2017-01-10
    • 1970-01-01
    相关资源
    最近更新 更多