【问题标题】:Fatal error: Call to a member function get_cart_contents_count() on a non-object in /functions.php on line 413致命错误:在第 413 行的 /functions.php 中的非对象上调用成员函数 get_cart_contents_count()
【发布时间】:2017-11-08 06:30:25
【问题描述】:

在线 413 $product_count = $woocommerce->cart->get_cart_contents_count();

function wc_sell_only_states( $states ) {
        // set our flag to be false until we find a product in that category
    $cat_check = false;
    global $woocommerce;
    ob_start();
    //==========================================AAAAAAAAAAAAAAAAA
    $product_count = $woocommerce->cart->get_cart_contents_count();
    if($product_count > 0){
    foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) { //foreach

    $product = $cart_item['data'];

    // replace 'membership' with your category's slug
    if ( has_term( 'VIP', 'product_tag', $product->id ) ) {//search product_cat
    $cat_check = true;
    // break because we only need one "true" to matter here
    //////////////////////////////////////////////////////////////////////////////////////
    if (!function_exists('soosti_add_checkout_content')) {
    function soosti_add_checkout_content() { // skyverge function
    echo '<p style="color:#F31114">Attention: In your shopping cart hava Special product that only shipping to <b>California</b>...</p>';
    }//end skyverge function
    }
    add_action( 'woocommerce_before_checkout_form', 'soosti_add_checkout_content', 12 );
    //-------------------------



    //---------------------------
    if (!function_exists('woo_override_checkout_fields')) {
    function woo_override_checkout_fields( $fields ) { // woo_override_checkout_fields Function


    $fields['billing']['billing_country'] = array(
    'type' => 'select',
    'label' => __('Country', 'woocommerce'),
    'options' => array('US' => 'United States(US)')
    ); 

    $fields['billing']['billing_state'] = array(
    'type' => 'select',
    'label' => __('State', 'woocommerce'),
    'options' => array('CA' => 'California(CA)')

    );

    return $fields; 
    } //end woo_override_checkout_fields Function
    }
    add_filter( 'woocommerce_checkout_fields' , 'woo_override_checkout_fields' );

如何解决这个错误?

当我运行此代码时,我得到一个致命错误。我觉得

$woocommerce->cart->get_cart_contents_count(); 

清空。

那么我该如何解决这个错误呢?请帮帮我

【问题讨论】:

    标签: php error-handling woocommerce fatal-error


    【解决方案1】:

    终于找到答案了

    致命错误:调用成员函数 get_cart_contents_count() 非对象

    function a000_remove_bundles_counting(){
    
    global $woocommerce_bundles;
    remove_filter( 'woocommerce_cart_contents_count',
    array( $woocommerce_bundles->display, 'woo_bundles_cart_contents_count' ) );
    }
    add_action( 'init', 'a000_remove_bundles_counting' );
    

    在同时添加自己的 via 之前禁用 Bundles 过滤器 d000_cart_contents_count

    function d000_cart_contents_count( $count ) {
        global $woocommerce;
     $cat_check = false;
      foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) { //foreach
    
     $product = $cart_item['data'];
    
     if ( has_term( 'tv', 'product_tag', $product->id ) ) {//search product_cat
    $cat_check = true;
    // break because we only need one "true" to matter here
    if (!function_exists('woo_override_checkout_fields')) {
    function woo_override_checkout_fields( $fields ) { // woo_override_checkout_fields Function
    
    
    $fields['billing']['billing_country'] = array(
    'type' => 'select',
    'label' => __('Country', 'woocommerce'),
    'options' => array('US' => 'United States(US)')
    ); 
    
    $fields['billing']['billing_state'] = array(
    'type' => 'select',
    'label' => __('State', 'woocommerce'),
    'options' => array('CA' => 'California(CA)')
    
    );
    
    return $fields; 
    } //end woo_override_checkout_fields Function
    }
    add_filter( 'woocommerce_checkout_fields' , 'woo_override_checkout_fields' );
    
    } // end search product_cat 
      }// end foreach
    
    
    
    return $count;
    }
    add_filter( 'woocommerce_cart_contents_count',
    'd000_cart_contents_count' );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-15
      • 1970-01-01
      • 1970-01-01
      • 2016-05-24
      • 2014-02-10
      • 1970-01-01
      • 1970-01-01
      • 2013-11-25
      相关资源
      最近更新 更多