I have put a select box in the checkout page like this.

我在结帐页面中放了一个选择框,就像这样。

function rx_wc_reward_points_check() {

    $reward_points = '<select class="rx-rewad-points" id="rx-redemption-points">
        <option value="1250">$25.00 Off (1250 Points) </option>
        <option value="2500">$50.00 Off (2500 Points) </option>
        <option value="5000">$100.00 Off (5000 Points) </option>
        <option value="7000">$150.00 Off (7000 Points) </option>
    </select>';
    $reward_points .= '<a class="button alt" name="rx_reward_points_btn" id="rx_reward_points" value="Apply" data-value="Reward Points">Apply Now</a>';
    echo $reward_points;
}
add_action( 'woocommerce_checkout_after_customer_details', 'rx_wc_reward_points_check', 10, 0 ); 

After that i have added this to the function file for

之后我将其添加到函数文件中

function rx_wc_deduct_reward() {
    global $woocommerce;
    $reward_value = $_POST['rewardpoints'];
    WC()->cart->add_fee( 'Fee', -$reward_value );

    echo 'success';
    exit;
}
add_action( 'wp_ajax_rx_wc_deduct_reward', 'rx_wc_deduct_reward' );
add_action( 'wp_ajax_nopriv_rx_wc_deduct_reward', 'rx_wc_deduct_reward' );

This is using for the ajax part

这用于ajax部分

jQuery(document).ready( function() {
    jQuery('#rx_reward_points').on('click', function() {
        var selectedrewad = jQuery( "#rx-redemption-points option:selected" ).val();
        jQuery.ajax({
         type : "post",
         url : ajax_var.ajaxurl,
         data : { action: "rx_wc_deduct_reward", rewardpoints : selectedrewad },
         success: function(response) {
            console.log(response)
            if(response == "success") {
               console.log('done');
            }
            else {
               console.log("Your vote could not be added")
            }
         }
        });
    })
})

But it is not working. I have done some mistake in the "rx_wc_deduct_reward" function. I cant figure out how to do this.

但它没有用。我在“rx_wc_deduct_reward”函数中做了一些错误。我无法弄清楚如何做到这一点。

Any help is appreciated.

任何帮助表示赞赏。

1 个解决方案

#1


2

You need to make it slight different to get it work as you get some errors and missing parts:

当你遇到一些错误和遗漏部分时,你需要让它略有不同才能使它工作:

// Displaying a select field and a submit button in checkout page
add_action( 'woocommerce_checkout_after_customer_details', 'rx_wc_reward_points_check', 10, 0 );
function rx_wc_reward_points_check() {

    echo '<select class="rx-rewad-points" id="rx-redemption-points">
        <option value="25">' . __("$25.00 Off (1250 Points)", "woocommerce" ) . '</option>
        <option value="50">' . __("$50.00 Off (2500 Points)", "woocommerce" ) . '</option>
        <option value="100">' . __("$100.00 Off (5000 Points)", "woocommerce" ) . '</option>
        <option value="150">' . __("$150.00 Off (7000 Points)", "woocommerce" ) . '</option>
    </select>
    <a class="button alt" name="rx_reward_points_btn" id="rx_reward_points" value="Apply" data-value="Reward Points">Apply Now</a>';
}

// jQuery - Ajax script
add_action( 'wp_footer', 'rx_wc_reward_points_script' );
function rx_wc_reward_points_script() {
    // Only checkout page
    if ( ! is_checkout() ) return;
    ?>
    <script type="text/javascript">
    jQuery( function($){
        $('#rx_reward_points').on('click', function() {
            $.ajax({
                type: "post",
                url:  wc_checkout_params.ajax_url,
                data: {
                     'action' : 'rx_wc_deduct_reward',
                     'rewardpoints' : $("#rx-redemption-points").val()
                },
                success: function(response) {
                    $('body').trigger('update_checkout');
                    console.log('response: '+response); // just for testing | TO BE REMOVED
                },
                error: function(error){
                    console.log('error: '+error); // just for testing | TO BE REMOVED
                }
            });
        })
    })
    </script>
    <?php
}

// Wordpress Ajax code (set ajax data in Woocommerce session)
add_action( 'wp_ajax_rx_wc_deduct_reward', 'rx_wc_deduct_reward' );
add_action( 'wp_ajax_nopriv_rx_wc_deduct_reward', 'rx_wc_deduct_reward' );
function rx_wc_deduct_reward() {
    if( isset($_POST['rewardpoints']) ){
        WC()->session->set( 'custom_fee', esc_attr( $_POST['rewardpoints'] ) );
        echo true;
    }
    exit();
}

// Add a custom dynamic discount based on reward points
add_action( 'woocommerce_cart_calculate_fees', 'rx_rewardpoints_discount', 20, 1 );
function rx_rewardpoints_discount( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Only for targeted shipping method
    if (  WC()->session->__isset( 'custom_fee' ) )
        $discount = (float) WC()->session->get( 'custom_fee' );

    if( isset($discount) && $discount > 0 )
        $cart->add_fee( __( 'Reward discount', 'woocommerce' ), -$discount );
}

Code goes in function.php file of your active child theme (or active theme). Tested and works

代码位于活动子主题(或活动主题)的function.php文件中。经过测试和工作

更多相关文章

  1. Jquery中的队列函数quene()、dequene()、clearQuene()
  2. Internet Explorer导致无效的真实性令牌错误
  3. DataTables警告:table id = DataTables_Table_0 - Ajax错误。有
  4. 如何在使用jquery验证和自定义错误放置时清除错误
  5. Ajax调用php脚本返回404错误
  6. Ajax_04之jQuery中封装的Ajax函数
  7. Jquery验证插件,获取错误字符串
  8. 怎么用js或jquery把一个函数b绑定到另一个函数a之后执行
  9. 与symfony一起使用ajax时出现500内部服务器错误

随机推荐

  1. PHP 消息队列 Kafka 使用
  2. PHP 并发场景的 3 种解决方案
  3. PHP如何实现简单的Socket
  4. PHP结合AJAX实现搜索提示功能
  5. PHP如何将中文转英文
  6. 深入理解依赖注入是如何实现解耦
  7. PHP中的危险函数你知道吗?
  8. 【 callable-fake】虚构你的可调用函数以
  9. PHP使用Redis常见7种使用场景
  10. php如何去除空数组