检查变量是否是 WooCommerce 中类对象的实例

我正在运行一个功能,检查用户之前的订单中是否有退款的商品,如果有,则在结帐时将积分作为负购物车费用应用。该功能适用于之前下过订单的用户,但对新用户来说会在网站上造成严重错误。


Fatal error: Uncaught Error: Call to a member function get_refunds() on bool in /wp-content/themes/my-theme/refunds.php:20 Stack trace:  

#0 /wp-includes/class-wp-hook.php(287): add_last_order_refund_total_at_checkout(Object(WC_Cart))  

#1 /wp-includes/class-wp-hook.php(311): WP_Hook->apply_filters('', Array)  

#2 /wp-includes/plugin.php(478): WP_Hook->do_action(Array)  

#3 /wp-content/plugins/woocommerce/includes/class-wc-cart.php(1714): do_action('woocommerce_car...', Object(WC_Cart))  

#4 /wp-content/plugins/woocommerce/includes/class-wc-cart-totals.php(270): WC_Cart->calculate_fees()  

#5 /wp-content/plugins/woocommerce/includes/class-wc-cart-totals.php(829): WC_Cart_Totals->get_fees_from_cart()  

#6 /wp-content/plugins/woocommerce/includes/class-wc- in /wp-content/themes/my-theme/refunds.php on line 20

以下是检查上一个订单是否有退款的代码:


//Check total of refunded items from last order - add as a fee at checkout

function add_last_order_refund_total_at_checkout($cart_object){

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )

        return;


    $user_id = get_current_user_id(); // The current user ID

    $customer = new WC_Customer( $user_id );

    $last_order = $customer->get_last_order();

    $order_id = $last_order;


    // Get the WC_Order Object instance (from the order ID)

    $order = wc_get_order( $order_id );


    // Get the Order refunds (array of refunds)

    $order_refunds = $order->get_refunds();

    $total_to_refund = $order->get_total_refunded()*(-1);//WIP need to check which items have tax

    

    if (!empty($order_refunds)) WC()->cart->add_fee( 'Refund', $total_to_refund  );  

}

add_action( 'woocommerce_cart_calculate_fees', 'add_last_order_refund_total_at_checkout', 10, 1 );

 

我相信我需要首先检查用户是否有任何以前的订单,否则 get_refund() 会导致错误,因为他们没有任何订单要检查?我该如何安全地做到这一点?


倚天杖
浏览 114回答 2
2回答

烙印99

如果您查看WC_Customer get_last_order()方法文档 或源代码,您将看到:/* * @return WC_Order|false */这意味着该get_last_order() 方法可以返回:物体WC_Order_或false布尔值。所以你可以在你的代码中使用:$last_order = $customer->get_last_order();if ( ! $last_order )  {   return; // Exit (Not an order)}为了避免这个错误。现在,您可以使用is_a()php 条件函数来检查变量是否来自特定的 Object 类,而不是其他变量,例如:$last_order = $customer->get_last_order();if ( ! is_a( $last_order, 'WC_Order' ) ) {   return; // Exit (Not an order)}// Your other code… 或者您可以使用 method_exists()php 条件函数作为WC_Order get_refunds()方法,以检查 a 变量是否来自特定的 Object 类而不是其他内容:$last_order = $customer->get_last_order();if ( ! method_exists( $last_order, 'get_refunds' ) )  {   return; // Exit (Not an order)}// Your other code…这三种情况工作得很好,避免了错误

汪汪一只猫

这样试试吧,已经添加了多个控件,见评论// Check total of refunded items from last order - add as a fee at checkoutfunction add_last_order_refund_total_at_checkout( $cart ) {    if ( is_admin() && ! defined( 'DOING_AJAX' ) )        return;    // The current user ID    $user_id = get_current_user_id();        // User id exists    if ( $user_id > 0 ) {        $customer = new WC_Customer( $user_id );                // Get last order        $last_order = $customer->get_last_order();        // True        if ( $last_order ) {            // Get the order id            $order_id = $last_order->get_id();                    // Order ID exists            if ( is_numeric( $order_id ) ) {                // Get the WC_Order Object instance (from the order ID)                $order = wc_get_order( $order_id );                // Get the Order refunds (array of refunds)                $order_refunds = $order->get_refunds();                            // NOT empty                if ( ! empty( $order_refunds) ) {                    // WIP need to check which items have tax                    $total_to_refund = $order->get_total_refunded();                                    // Add fee                    $cart->add_fee( 'Refund', $total_to_refund  );                }            }        }    }}add_action( 'woocommerce_cart_calculate_fees', 'add_last_order_refund_total_at_checkout', 10, 1 );
打开App,查看更多内容
随时随地看视频慕课网APP