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