首页 > 其他分享 >shopify模板二次开发 增加购物车、立即购买功能

shopify模板二次开发 增加购物车、立即购买功能

时间:2024-05-31 10:00:03浏览次数:16  
标签:shopify settings 购物车 二次开发 promotion label type id block

 
<div class="promotionDiscount" data-id="{{ section.settings.promotionDiscount_id }}">
    <div class="promotionDiscount_conter container">
      <div class="promotionDiscount_title">{{ section.settings.promotionDiscount_title }}</div>
      <div class="promotionProduct_box">
        {% for block in section.blocks %}
            <div class="promotionProduct_list" >
                <div class="promotion">{{ block.settings.promotion }}</div>
               {%- render 'product-item', product: block.settings.promotionProduct -%}
                <div class="promotionProduct_list_bottom">
                <div class="promotionProduct_list_num">
                 <button type="button" class="promotion_reduce promotion_icon" disabled >-</button>
                 <input id="promotion_num" class="promotion_num" name="quantity" type="text" value="1">
                 <button type="button" class="promotion_add promotion_icon">+</button>
               </div>
               {% if block.settings.promotionProduct.available == true %}
                 {% if block.settings.method == 'cart' %}
                 <div class="promotionButton promotionButton_cart" data-variants="{{ block.settings.promotionProduct.variants[0].id }}">
                    {{ block.settings.button }}
                    <svg t="1663833321925" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2890" width="27" height="27"> <path d="M853.333333 507.733333H128v42.666667h733.866667l-145.066667 145.066667 29.866667 29.866666 192-192L746.666667 341.333333l-29.866667 29.866667 136.533333 136.533333z" fill="#BE163D" p-id="2891"></path> </svg>
                 </div>
                 {% elsif block.settings.method == 'pay' %}
                  <div class="promotionButton promotionButton_pay" data-variants="{{ block.settings.promotionProduct.variants[0].id }}">
                    {{ block.settings.button1 }}
                    <svg t="1663833321925" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2890" width="27" height="27"> <path d="M853.333333 507.733333H128v42.666667h733.866667l-145.066667 145.066667 29.866667 29.866666 192-192L746.666667 341.333333l-29.866667 29.866667 136.533333 136.533333z" fill="#BE163D" p-id="2891"></path> </svg>
                  </div>
                 {% elsif block.settings.method == 'details' %}
                  <div class="promotionButton">
                   <a href="{{ block.settings.promotionProduct.url }}">
                     {{ block.settings.button1 }}
                    <svg t="1663833321925" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2890" width="27" height="27"> <path d="M853.333333 507.733333H128v42.666667h733.866667l-145.066667 145.066667 29.866667 29.866666 192-192L746.666667 341.333333l-29.866667 29.866667 136.533333 136.533333z" fill="#BE163D" p-id="2891"></path> </svg>
                   </a>
                  </div>
                 {% endif %}
               
               {% else %}
                <div class="promotionButton_soldOut">Sold out</div>
               {% endif %}
              
               
                </div>
            </div>
        {% endfor %}
      </div>
    </div>
</div>

2.利用js来调用购物车 API ,quantity是您要添加的变体数量,id是该变体的变体 ID。您可以通过在数组中附加更多对象来将多个变体添加到购物车items。

<script>
  // 点击添加购物车产品数量
$(".promotion_add").click(function() {
    var valNumber = Number($(this).siblings(".promotion_num").val());
    valNumber++;
    $(this).siblings(".promotion_num").val(valNumber)
    $(this).siblings(".promotion_reduce").removeAttr("disabled")
})
// 点击减少购物车产品数量
$(".promotion_reduce").click(function () {
    var valNumber = Number($(this).siblings(".promotion_num").val());
         if (valNumber <= 1) {
             $(this).attr("disabled","disabled");
         }else if(valNumber > 1){
            valNumber--;
            $(this).siblings(".promotion_num").val(valNumber)
         }
         
})
//input输入数量
$('#promotion_num').bind('input porpertychange' ,function(){
    var inputNum = Number($(this).val())
$(this).val(inputNum)
if(inputNum > 1) {
    $(this).siblings(".promotion_reduce").removeAttr("disabled")
}else {
    $(this).siblings(".promotion_reduce").attr("disabled","disabled");
}
})
//点击加入购物车
$('.promotionButton_cart').click(function(){
  var quantity = $(this).siblings('.promotionProduct_list_num').children(".promotion_num").val()
  var variants  = $(this).attr('data-variants')
  let formData = {
 'items': [{
  'id': variants,
  'quantity': quantity
  }]
};
 
fetch(window.Shopify.routes.root + 'cart/add.js', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify(formData)
})
.then(response => {
  window.location.href='/cart'
  return response.json();
})
.catch((error) => {
  console.error('Error:', error);
});
})
//点击去支付页面
$('.promotionButton_pay').click(function(){
  var quantity = $(this).siblings('.promotionProduct_list_num').children(".promotion_num").val()
  var variants  = $(this).attr('data-variants')
  let formData = {
 'items': [{
  'id': variants,
  'quantity': quantity
  }]
};
 
fetch(window.Shopify.routes.root + 'cart/add.js', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify(formData)
})
.then(response => {
 window.location.href='/checkout'
  return response.json();
})
.catch((error) => {
  console.error('Error:', error);
});
})
</script>

3.这个是Shopify的liquid语法 ,后台配置

{% schema %}
{
"name": "Promotional Discount",
"settings": [
    {
"type": "text",
"id": "promotionDiscount_title",
"label": "title"
},
{
"type": "text",
"id": "promotionDiscount_id",
"label": "Id"
},
    {
"type": "color",
"id": "button",
"label": "button Color",
"default": "#fff"
},
{
"type": "color",
"id": "borderColor",
"label": "button border Color",
"default": "#fff"
},
{
"type": "color",
"id": "textColor",
"label": "button text Color",
"default": "#fff"
}
],
"blocks" : [
{
"type": "promotionProduct",
"name": "promotion_product",
"settings":[
{
"type": "text",
"id": "promotion",
"label": "promotion text"
},
{
"type": "product",
"id": "promotionProduct",
"label": "product"
},
{
"type": "text",
"id": "button",
"label": "go to details button"
},
{
"type": "text",
"id": "button1",
"label": "go to cart button"
},
{
"type": "text",
"id": "button2",
"label": "go to pay button"
},
{
      "type": "select",
      "id": "method",
      "label": "Select button method",
      "options": [
        {
          "value": "details",
          "label": "go to details"
        },
        {
          "value": "cart",
          "label": "go to cart"
        },
        {
          "value": "pay",
          "label": "go to pay"
        }
      ],
      "default": "cart"
    }
]
}
],
"presets": [{
"name": "Promotional Discount"
}]
}
{% endschema %}

标签:shopify,settings,购物车,二次开发,promotion,label,type,id,block
From: https://blog.csdn.net/withkai44/article/details/139268565

相关文章

  • CATIA二次开发VBA入门(4)——进程外开发环境搭建,vb.net在Visual Studio中开发,创建圆柱曲
    目录引出vb.net和vb6.0进程外开发环境搭建vb.net开发环境搭建《CATIA二次开发技术基础》模板添加宏库引用vs开发环境初步vs中的立即窗口对象浏览器建立模板案例:创建一堆圆柱曲面第一步:录制宏第二步:代码精简第三步:for循环改造第四步:人机交互改造窗口模态设置导出窗口......
  • 基于Python与水星二代摄像头的二次开发
    第一章Videocapture的正确使用大家好!关于摄像头的基本调用,相信大家以及初步学会了。我们买来这个摄像头,本来就是想着自己使用,进行二次开发的。但是大家根据OpenCV的调用函数Videocapture(),发现根本无法调用,这是为什么?首先,判断外接摄像头能否调用,我们有两个非常简单的方法:方......
  • QGIS开发笔记(三):Windows安装版二次开发环境搭建(下):将QGis融入QtDemo,添加QGis并加载tif遥
    前言  使用QGis的目的是进行二次开发,或者说是融入我们的应用(无人车、无人船、无人机),本片描述搭建QGis二次基础开发环境,由于实在是太长了,进行了分篇:上半部分:主要是安装好后,使用QtCreator可以使用QGIs的apps下的Qt使用对应的编译器编译不带qgis的空工程。下半部分:在上半......
  • Excalidraw画板调研-二次开发
    最近刚入职一家公司,主管让我研究一下Excalidraw。有一个需求需要用到画板,Excalidraw是开源的,某些功能如果通过传参无法做到,就需要二次开发。目前遇到几个困难:1.如果通过传参来实现,Excalidraw是基于react的,而我们的项目是VUE框架,就无法直接使用基于react的Excalidraw。虽然后面......
  • SUMER UI3.0组件库,基于Uni-app前端框架!一端开发,多端运行!本组件库可快速二次开发各种类
    sumer-ui介绍基于uView微信小程序UI组件库,兼容vue3。本插件是SUMER组件库,只提供组件库源码下载(不包含模板源码),本组件库可快速二次开发各种类别各行业模板,包括:商城、视频、直播、聊天、支付、新闻、社区、地图、导航、出行、社区、博客、新闻、游戏、影视、订票、广告等,......
  • CATIA二次开发VB入门(1)——认识catia二次开发,宏的录制、回放和编辑
    目录引出认识CATIA二次开发刘瑞欣vb程序设计教程Excel中的vba开发catia中的vba开发宏的录制、回放和编辑宏代码精简画圆柱阵列宏Macro文件的3种类型宏的保存:文件夹,项目,catia文件宏加入到工具条中插曲:工具条的恢复总结发生肾么事了??鼠标中键旋转不了解决:特征树不显示......
  • CAD二次开发(1)- 初步和CAD进行通讯交互
    1.安装CAD和VS我这里CAD选择的是2022版本VisualStudioEnterprise2022(64位)2.在VS上创建项目2.1创建类库类型的项目这里要特别注意,我们选择的类库是需要最终生成.dll文件的,经过我的测试,只有上图的模板可以使用,具体原因我需要后面去探索。2.2引入CAD的相......
  • OBS直播二次开发_投屏
    相关文章链接OBS直播二次开发_OBS直播软件介绍OBS直播二次开发_windows版本编译OBS直播二次开发_简单教程OBS直播二次开发_界面修改相关OBS直播二次开发_切换视口水平和垂直OBS直播二次开发_设置快捷键OBS直播二次开发_添加新的uiqt界面OBS直播二次开发_调用get......
  • SOLIDWORKS二次开发服务商 慧德敏学
    SOLIDWORKS是一套三维设计软件,采用特征建模、变量化驱动可方便地实现三维建模、装配和生成工程图。SOLIDWORKS软件本身所具有的交互方式,可以使用户对已生成模型的尺寸、几何轮廓和相互约束关系随时进行修改,而不需要编程。但要实现设计意义上的变量化绘图和系列化设计,需要......
  • vue3+ts购物车demo
    <template><div><h1>ShoppingCart</h1><button@click="addItem">AddItem</button><button@click="deleteSelectedItems">DeleteSelectedItems</button><button@c......