简体   繁体   中英

Apply a fee to cart in Woocommerce

I am trying to programatically add a fee to the woocommerce cart from a script which is executed on a form submission. As far as I am aware, I don't think I am able to use a hook as I need to apply the custom fee when the form on the page has been submitted (Custom API integration).

I have tried doing the following within the script:

add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );
function woo_add_cart_fee( $cart ){
    $valid = false;

    if ( ! $_POST || ( is_admin() && ! is_ajax() ) ) {
        return;
    }

    if (isset($_POST['coupon_code'])) {
        $code = $_POST['coupon_code'];
        $coupon = new WC_Coupon($code);
        if($coupon->get_amount() != null){
            $valid == true;
        }

        //if not then lets check to see if its a giftcard.
        if($valid == false){
            $api_login="xxxxxx";
            $api_password="xxxxxx";
            $url = "https://xxxxxx.com/xxxxx/xxxxx.svc";
            $client = new SoapClient( $url . "?singleWsdl",
                array(
                "location" => $url,
                "login" => $api_login,
                "password" => $api_password,
                "trace" => 1
                )
            );  

            $request = new StdClass();
            $request->bonId = $code;
            $request->bonType = 'GiftCard';

            // call the correct database
            $request->context = new StdClass();
            $request->context->DatabaseId = 'xxxxx';    

            try {
                $resu = $client->GetBonAvailableAmount($request);
                if (isset($resu->GetBonAvailableAmountResult->Amount)) {
                    $amount = $resu->GetBonAvailableAmountResult->Amount;
                    $cart->add_fee('xxxxxx Gift Card ', floatval('-'.$amount * 0.83333), false, '' );                     
                } else {
                    $response['status'] = 'error';
                    $response['message'] = 'Gift card not recognized.';
                }
            } catch (Exception $e) {

            }
        }
    }
} 

and I can see that when I echo the cart object there is a fee object which contains all the correct data.

It seems that the cart or totals are not updating, if i refresh the page, they still do not reflect the values i am expected.

I have trawled pretty much all Stack Overflow posts and cant seem to find anything which solves the issue.

Is there anything that I am missing here?

You need to use a custom function hooked in woocommerce_cart_calculate_fees action hook:

add_action( 'woocommerce_cart_calculate_fees', 'add_a_custom_fee', 10, 1 );
function add_a_custom_fee( $cart ) {
        $amount = 20;
        $cart->add_fee( __('Custom fee'), $amount );
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM