使用WooCommerce_Cart_Calculate_Fees操作时,结账被BLOCUI阻止

时间:2019-04-08 作者:thepi

我正在使用以下代码添加结帐时的自定义费用:

function woocommerce_custom_surcharge() {
global $woocommerce;
if ( is_admin() && ! defined( \'DOING_AJAX\' ) )
    return;

if ($_REQUEST[\'s-baby-seats\'] || $_REQUEST[\'s-taxi-return-id\']) {
    global $c_seats;
    global $c_return_fare;
    $baby_seats_fee = intval($c_seats[1]);
    if ($_REQUEST[\'s-taxi-return-id\']) {
        $return_fee = intval($c_return_fare);
    } else {
        $return_fee = 0;
    }
    $surcharge = $baby_seats_fee + $return_fee;
    $surcharge_label = esc_html__(\'Extra + Return\', \'berrytaxiplon\');
    $woocommerce->cart->add_fee( $surcharge_label, $surcharge );
}
} 
add_action( \'woocommerce_cart_calculate_fees\',\'woocommerce_custom_surcharge\' );
费用按预期添加,但结帐的order\\u review部分仍被blockUI禁用。我看不到任何php(“WP\\u DEBUG”为true)或js控制台错误。我不知道如何调试这种行为。

如果不是我的代码,只需添加$woocommerce->cart->add_fee( \'test\', 5 ); 订单审核未被阻止/禁用,用户可以正常下单。

我的代码中有什么可能导致这种情况,以及如何解决这个问题?谢谢

编辑1:

我更改了我的函数,并删除了对此的条件和$\\u请求:

function woocommerce_custom_surcharge() {
global $woocommerce;
if ( is_admin() && ! defined( \'DOING_AJAX\' ) )
    return;

    global $c_seats;
    global $c_return_fare;
    $baby_seats_fee = intval($c_seats[1]);
    $return_fee = intval($c_return_fare);
    $surcharge = $baby_seats_fee + $return_fee;
    $surcharge_label = esc_html__(\'Extra + Return\', \'berrytaxiplon\');
    $woocommerce->cart->add_fee( $surcharge_label, $surcharge );
}
add_action( \'woocommerce_cart_calculate_fees\',\'woocommerce_custom_surcharge\' );
现在blockUI在几秒钟内就被删除了,但是费用,虽然它一开始呈现正确,但js会将其设置为0。

编辑2:

原来我一直都在使用会话。通过此帖子解决:Add a cart fee from an url variable in Woocommerce

1 个回复
最合适的回答,由SO网友:LoicTheAztec 整理而成

You can not keep posted values directly in your hooked function 因为它不存储在任何地方,当客户离开购物车页面时,您会将其松开。所以blockUI 与此无关。

有两种方法可以保存发布的值:

如果自定义数据发布在Woocommerce单一产品页面之外,则在Woocommerce会话上而且您的代码有点过时,因为$global woocommerce 不再需要,并替换为WC(). 现在开始woocommerce_cart_calculate_fees 钩子函数,代码中缺少一个变量$cart (WC\\U Cart对象)

您的代码实际上不可测试,因为我们不知道以下内容:

global $c_seats;
global $c_return_fare;
您可以尝试以下两种方法之一:

1) 数据发布在Woocommerce单一产品页面之外:

// Add custom cart item data on add to cart
add_filter( \'init\', \'set_custom_data_requested_to_wc_sessions\', 10, 3 );
function set_custom_data_requested_to_wc_sessions() {

    if( isset($_REQUEST[\'s-baby-seats\']) || isset($_REQUEST[\'s-taxi-return-id\']) ) {
        // Enable Woocommerce sessions (if not done yet)
        if ( ! WC()->session->has_session() ) {
            WC()->session->set_customer_session_cookie( true );
        }

        $session_data = []; // initializing

        if( isset($_REQUEST[\'s-baby-seats\']) && ! empty($_REQUEST[\'s-baby-seats\']) ) {
            // Add the dropdown value as custom cart item data
            $session_data[\'baby_seats\'] = wc_clean( esc_attr($_REQUEST[\'s-baby-seats\']) );
        }

        if( isset($_REQUEST[\'s-taxi-return-id\']) && ! empty($_REQUEST[\'s-taxi-return-id\']) ) {
            // Add the dropdown value as custom cart item data
            $session_data[\'return_id\'] = wc_clean( esc_attr($_REQUEST[\'s-taxi-return-id\']) );
        }

        // Set the data to custom wc_sessions
        if( sizeof($session_data) > 0 ) {
            WC()->session->set(\'fee_data\', $session_data);
        }
    }
}

// Add a fee
add_action( \'woocommerce_cart_calculate_fees\',\'add_custom_surcharge\', 10, 1 );
function add_custom_surcharge( $cart ) {
    if ( is_admin() && ! defined( \'DOING_AJAX\' ) )
        return;

    global $c_seats, $c_return_fare;

    $surcharge = 0; // Initializing
    $fee_data  = WC()->session->get(\'fee_data\'); // Get the data from Woocommerce session

    if( isset($fee_data[\'baby_seats\']) || isset($fee_data[\'return_id\']) ) {
        $baby_seats_fee  = isset($fee_data[\'baby_seats\']) ? intval($c_seats[1]) : 0;
        $return_id_fee =  isset($fee_data[\'return_id\']) && $fee_data[\'return_id\'] ? intval($c_return_fare) : 0;

        $surcharge = $baby_seats_fee + $return_id_fee; // Add to the fee cost
    }

    // Add the calculated fee
    if ( $surcharge > 0 ) {
        $cart->add_fee( esc_html__(\'Extra + Return\', \'woocommerce\'), $surcharge );
    }
}
代码进入功能。活动子主题(或活动主题)的php文件。它应该会起作用。

在单个产品页面中添加到购物车上发布的数据(基于购物车项目):

// Add custom cart item data on add to cart
add_filter( \'woocommerce_add_cart_item_data\', \'filter_add_cart_item_data_callback\', 10, 3 );
function filter_add_cart_item_data_callback( $cart_item_data, $product_id, $variation_id) {
    if( isset($_REQUEST[\'s-baby-seats\']) && ! empty($_REQUEST[\'s-baby-seats\']) ) {
        // Add the dropdown value as custom cart item data
        $cart_item_data[\'s-baby-seats\'] = wc_clean( esc_attr($_REQUEST[\'s-baby-seats\']) );
        $unique_key = true;
    }

    if( isset($_REQUEST[\'s-taxi-return-id\']) && ! empty($_REQUEST[\'s-taxi-return-id\']) ) {
        // Add the dropdown value as custom cart item data
        $cart_item_data[\'s-taxi-rid\'] = wc_clean( esc_attr($_REQUEST[\'s-taxi-return-id\']) );
        $unique_key = true;
    }

    if( isset($unique_key) && $unique_key )
        $cart_item_data[\'unique-key\'] = md5(microtime().rand()); // Make each item unique

    return $cart_item_data;
}

// Add a fee
add_action( \'woocommerce_cart_calculate_fees\',\'add_custom_surcharge\', 10, 1 );
function add_custom_surcharge( $cart ) {
    if ( is_admin() && ! defined( \'DOING_AJAX\' ) )
        return;

    global $c_seats, $c_return_fare;

    $surcharge = 0; // Initializing

    // Loop through cart items
    foreach( $cart->get_cart() as $cart_item ) {
        if( isset($cart_item[\'s-baby-seats\']) || isset($cart_item[\'s-taxi-rid\']) ) {
            $baby_seats_fee  = isset($cart_item[\'s-baby-seats\']) ? intval($c_seats[1]) : 0;
            $taxi_return_fee =  isset($cart_item[\'s-taxi-rid\']) && $cart_item[\'s-taxi-rid\'] ? intval($c_return_fare) : 0;

            $surcharge += $baby_seats_fee + $return_fee; // Add to the fee cost
        }
    }

    // Add the calculated fee
    if ( $surcharge > 0 ) {
        $cart->add_fee( esc_html__(\'Extra + Return\', \'woocommerce\'), $surcharge );
    }
}
代码进入功能。活动子主题(或活动主题)的php文件。它可以工作。

<小时>Related answer on SO: Add a cart fee from an url variable in Woocommerce

相关推荐