Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 检查一个变量是否是数据库中类对象的实例_Php_Woocommerce_Instance Variables_Orders_Object Class - Fatal编程技术网

Php 检查一个变量是否是数据库中类对象的实例

Php 检查一个变量是否是数据库中类对象的实例,php,woocommerce,instance-variables,orders,object-class,Php,Woocommerce,Instance Variables,Orders,Object Class,我正在运行一个功能,通过该功能,我可以检查用户是否在以前的订单上有退款的商品,以及他们是否在结账时将信用卡作为负的购物车费用。该功能适用于以前下过订单的用户,但会在网站上为新用户造成严重错误 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/

我正在运行一个功能,通过该功能,我可以检查用户是否在以前的订单上有退款的商品,以及他们是否在结账时将信用卡作为负的购物车费用。该功能适用于以前下过订单的用户,但会在网站上为新用户造成严重错误

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_Return()会导致错误,因为他们没有任何要检查的订单?如何安全地执行此操作?

试试这种方法,添加了多个控件,请参见注释

//检查上次订单的退款项目总数-在结帐时作为费用添加
功能添加\上次\订单\退款\结帐时的总额($cart){
if(定义了('DOING'uajax'))
返回;
//当前用户ID
$user\u id=get\u current\u user\u id();
//用户id存在
如果($user\u id>0){
$customer=新的WC\U客户($user\U id);
//拿到最后的订单
$last_order=$customer->get_last_order();
//真的
如果($上次订单){
//获取订单id
$order\u id=$last\u order->get\u id();
//订单ID存在
如果(是数字($order\u id)){
//获取WC_Order对象实例(从订单ID)
$order=wc\u get\u order($order\u id);
//获得订单退款(退款数组)
$order_returns=$order->get_returns();
//不空
如果(!空($order\u退款)){
//WIP需要检查哪些项目有税
$total_to_return=$order->get_total_returned();
//加费
$cart->添加费用('return',$total\u to\u return);
}
}
}
}
}
添加操作('woocommerce\u cart\u calculate\u fees'、'add\u last\u order\u Return\u total\u at\u checkout',10,1);

如果您查看
WC\u客户
获取最后一次订单()
方法或,您将看到:

/*
 * @return WC_Order|false
 */
这意味着
get\u last\u order()
方法也可以返回

  • WC\u顺序
    对象
  • false
    布尔值
因此,您可以在代码中使用:

$last_order = $customer->get_last_order();

if ( ! $last_order )  {
   return; // Exit (Not an order)
}
以避免此错误


现在,您可以使用php条件函数检查变量是否来自特定对象类,而不是类似以下内容:

$last_order = $customer->get_last_order();

if ( ! is_a( $last_order, 'WC_Order' ) ) {
   return; // Exit (Not an order)
}

// Your other code… 
或者,您可以将php条件函数用于
WC\u Order
get\u returns()
方法,以检查a变量是否来自特定对象类,而不是其他对象:

$last_order = $customer->get_last_order();

if ( ! method_exists( $last_order, 'get_refunds' ) )  {
   return; // Exit (Not an order)
}

// Your other code…
这三个案例运行良好,避免了错误