致命错误:在第413行的/functions.php中的非对象上调用成员函数get_cart_contents_count()

时间:2017-06-07 04:33:33

标签: php error-handling woocommerce fatal-error

  

第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(); 

变空。

那么如何解决此错误?请帮帮我

1 个答案:

答案 0 :(得分:0)

最后我找到了答案

  

致命错误:在a上调用成员函数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' );