我在结帐页中放置了一个选择框,如下所示。

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_decrept_reward”功能中犯了一些错误。我不知道怎么做。
如有任何帮助,我们将不胜感激。

最佳答案

当出现一些错误和缺少的部分时,您需要使其略有不同才能正常工作:

// 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文件中。测试和工作
php - Woocommerce结帐页面中使用AJAX和Fee API的动态折扣-LMLPHP

07-24 09:16