业务经理

微信扫描以上二维码

028-61286886

技术咨询

用VUE替换SKU多属性动态价格、总价、数量等

迅睿框架 更新 2024-03-04 15:49:11 小昕

用VUE替换SKU多属性动态价格、总价、数量等

每选择一个SKU都动态展示价格,自动组合好所选择的SKU码,、图片、采购多件实时计算价格展示给用户

image

<html>
<head>
  <title>SKU Component Demo with Stepper</title>
  <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
</head>
<body>
  <div id="app">
    <sku-component :sku-data="skuData"></sku-component>

  </div>

  <script>
    JS在下方
  </script>
</body>
</html>


JS逻辑

// SKU组件定义
    const SkuComponent = {
      props: ['skuData'],
      data() {
        return {
          groups: [],
          options: [],
          selectedOptions: [],
          selectedSku: null,
          quantity: 1 // 添加数量属性
        };
      },
      mounted() {
        this.initSkuData();
      },
      methods: {
        initSkuData() {
          this.groups = this.skuData.group;
          this.options = this.skuData.name;
          this.selectedOptions = new Array(this.groups.length).fill(0);
          this.updateSelection();
        },
        updateSelection() {
          const skuKey = this.selectedSkuKey;

          this.selectedSku = this.skuData.value[skuKey];
        },
        incrementQuantity() {
          this.quantity++;
        },
        decrementQuantity() {
          if (this.quantity > 1) {
            this.quantity--;
          }
        },
        redirectToABC() {
          const url = `http://www.abc.com?quantity=${this.quantity}?abc=${this.selectedSkuKey}`;
          window.location.href = url;
        }
      },
      computed: {
        totalPrice() {
          if (this.selectedSku) {
            const price = parseFloat(this.selectedSku.price);
            return (price * this.quantity).toFixed(2);
          }
          return 0;
        },
        selectedSkuKey() {
         return this.selectedOptions
         .map((optionIndex, groupIndex) => `${groupIndex}_${optionIndex}`)
         .join('_');
        }
      },
      template: `
        <div>
          <div v-for="(group, index) in groups" :key="index">
            <h3>{{ group }}</h3>
            <div v-for="(name, optionIndex) in options[index]" :key="optionIndex">
              <label>
                <input
                  type="radio"
                  :value="optionIndex"
                  v-model="selectedOptions[index]"
                  @change="updateSelection()"
                />
                {{ name }}
              </label>
            </div>
          </div>
          <div v-if="selectedSku">
            <h4>SKU Details</h4>
            <p>Selected SKU: {{ selectedSkuKey }}</p>
            <p>Image: {{ selectedSku.image }}</p>
            <p>Price: {{ totalPrice }}</p>
            <p>Quantity:
              <button @click="decrementQuantity">-</button>
              <input type="number" v-model.number="quantity" min="1" />
              <button @click="incrementQuantity">+</button>
            </p>
            <p>SN: {{ selectedSku.sn }}</p>
            <button @click="redirectToABC">提交</button>
          </div>
        </div>
      `
    };

    // 创建Vue实例,将SKU组件作为全局组件注册
    new Vue({
      el: '#app',
      components: {
        SkuComponent
      },
      data() {
        return {
          skuData: {
             group: [{loop $price_sku['group'] $gid $gname}{if $gname}'{$gname}',{/if}{/loop}],
                        name: [{loop $price_sku['group'] $gid $gname}{if $gname}[{php $i=0;}{loop $price_sku['name'][$gid] $vid $vname}'{$vname}',{php $i=1;}{/loop}],{/if}{/loop}],
                        value: {{if $price_sku}{loop $price_sku['value'] $i $v}'{$i}': {image: '{dr_thumb($v.image, 100, 100)}',price: '{intval($v.price)}',quantity: '{$v.quantity}',sn: '{$v.sn}',yprice: '{$v.yprice}',},{/loop}{/if}},
                        }
          }
        };
      }
    });


方案

  • 马拉糕 #1 · 2024-01-02 21:50:22
    看看看看看看
  • 微信20329916 #2 · 2024-03-04 15:49:11
    • 看看看看看看
    代码语言

迅睿系统漏洞

导入微信文章
导入微信文章(2026-09-15)
优化排版缺少文字情况
GEO系统
GEO系统(2026-09-15)
运营客户端,防止误触阴影处关闭对话框
独立站群系统
独立站群系统(2026-09-14)
子站独立部署时修复同步附件失败问题
在线客服
在线客服(2026-09-13)
修复删除会话报错问题 删除会话时联动删除聊天图片
在线考试竞赛系统
在线考试竞赛系统(2026-09-12)
增加投票功能 考试功能拓展 注意:更新后需要执行系统更新,更新数...
网上信箱
网上信箱(2026-09-12)
修复新版本兼容问题
GeoSeo收录宝
GeoSeo收录宝(2026-09-12)
1 优化Update.php文件,解决版本在线升级不完整问题 2 ...
GeoSeo工具箱
GeoSeo工具箱(2026-09-11)
修正历史记录推送
微信系统
微信系统(2026-09-11)
增加数据统计总览界面 账号管理增加服务和公众号的区别 菜单管理支持更...
答题系统
答题系统(2026-09-11)
一站式在线答题系统,支持真题考试、题库练习和错题本,适合培训、考证、...