【问题标题】:Get woocommerce cart total in theme's functions.php在主题的functions.php中获取woocommerce购物车总数
【发布时间】:2016-03-11 19:18:00
【问题描述】:

如果购物车是免费的,我想在 Woocommerce 结帐中隐藏账单详细信息,所以我试图在主题 functions.php 中获取购物车价格,但出现此错误:

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

我尝试使用的代码是:

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;
    }
}

我在互联网的帮助下通过了这里,但我被困住了。如果您能帮助我,我将不胜感激。

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    您需要将代码包装在一个函数中,以便仅在执行woocommerce_checkout_process 操作时调用它。

    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;
          }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-10
      • 1970-01-01
      • 2015-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多