woocommerce增加的费用不计算总是0 neather总价

时间:2018-02-22 10:50:11

标签: wordpress woocommerce

我有2个功能。一个是计算费用(这里是负责计算费用并将其保存为全局变量的部分):

function pre_checkout()
{
    global $woocommerce;
    $section2 ='';
    $section3 ='';
    $section2 = sanitize_text_field($_POST["user_data"]["section2"]);
    $section3 = sanitize_text_field($_POST["user_data"]["section3"]);
    $how_many_months = preg_replace('/\D/', '', $_POST["user_data"]["section1"]);
    $boxToFind = '';
    if(($section2 == 'toy_yes') || ($section2 == 'toy_no'))
    {
        $boxToFind = sanitize_text_field($_POST["user_data"]["section1"]);
    }
    else{
        $boxToFind = sanitize_text_field($_POST["user_data"]["section1"].$_POST["user_data"]["section2"]);
    }
    WC()->cart->empty_cart(true);
    $boxesLookupTable = array(
            //susbscriptions
     'small1month'=>'112',
    'medium1month'=>'113',
    'large1month'=>'114',
    'small6month'=>'115',
    'medium6month'=>'116',
    'large6month'=>'117',
    'small12month'=>'118',
    'medium12month'=>'119',
    'large12month'=>'120',
        //gifts
    'gift1month'=>'133',
    'gift3month'=>'134',
    'gift6month' =>'135',
    'gift12month' =>'136'
    );
    $productId = $boxesLookupTable[$boxToFind];
//    echo $productId;
    //check if product already in cart
    // if no products in cart, add it
    WC()->cart->add_to_cart($productId,1);
    if($section2 === "toy_yes"){
        $product = wc_get_product($productId);
        $price = $product->get_price();
        $toy_price = 4.99;
        unset($_COOKIE['product_price_with_toy']);
        setcookie('product_price_with_toy',$toy_price*$how_many_months,time() + (86400 * 30),"/");
    //  WC_Cart::add_fee( 'Gift Fee ', $product_price_with_toy,false, false );
        /* "gift"; */
    }
    if($section3 === "toy_yes"){
        $how_many_months = preg_replace('/\D/', '', $_POST["user_data"]["section2"]);
        $product = wc_get_product($productId);
        $price = $product->get_price();
        $toy_price = 4.99;
        unset($_SESSION['product_price_with_toy']);
        $_SESSION['product_price_with_toy']  = $price + $toy_price;
        unset($_COOKIE['product_price_with_toy']);
        setcookie('product_price_with_toy',$how_many_months * $toy_price,time() + (86400 * 30),"/");
    //    WC_Cart::add_fee( 'Gift Fee ', $product_price_with_toy,false, false );
        /*"subscription"; */
    };
    $items = WC()->cart->get_cart();
    global $woocommerce;
    $cart_url = wc_get_cart_url();
    $checkout_url = wc_get_checkout_url();
    foreach($items as $item => $values) {
        $_product = $values['data']->post;
    ?>
    <div class="dropdown-cart-wrap">
        <div class="dropdown-cart-left">
            <?php echo get_the_post_thumbnail( $values['product_id'], 'thumbnail' ); ?>
        </div>
        <div class="dropdown-cart-right">
            <h5><?php echo $_product->post_title; ?></h5>
            <p><strong>Quantity:</strong>
               <?php $product = wc_get_product($productId);
                woocommerce_quantity_input( array( 'min_value' => 1, 'max_value' => $product->backorders_allowed() ? '' : $product->get_stock_quantity() ) );
                ?></p>
            <?php global $woocommerce;
            $currency = get_woocommerce_currency_symbol();
            $price = get_post_meta( $values['product_id'], '_regular_price', true);
            $sale = get_post_meta( $values['product_id'], '_sale_price', true);
            ?>
            <?php if($sale) { ?>
                <p class="price"><strong>Price:</strong> <del><?php echo $currency; echo $price; ?></del> <?php echo $currency; echo $sale; ?></p>
            <?php } elseif($price) { ?>
                <p class="price"><strong>Price:</strong> <?php echo $currency; echo $price; ?></p>
            <?php }
            ?>
            <div class="dropdown-cart-right dropdown-checkout-link">
                <a class="checkout-submit checkout green-btn text-center" href="#">Order</a>
            </div>
        </div>
        <div class="clear"></div>
    </div>
<?php }
    die();
}
add_action('wp_ajax_pre_checkout','pre_checkout');
add_action('wp_ajax_nopriv_pre_checkout', 'pre_checkout');

接下来,我有一个woocommerce行动:

function add_cart_fee() {
    $chuj = $GLOBALS['product_price_with_toy'];
    WC()->cart->add_fee( __('Extra Gift Fee', 'woocommerce'),$chuj );
}
add_action( 'woocommerce_cart_calculate_fees', 'add_cart_fee' ); 

当使用会话时,基本玩具费总是0,全局变量 - 只通过cookie将值传递给行动 - 我不明白为什么woocommerce / wordpress正在擦除我在计算功能中设置的全局值。或者也许有人知道如何使用钩子和动作更新总价格,这甚至比使用费用字段更好。

fee for gift product fee for subscription type product

0 个答案:

没有答案