我已写下以下代码,以便在订单状态更改完成后发送通知。但我不知道为什么代码不起作用。每当我尝试更改订单状态时,它会重定向到包含500个错误代码的空白页。你能告诉我如何在下面的代码中获得税务细节。
订购后,会向销售代表发送电子邮件通知 证实
add_action("woocommerce_order_status_changed",
"my_awesome_publication_notification", 10, 3 );
function my_awesome_publication_notification($order_id, $checkout=null) {
$order = new WC_Order( $order_id );
$order_total_amount = $order->get_formatted_order_total();
$items = $order->get_items();
foreach ( $items as $item ) {
$product_name= $item['name'];
$product_id= $item['product_id'];
}
$_product = wc_get_product( $product_id );
$product_regular_price = $_product->get_regular_price();
$order_data = $order->get_data();
$order_date_created = $order_data['date_created'];
$order_o_total = $order_data['total'];
$order_discount_total = $order_data['discount_total'];
$order_total_tax1 = $order_data['total_tax'];
if($order->status === 'completed' ) {
if( $order->get_used_coupons() ) {
foreach( $order->get_used_coupons() as $coupon) {
$coupon_name = $coupon;
/* get coupon's details */
$coupon_post_obj = get_page_by_title($coupon, OBJECT, 'shop_coupon');
$coupon_id = $coupon_post_obj->ID;
$coupons_obj = new WC_Coupon($coupon_id); /* get coupon instance */
$coupon_discount_amount = $coupons_obj->get_discount_amount();
$sales_representative_name = get_post_meta( $coupon_id, 'sales_representative_name', true );
$sales_representative_email_address = get_post_meta( $coupon_id, 'sales_representative_email_address', true );
}
$to = $sales_representative_email_address;
$subject = 'New Order Completed Using Assigned Coupon Code';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: My Name <nanganstella@gmail.com>' . "\r\n";
$message = '<html><body>';
$message .= '<h1>A new order has been completed using assigned coupon code.<br/></h1>';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= '<tr><td><strong>Product</strong></td><td>'. $product_name .'</td></tr>';
$message .= '<tr><td><strong>Coupons used</strong></td><td>'. $coupon_name .'</td></tr>';
$message .= '<tr><td><strong>Date</strong></td><td>'. $order_date_created .'</td></tr>';
$message .= '<tr><td><strong>Product Price</strong></td><td>'. $product_regular_price .'CFA</td></tr>';
$message .= '<tr><td><strong>Order Discount </strong></td><td>'. $order_discount_total.'CFA</td></tr>';
$message .= '<tr><td><strong>Order Tax </strong></td><td>'. $order_total_tax1 .'</td></tr>';
$message .= '<tr><td><strong>Order Amount</strong></td><td>'. $order_total_amount .'</td></tr>';
$message .= '</table>';
$message .= '</body></html>';
wp_mail( $to, $subject, $message, $headers );
}
}
}
错误:状态调用不正确。不应直接访问订单属性。
Backtrace: require('wp-blog-header.php'),
require_once('wp-includes/template-loader.php'),
do_action('template_redirect'),
WP_Hook->do_action, WP_Hook->apply_filters,
WC_AJAX::do_wc_ajax,
do_action('wc_ajax_checkout'),
WP_Hook->do_action,
WP_Hook->apply_filters,
WC_AJAX::checkout,
WC_Checkout->process_checkout,
WC_Checkout->process_order_payment,
WC_Gateway_BACS->process_payment,
WC_Order->update_status,
WC_Order->save,
WC_Order->status_transition,
do_action('woocommerce_order_status_changed'),
WP_Hook->do_action,
WP_Hook->apply_filters,
my_awesome_publication_notification,
WC_Abstract_Legacy_Order->__get,
wc_doing_it_wrong. This message was added in version 3.0.