【问题标题】:Return original value jquery / php返回原始值 jquery / php
【发布时间】:2021-10-01 07:35:47
【问题描述】:

我正在使用此代码来运行我的额外按钮。它将原始价格值更改为自定义值。是否可以再次单击名为“afterclickbutton”的按钮以获取原始值的价格 - 意思是获取正常价格并重置整个东西,因此可以再次单击并获取自定义值,然后单击并获取正常价格值等等等?

// Přidání bestpprice' vlastníhopole $bestprice = get_post_meta($product->get_id(), '_bestprice', true);

if( ! empty($bestprice) ):

$bestprice = wc_get_price_to_display( $product, array( 'price' => $bestprice ) );
$reg_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) );
$range = wc_format_sale_price( $reg_price, $bestprice );
?>
<!-- The button and hidden field --> 
<div class="bestprice-wrapper"><br>
    <a href="" class="get_bestprice button alt" id="get_bestprice"><?php _e('Aktivovat lepší cenu');?></a>
    <input type="hidden" name="bestprice" id="bestprice" class="bestprice" value="" />
  </div>
<!-- The jQuery code --> 
<script type="text/javascript">
    (function($){
        var b = '<?php echo $bestprice; ?>',
            i = 'input[name=bestprice]',
            p = 'p.price',
            r = '<?php echo $range; ?>',
            t = 'a#get_bestprice'
            u = true;
        $(t).click( function(e){
            e.preventDefault();
            if(u){
                $(p).html(r);  // Replacing price with the range
                $(i).val(b);  // Set the best price in hidden input field
                $(t).text(‚Activated'); // change button text
                $(t).removeClass('alt'); // Remove button 'alt' class for styling
                                  $(t).removeClass('get_bestprice')
                                          $(t).addClass(‚afterclickbutton')
                                  u = false;   // Disable button


            }
        });
    })(jQuery);
</script>
<?php
endif; }

【问题讨论】:

  • 可能有更好的方法,但我会让初始页面绘制将“旧”价格存储在其他地方,也许在隐藏的 div 中,然后在用户按下 afterclickbutton 时恢复该值,并将按钮类设置回get_bestprice,以便您可以有效地在两者之间切换。

标签: javascript php html jquery reset-button


【解决方案1】:

更新 2:

if( ! empty($bestprice) ):

$bestprice = wc_get_price_to_display( $product, array( 'price' => $bestprice ) );
$reg_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) );
$range = wc_format_sale_price( $reg_price, $bestprice );
?>
<!-- The button and hidden field --> 
<div class="bestprice-wrapper"><br>
    <a href="" class="get_bestprice button alt" id="get_bestprice"><?php _e('Aktivovat lepší cenu');?></a>
    <input type="hidden" name="bestprice" id="bestprice" class="bestprice" value="" />
  </div>
<!-- The jQuery code --> 
<script type="text/javascript">
(function($){
        var b = '<?php echo $bestprice; ?>',
            o = $('p.price').clone().find('del').remove().end().text().trim().replace(/[^\d.,]/g, ''),
            i = 'input[name=bestprice]',
            p = 'p.price',
            r = '<?php echo $range; ?>',
            t = 'a#get_bestprice'
            u = true;
        $(t).click( function(e){
            e.preventDefault();
            if(u){
                if ($(p).attr('type') == undefined){ $(p).attr('type','_regular_price') };
                if ($(`${p}[type="_best_price"]`).length == 0) {
                    $(p).hide().after(`<p class="price" type="_best_price">${r}</p>`); // add best price and hide regulare
                }else{
                    $(`${p}[type="_regular_price"]`).hide();
                    $(`${p}[type="_best_price"]`).show();
                }
                //$(p).html(r);  // Replacing price with the range
                $(i).val(b);  // Set the best price in hidden input field
                $(t).text('Activated') // change button text
                .removeClass('alt get_bestprice') // Remove button 'alt' class for styling
                .addClass('afterclickbutton');
                u = false;   // Disable button
            }else{
                $(`${p}[type="_best_price"]`).hide();
                $(`${p}[type="_regular_price"]`).show();
                $(i).val(o);
                $(t).text('Active best price')
                .addClass('alt get_bestprice')
                .removeClass('afterclickbutton');
                u = true; 
            }
        });
    })(jQuery);
</script>
<?php
endif; }

【讨论】:

  • 感谢您的回答。不幸的是,这个技巧没有奏效。我试图用 '_get_regular_price' 替换 'original_value',这是一个在 woocommerce 的产品价格设置中给出产品经典价格的类。不幸的是,即使发生了这种变化,价格也没有恢复,始终保持在“最佳价格”,如以下链接所示:testwpcena.atwebpages.com/product/akzm-8480-ix
  • 使用此更新,之前的最后一个脚本有问题。
  • 你是个了不起的人 :) 谢谢你
  • 谢谢,不客气 :)
  • 嗨,我刚刚看到这个''JQuery 添加属性功能不起作用''你帮助解决了这个问题。而我的想法是 - 是否有可能对我的 btn 实施这样的事情?我想要实现的是让客户可以选择更好的价格和带有 bestprice 按钮的免费礼物(来自这个主题)作为礼物我使用插件“PW Woocommerce Advanced Gift Rules by Proword”-我可以提供来自 main.php 等示例页面的数据testwpcena.atwebpages.com/product/akzm-8480-ix 是否可以向按钮添加功能以禁用启用礼物? @YounesBennour
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-05
  • 2021-10-22
  • 1970-01-01
  • 1970-01-01
  • 2011-06-01
相关资源
最近更新 更多