【问题标题】:Generate JS variable array through data-set from woocommerce HTML variation通过来自 woocommerce HTML 变体的数据集生成 JS 变量数组
【发布时间】:2020-02-18 01:42:27
【问题描述】:

这是原版 JS 代码的一部分 =

var prices = [100, 536, 2368];

但是上面是硬编码的。我希望它们通过 HTML 选择下拉菜单:

<select class="license_type" name="license_type" id="license_type">
    <option value="license" data-price="500">Single Site License</option>
    <option value="license" data-price="700">5 Site License</option>
    <option value="license" data-price="1400">Developers License</option>
</select>

这里数据集中的值是在 woocommerce 中动态生成的:

数据集值将根据 woocommerce 产品动态生成。

我们如何确保数组var prices 始终根据可变价格在每个产品页面上动态更新?

更新→

这里回答的这个问题有联系→ Woocommerce variation dropdown option tag add some dataset

(这就是我们在选项标签的 HTML 中引入数据集的方式)

最终在浏览器中呈现如下:

【问题讨论】:

  • 在一个选择字段上,每个选项的值通常应该是不同的……现在有了提供的代码和细节,人们怎么能猜出你的问题呢?
  • 先生,更新了问题让我知道它是否改进了问题?
  • 目前有硬编码的香草JS →var prices = [100, 536, 2368]; but I want that this array should generate from data-price in options HTML.
  • 即使现在我也无法表达这个问题,我渴望改进这个问题。

标签: javascript jquery woocommerce dataset hook-woocommerce


【解决方案1】:
    add_action( 'wp_footer', 'webtoffee_product_attr_filter' );

    function webtoffee_product_attr_filter() {
       if ( !is_product() ) {
          return;
       }
       ?>
          add_action('wp_footer', 'webtoffee_checkout_shipping_filter_it_states');

    function webtoffee_checkout_shipping_filter_it_states() {
        if (!is_product()) {
            return;
        }
        ?>

       <script>

        var x = document.getElementById("color");
        var prices = new Array();;
        var i;
        for (i = 0; i < x.length; i++) {
            if(x.options[i].getAttribute('data-custom')){
            prices.push(x.options[i].getAttribute('data-custom'));
        }else{
            prices.push("0");
        }
        };
        console.log(prices);
        </script>

    <?php
}

试试这个

【讨论】:

  • 这里的颜色是什么?
  • 有没有办法让最终的数组像 [0, 69, 79]。首先应该是 0 来照顾选择一个选项部分。
  • 您知识渊博,先生。在这个阶段需要达到多少个月/几年?
  • 你也能解决这个问题吗→stackoverflow.com/questions/58514945/…
  • 先生,您能帮我吗here
猜你喜欢
  • 1970-01-01
  • 2019-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-18
  • 1970-01-01
  • 1970-01-01
  • 2021-09-16
相关资源
最近更新 更多