使用 cookie 向 WooCommerce 购物车添加费用
Add fee to WooCommerce cart using a cookie
我有一个场景,其中在用户流的早期确定了一个数字。我使用 setcookie( "mileage", $distance_surcharge, time() + 36000 );
.
将其保存为 cookie
然后我尝试使用该 cookie 中的值向我的购物车添加附加费,但似乎没有传递 cookie 中的值。
根据 Woocommerce 文档的指示,这是我的 functions.php 文件中的片段:https://docs.woocommerce.com/document/add-a-surcharge-to-cart-and-checkout-uses-fees-api/
add_action( 'woocommerce_cart_calculate_fees','gt21_add_mileage' );
function gt21_add_mileage() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if(isset($_COOKIE['mileage'])) {
$fee = $_COOKIE['mileage'];
$woocommerce->cart->add_fee( 'Extra Mileage Fee', $fee, true, 'standard' );
}
}
我可以在购物车中看到额外的订单项,但只有当我将 $fee
设置为实际整数时,该值才会通过。
问题似乎与 cookie 的创建有关
这将在购物车页面上显示 cookie 的输出(用于调试目的)以及添加的费用
// Set cookie
function action_init() {
// Settings
$path = parse_url( get_option('siteurl'), PHP_URL_PATH );
$host = parse_url( get_option('siteurl'), PHP_URL_HOST );
$expiry = time() + 36000;
// Value
$distance_surcharge = 10;
// Set cookie
setcookie( 'mileage', $distance_surcharge, $expiry, $path, $host );
}
add_action( 'init', 'action_init' );
// Cart page (before cart - DEBUG)
function action_woocommerce_before_cart() {
// Isset
if ( isset( $_COOKIE['mileage'] ) ) {
$cookie = $_COOKIE['mileage'];
// Output
echo 'OK = ' . $cookie;
} else {
// Output
echo 'NOT OK';
}
}
add_action( 'woocommerce_before_cart', 'action_woocommerce_before_cart', 15 );
// Add fee
function action_woocommerce_cart_calculate_fees( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Isset
if ( isset( $_COOKIE['mileage'] ) ) {
// Fee
$fee = $_COOKIE['mileage'];
// Applying fee
$cart->add_fee( __( 'Extra Mileage Fee', 'woocommerce' ), $fee, true );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'action_woocommerce_cart_calculate_fees', 10, 1 );
代码进入活动子主题(或活动主题)的 functions.php 文件。在 Wordpress 5.8.1 和 WooCommerce 5.8.0
中测试和工作
我有一个场景,其中在用户流的早期确定了一个数字。我使用 setcookie( "mileage", $distance_surcharge, time() + 36000 );
.
然后我尝试使用该 cookie 中的值向我的购物车添加附加费,但似乎没有传递 cookie 中的值。
根据 Woocommerce 文档的指示,这是我的 functions.php 文件中的片段:https://docs.woocommerce.com/document/add-a-surcharge-to-cart-and-checkout-uses-fees-api/
add_action( 'woocommerce_cart_calculate_fees','gt21_add_mileage' );
function gt21_add_mileage() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if(isset($_COOKIE['mileage'])) {
$fee = $_COOKIE['mileage'];
$woocommerce->cart->add_fee( 'Extra Mileage Fee', $fee, true, 'standard' );
}
}
我可以在购物车中看到额外的订单项,但只有当我将 $fee
设置为实际整数时,该值才会通过。
问题似乎与 cookie 的创建有关
这将在购物车页面上显示 cookie 的输出(用于调试目的)以及添加的费用
// Set cookie
function action_init() {
// Settings
$path = parse_url( get_option('siteurl'), PHP_URL_PATH );
$host = parse_url( get_option('siteurl'), PHP_URL_HOST );
$expiry = time() + 36000;
// Value
$distance_surcharge = 10;
// Set cookie
setcookie( 'mileage', $distance_surcharge, $expiry, $path, $host );
}
add_action( 'init', 'action_init' );
// Cart page (before cart - DEBUG)
function action_woocommerce_before_cart() {
// Isset
if ( isset( $_COOKIE['mileage'] ) ) {
$cookie = $_COOKIE['mileage'];
// Output
echo 'OK = ' . $cookie;
} else {
// Output
echo 'NOT OK';
}
}
add_action( 'woocommerce_before_cart', 'action_woocommerce_before_cart', 15 );
// Add fee
function action_woocommerce_cart_calculate_fees( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Isset
if ( isset( $_COOKIE['mileage'] ) ) {
// Fee
$fee = $_COOKIE['mileage'];
// Applying fee
$cart->add_fee( __( 'Extra Mileage Fee', 'woocommerce' ), $fee, true );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'action_woocommerce_cart_calculate_fees', 10, 1 );
代码进入活动子主题(或活动主题)的 functions.php 文件。在 Wordpress 5.8.1 和 WooCommerce 5.8.0
中测试和工作