【问题标题】:give select value to php wodpress为 php wordpress 赋予选择值
【发布时间】:2022-01-04 17:38:02
【问题描述】:

我需要从这个输入中给出价值:

<select name="sample" id="select_rent" class="select" style="border-radius: 6px; padding: 10px; padding-right:40px;">
    <option value="<?php   echo get_post_meta(get_the_ID(), 'rent_price_1', true ); ?>">1</option>
    <option value="<?php   echo get_post_meta(get_the_ID(), 'rent_price_3', true ); ?>">3</option>
    <option value="<?php   echo get_post_meta(get_the_ID(), 'rent_price_6', true ); ?>">6</option>
    <option value="<?php   echo get_post_meta(get_the_ID(), 'rent_price_12', true ); ?>">12</option>
    <option value="<?php   echo get_post_meta(get_the_ID(), 'rent_price_24', true ); ?>">24</option>
</select>

到这个php代码(作为变量select_value):

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 1000, 1);
function add_custom_price( $cart ) {
    // This is necessary for WC 3.0+
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Avoiding hook repetition (when using price calculations for example | optional)
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ) {
        $cart_item['data']->set_price( $select_value );
    }
}

我有这个原始的 ajax,不知道该怎么办:

 <script type="text/javascript">
                jQuery(function ($) {
                    $('#select_rent').change(function () {
                        var workselected = $(this).val();
                        $.ajax({
                            method: "POST",
                            url: "url",
                            data: $('#select_rent').val(),
                            success: function (data) {
                                console.log($('#select_rent').val());
                            }
                        });
                    });
                })
    </script>

ajax 不起作用400 (Bad Request),我不知道如何为 php 赋予价值。有人可以帮忙吗?

我修复了 ajax,现在它控制台记录值。我怎样才能把它交给php?

【问题讨论】:

    标签: php ajax wordpress woocommerce


    【解决方案1】:

    将您的 ajax url 更改为:admin_url('admin-ajax.php'); 并带有特定的操作名称,例如:get_data

    $.ajax({
         method: "POST",
         url: "<?php echo admin_url('admin-ajax.php'); ?>",
         data: {
             action:'get_data' ,
             select_rent : $('#select_rent').val(),
         },
         success: function (data) {
               console.log($('#select_rent').val());
         }
    });
    

    这会将您的数据发送到 WordPress ajax 处理程序,WordPress 将调用您已经在 function.php 或 someWhere else 中定义的操作。

    add_action( 'wp_ajax_nopriv_get_data', 'my_ajax_handler' );
    add_action( 'wp_ajax_get_data', 'my_ajax_handler' );
    
    function my_ajax_handler() {
       //whatever you want to do;
       return $result ;
    }
    

    现在在//whatever you want to do; 中,您必须以某种方式保存通过ajax 调用接收到此函数的值。例如,您可以执行以下操作:

    • 将其保存在数据库中
    • 在会话中保存
    • 保留在设置中
    • 在redis中持久化

    现在是时候定义你的最终行动了:

    add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 1000, 1);
    function add_custom_price( $cart ) {
        // fetch previously saved data and do whatever you want with it.
    }
    

    【讨论】:

    • 帮助您的回复!我可以只对数据进行 json 编码并使用它吗?我想这样给我们:$_POST['data'] 但如果我回应它 - 它是空的。
    • 你必须回显 select_rent 而不是 data 。是的,您可以在my_ajax_handler 函数中获取带有 post 参数的 select_rent。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-17
    • 1970-01-01
    • 1970-01-01
    • 2013-04-05
    • 2019-12-17
    • 1970-01-01
    相关资源
    最近更新 更多