简体   繁体   中英

Get woocommerce cart total in theme's functions.php

I want to hide billing details in Woocommerce checkout if the cart is free, so i'm trying to get the cart price in theme functions.php and i get this error:

 Call to a member function get_cart_total() on a non-object

The code that i'm trying to use is:

add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );

global $woocommerce;
if ( $woocommerce->cart->get_cart_total() == 0 ) {
    function kia_filter_billing_fields($fields){
        unset( $fields["billing_email"] );
        unset( $fields["billing_last_name"] );
        unset( $fields["billing_first_name"] );
        unset( $fields["billing_country"] );
        unset( $fields["billing_company"] );
        unset( $fields["billing_address_1"] );
        unset( $fields["billing_address_2"] );
        unset( $fields["billing_city"] );
        unset( $fields["billing_state"] );
        unset( $fields["billing_postcode"] );
        unset( $fields["billing_phone"] );
        return $fields;
    }
}

I got through here with the help of the Internet, but I'm stuck. I would appreciate if you could help me.

You need to wrap your code in a function, so that it is only called when the woocommerce_checkout_process action is executed.

add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );

function wc_minimum_order_amount()
{
  global $woocommerce;
  if ( $woocommerce->cart->get_cart_total() == 0 ) {
      function kia_filter_billing_fields($fields){
          unset( $fields["billing_email"] );
          unset( $fields["billing_last_name"] );
          unset( $fields["billing_first_name"] );
          unset( $fields["billing_country"] );
          unset( $fields["billing_company"] );
          unset( $fields["billing_address_1"] );
          unset( $fields["billing_address_2"] );
          unset( $fields["billing_city"] );
          unset( $fields["billing_state"] );
          unset( $fields["billing_postcode"] );
          unset( $fields["billing_phone"] );
          return $fields;
      }
  }
}

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