Woocommerce结帐页面中使用AJAX和Fee API的动态折扣 [英] Dynamic discount using AJAX and Fee API in Woocommerce checkout page

查看:65
本文介绍了Woocommerce结帐页面中使用AJAX和Fee API的动态折扣的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这样的结帐页面中放置了一个选择框.

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 ); 

之后,我将其添加到

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' );

这用于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")
            }
         }
        });
    })
})

但是它不起作用.我在"rx_wc_deduct_reward"函数中犯了一些错误.我不知道该怎么做.

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.

感谢您的帮助.

推荐答案

在遇到一些错误和遗漏的部分时,您需要使其稍有不同才能使其工作:

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($){
        if (typeof wc_checkout_params === 'undefined')
            return false;

        $('#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 );
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试并有效

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

这篇关于Woocommerce结帐页面中使用AJAX和Fee API的动态折扣的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆