【问题标题】:Uncaught Error: Call to a member function get_cart() on null未捕获的错误:在 null 上调用成员函数 get_cart()
【发布时间】:2020-03-07 18:11:54
【问题描述】:

在 Woocommerce 上,我的函数中有一条错误消息,但我不明白为什么会发生。

未捕获的错误:调用成员函数 get_cart() on null in ... 该函数检查一个类别产品是否在购物车中。

错误显示在后台的订单详情中

在functions.php中使用,在结账页面执行, 由这些钩子调用: woocommerce_before_order_notes woocommerce_checkout_process woocommerce_checkout_update_order_meta woocommerce_admin_order_data_after_billing_address woocommerce_email_order_meta_keys

https://i.stack.imgur.com/v7Atp.png

function is_in_the_cart() {

    $found=false;

// Find if product is in the cart price <=40
    foreach( WC()->cart->get_cart() as $cart_item_key => $values )
 {
        $cart_product = $values['data'];
        $price = accessProtected($values['data'], 'changes')['price'];
        if ($price >=40 && $cart_product->id=='969') {$found=true;}
  }


if ( $found ) {
        return true;
    } else {
        return false;
    }
}



     add_action('woocommerce_checkout_process', 'wps_select_checkout_field_process');
     function wps_select_checkout_field_process() {
         if (is_in_the_cart())
    {
        global $woocommerce;

        // Check if set, if its not set add an error.
        if ($_POST['dvd'] == "blank")
         wc_add_notice( '<strong>Merci de séléctionner un DVD</strong>', 'error' );
    }
     }

     //* Update the order meta with field value
     add_action('woocommerce_checkout_update_order_meta', 'wps_select_checkout_field_update_order_meta');
     function wps_select_checkout_field_update_order_meta( $order_id ) {
         if (is_in_the_cart())
    {
       if ($_POST['dvd']) update_post_meta( $order_id, 'dvd', esc_attr($_POST['dvd']));
    }
     }


     add_action( 'woocommerce_admin_order_data_after_billing_address', 'wps_select_checkout_field_display_admin_order_meta', 10, 1 );
    function wps_select_checkout_field_display_admin_order_meta($order){
         if (is_in_the_cart())
    {
        echo '<p><strong>DVD: </strong> ' . get_post_meta( $order->id, 'dvd', true ) . '</p>';
    }
    }

    //* Add selection field value to emails
    add_filter('woocommerce_email_order_meta_keys', 'wps_select_order_meta_keys');
    function wps_select_order_meta_keys( $keys ) {
         if (is_in_the_cart())
    {
        $keys['Dvd:'] = 'dvd';
        return $keys;
        }
    }


【问题讨论】:

  • 您在哪里使用该代码?你什么时候显示结果?在哪个页面上?用哪个钩子?
  • OK 问题已编辑

标签: woocommerce


【解决方案1】:

我想你想在你的函数之前检查以确保购物车不是空的。

另外...您可以使用 get_price() 从您使用 $values['data'] 检索的产品对象中访问价格

    function is_in_the_cart(){

        // Make sure it's only on front end
        if (is_admin()) return false;

        $found = false;

        // If cart is empty - bail and return false
        if (empty (WC()->cart->get_cart())) {  

            return false;

        } else {

        foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
            $cart_product = $values[ 'data' ];
            // Find if product is in the cart price <=40
            $price = floatval($cart_product->get_price());
            if ( $price >= 40 && $cart_product->id == '969' ) {
                $found = true;
                }
            }

            if ( $found ) {
                return true;
            } else {
                return false;
            }
        }
    }

更新 - 添加了is_admin 以防止在后端运行。

【讨论】:

  • 感谢空置条件。关于 get_price 这不是一个好的属性,因为我使用 WC 命名你的价格并且价格是动态的,所以我保留我的方法来获取它。
  • empty() 比 is_null() 更好吗?
  • get_cart() Returns the contents of the cart in an array. woocommerce/includes/class-wc-cart.php:604 所以返回的不是 null。
  • 这么好用空()?
  • 当我有错误购物车不是空的,因为订单已经完成
猜你喜欢
  • 1970-01-01
  • 2021-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-28
  • 2020-10-04
  • 2016-04-20
  • 2020-08-24
相关资源
最近更新 更多