Shopify 结账页面checkouts.liquid添加一个交叉推荐加购产品(适用于shopify plus)

在Shopify 结账页面checkouts.liquid添加一个交叉推荐加购产品(适用于shopify plus)

用到两个模板文件:
settings_schema.json 、checkout.liquid
为了方便管理使用这里用到一个元字段的应用插件:Metafields Guru。

  1. 在主题模板中配置几个基础字段的内容信息引用:
    在这里插入图片描述
    实现代码:
{
    "name": "Will: Checkout Product Cross Sell",
    "settings": [
      {
        "type": "checkbox",
        "id": "will_crosssell_enable",
        "label": "开启结账页交叉销售推荐",
        "default": false
      },
      {
        "type": "text",
        "id": "will_crosssell_header",
        "label": "标题",
        "info": "Other customer's favorite items"
      },
      {
        "type": "text",
        "id": "will_crosssell_text",
        "label": "提示文案",
        "info": "Limite-time sale! (CODE: HTG7) Unlock extra f30 savings with our hottest picks."
      },
      {
        "type": "text",
        "id": "will_crosssell_btn",
        "label": "按钮文案",
        "info": "Add to cart",
        "default": "Add to cart"
      }
    ]
  }

在应用插件Metafields Guru中添加相应的产品信息调用
Metafields Guru - Shop - Metafields 添加对应展示加购产品:
Type: JSON Namespace: cross_sell_products Key: item
Eg:

[{"id":41614540406989,"image":"https://us.ciarraappliances.com/cdn/shop/files/1_1.jpg?v=1686299346","title":"Ciarra HOOD TO GO Portable Mini Desktop Range Hood Shadow Noir","price":"$169.99"},{"id":41651319046349,"image":"https://us.ciarraappliances.com/cdn/shop/files/CiarraHOODTOGOPortableMiniDesktopRangeHoodOrange-1.jpg?v=1686283393","title":"Ciarra HOOD TO GO Portable Mini Desktop Range Hood Orangina","price":"$169.99"}]
  1. checkout.liquid实现功能代码:
         <!--  start cross sell by leo  20230919   -->
          {% if settings.will_crosssell_enable == true %}
          <div class="will-crosssell-adddiv" style="display:none;">
          <div class="will-crosssell">
            <h3>{{settings.will_crosssell_header}}</h3>
            <p>{{settings.will_crosssell_text}}</p>
            <div class="will-crosssell-list">
              {% for product_item in shop.metafields.cross_sell_products.item %}
                <div class="will-crosssell-item">
                  <div class="will-crosssell-item-img">
                    <img src="{{product_item.image}}" />
                  </div>
                  <div class="will-crosssell-title">{{ product_item.title }}<br><sqan class="will--crosssell-price">{{ product_item.price }}</sqan></div>
                  <div class="will-crosssell-btn"><button onclick="addToCart('{{ product_item.id }}')">{{settings.will_crosssell_btn}}</button></div>
                </div>
              {% endfor %}
              </div>
          </div>
          </div>
        <style>
          .will-crosssell{
             display:block;
             margin-bottom: 20px;
             border-top: 1px rgba(0,0,0,0.1) solid;
             padding-top: 2em;
          }
          .will-crosssell-list{
            margin-top: 1em;
          }
          .will-crosssell-item{
             display: flex;
             flex-wrap: nowrap;
             align-items: center;
             margin-bottom: 1rem;
             border: 1px rgba(0,0,0,0.1) solid;
             border-radius: 8px;
             padding: 1em;
          }
          .will-crosssell-item-img{
            max-width: 70px;
          }
         .will-crosssell-item-img img {
            width: 100%;
            border-radius: 8px;
            background: #fff;
            position: relative;
           border: 1px rgba(0,0,0,0.1) solid;
          }
          .will-crosssell-title{
            color: #313131;
            font-weight: 500;
            text-align: left;
            padding-left: 1em;
          }
          .will-crosssell-btn{
            padding-left: 1em;
          }
          .will-crosssell-btn button{
            border-radius: 8px;
            border: 1px rgba(0,0,0,0.1) solid;
            width: 5em;
            height:3em
          }
        </style>
        <script>
          try{
            // 获取要添加的内容
            const willCrosssellContent = document.querySelector('.will-crosssell-adddiv').innerHTML;
            // 获取要添加内容的目标位置
            const sidebarContent = document.querySelector('.order-summary__sections');
            // 将内容添加到目标位置
            sidebarContent.innerHTML += willCrosssellContent;
            
            function addToCart(productId) {
              var formData = new FormData();
              formData.append('id', productId);
              formData.append('quantity', 1);
          
              fetch('/cart/add.js', {
                method: 'POST',
                body: formData
              })
              .then(response => response.json())
              .then(data => {
                // 处理添加到结算列表的响应
                console.log(data);
                window.location.reload()
              })
              .catch(error => {
                // 处理错误
                console.error(error);
              });
            }
          }catch(e){}
        </script>
      {% endif %}
      <!--  end  cross sell by leo  -->
  1. 最终实现的效果:
    在这里插入图片描述
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值