Php WooCommerce get_items()电子邮件设置页面中出现致命错误

Php WooCommerce get_items()电子邮件设置页面中出现致命错误,php,wordpress,email,woocommerce,Php,Wordpress,Email,Woocommerce,如果订单是订阅父级,我们使用下面截取的此按钮取消发送订单的完整邮件。这是有效的。但我刚刚注意到,当我打开WooCommerce电子邮件设置时,我遇到了一个致命错误 工作解决方案,但在WooCommerce电子邮件设置页面上出现错误: add_filter( 'woocommerce_email_enabled_customer_completed_order', 'dcwd_conditionally_send_wc_email', 10, 2); function dcwd_conditio

如果订单是订阅父级,我们使用下面截取的此按钮取消发送订单的完整邮件。这是有效的。但我刚刚注意到,当我打开WooCommerce电子邮件设置时,我遇到了一个致命错误

工作解决方案,但在WooCommerce电子邮件设置页面上出现错误:

add_filter( 'woocommerce_email_enabled_customer_completed_order', 'dcwd_conditionally_send_wc_email', 10, 2);

function dcwd_conditionally_send_wc_email( $whether_enabled, $order ) {

    //error_log( 'is_enabled: ' . current_filter() . ' - Order: ' . $object->get_order_number() ); // Debugging info.
    foreach ( $order->get_items() as $item ) {
    // Get an instance of the WC_Product object
    $product = $item->get_product();

    // Get the correct product ID (for variations we take the parent product ID)
    $product_id = $product->get_type();

   //print_r($product_id); die();

   // Check for product categories for this item
   // two encounter both (variable and normal subscriptions) we need to check both => if ($product_id == 'subscription' || $product_id == 'subscription_variation')
   if($product_id == 'subscription_variation') {

       return false;
      }
   }
   return $whether_enabled;
}
这是错误,但我不明白问题是什么

Fatal error: Uncaught Error: Call to a member function get_items() on null in /home/hosting7878877/www/staging65678.ch/cbcmembership/wp-content/themes/generatepress_child/functions.php:44 Stack trace: #0 /home/hosting7878877/www/staging65678.ch/cbcmembership/wp-includes/class-wp-hook.php(289): dcwd_conditionally_send_wc_email(true, NULL) #1 /home/hosting7878877/www/staging65678.ch/cbcmembership/wp-includes/plugin.php(206): WP_Hook->apply_filters(true, Array) #2 /home/hosting7878877/www/staging65678.ch/cbcmembership/wp-content/plugins/woocommerce/includes/emails/class-wc-email.php(500): apply_filters('woocommerce_ema...', true, NULL, Object(WC_Email_Customer_Completed_Order)) #3 /home/hosting7878877/www/staging65678.ch/cbcmembership/wp-content/plugins/woocommerce/includes/admin/settings/class-wc-settings-emails.php(318): WC_Email->is_enabled() #4 /home/hosting7878877/www/staging65678.ch/cbcmembership/wp-includes/class-wp-hook.php(287): WC_Settings_Emails->email_notification_setting(Array) #5 /home/hosting7878877/www/staging65678.ch/cbcmembership/wp-includes/class-wp-hook.php(311): WP_Hook->apply_fi in /home/hosting7878877/www/staging65678.ch/cbcmembership/wp-content/themes/generatepress_child/functions.php on line 44

我测试了你的代码,它对我有效。您仍然可以通过检查变量是否是订单对象来解决错误:

// check if the variable is an order object 
if ( ! is_a( $order, 'WC_Order' ) ) {
    return $whether_enabled;
}
因此:


我测试了你的代码,它对我有效。您仍然可以通过检查变量是否是订单对象来解决错误:

// check if the variable is an order object 
if ( ! is_a( $order, 'WC_Order' ) ) {
    return $whether_enabled;
}
因此:


显然,有时WooCommerce在
$order
中传递
null
。添加此条件以绕过它:

if(!isset($order)| | count($order->get_items())==0){
返回$U是否启用;
}
最后的函数是这样的:

add_filter('woocommerce_email_enabled_customer_completed_order'、'dcwd_conditionally_send_wc_email',10,2);
函数dcwd\u有条件地发送\u wc\u电子邮件($是否启用,$订单){
如果(!isset($order)| count($order->get_items())=0){
返回$U是否启用;
}
//错误日志('is_enabled:'。当前过滤器()。-Order:'。$object->get_Order_number());//调试信息。
foreach($order->get_items()作为$item){
//获取WC_产品对象的实例
$product=$item->get_product();
//获取正确的产品ID(对于变体,我们采用父产品ID)
$product\U id=$product->get\U type();
//print_r($product_id);die();
//检查此项目的产品类别
//两个同时遇到(变量订阅和普通订阅)我们需要检查这两个=>如果($product_id=='subscription'.|$product_id=='subscription_VARIANCE')
如果($product\U id=='subscription\U VARIANCE'){
返回false;
}
}
返回$U是否启用;
}

显然,有时WooCommerce会在
$order
中传递
null
。添加此条件以绕过它:

if(!isset($order)| | count($order->get_items())==0){
返回$U是否启用;
}
最后的函数是这样的:

add_filter('woocommerce_email_enabled_customer_completed_order'、'dcwd_conditionally_send_wc_email',10,2);
函数dcwd\u有条件地发送\u wc\u电子邮件($是否启用,$订单){
如果(!isset($order)| count($order->get_items())=0){
返回$U是否启用;
}
//错误日志('is_enabled:'。当前过滤器()。-Order:'。$object->get_Order_number());//调试信息。
foreach($order->get_items()作为$item){
//获取WC_产品对象的实例
$product=$item->get_product();
//获取正确的产品ID(对于变体,我们采用父产品ID)
$product\U id=$product->get\U type();
//print_r($product_id);die();
//检查此项目的产品类别
//两个同时遇到(变量订阅和普通订阅)我们需要检查这两个=>如果($product_id=='subscription'.|$product_id=='subscription_VARIANCE')
如果($product\U id=='subscription\U VARIANCE'){
返回false;
}
}
返回$U是否启用;
}
检查isset($order)很好,我认为您的订单没有对象。检查isset($order)很好,我认为您的订单没有对象。