我有一個場景,其中在用戶流程的早期確定了一個數字。我正在使用setcookie( "mileage", $distance_surcharge, time() 36000 );.
然后,我嘗試使用該 cookie 中的值向我的購物車添加附加費,但似乎沒有傳遞來自 cookie 的值。
這是我的functions.php檔案中的片段,按照Woocommerce docs的指示: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為實際整數時才會傳遞該值。
uj5u.com熱心網友回復:
問題似乎更多與 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 中測驗和作業
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/348875.html
標籤:php WordPress的 饼干 求购 大车
