如何在删除WooCommerce产品时获取产品的元数据?

时间:2021-02-25 作者:Greg Maldewin

在删除订单之前,我无法访问订单的单个产品。

add_action(\'before_delete_post\', \'removeCheckDetail\', 99, 2 );
function removeCheckDetail( $order_id ) {

global $woocommerce; 
global $wpdb; 

// $order = new WC_Order($order_id);
$order = wc_get_order( $order_id ); // This Works
error_log("Starting " .$order->get_id()); 

$order_items = $order->get_items(); 
error_log("Count: " .count($order_items)); // Returns 0

// Loop through order line items
foreach( $order_items as $item_id => $item ) {
    error_log("CartItemKey: " .$item->get_name()); // No Loops
}
}

1 个回复
SO网友:Greg Maldewin

终于成功了。

add_action(\'before_delete_post\', \'removeCheckDetail\', 10, 1 );
function removeCheckDetail( $order_id ) {

global $woocommerce; 
global $wpdb; 

$order = new WC_Order($order_id);
$order_items = $order->get_items(); 

// Loop through order line items
foreach( $order_items as $item_id => $item ) {

   $url = $item->get_meta(\'Check Detail\');  

   parse_str( parse_url( $url, PHP_URL_QUERY), $array );

   $key = strstr($array[\'ck_item\'], \'"\', true);

   $wpdb->check_detail = "{$wpdb->prefix}check_detail";

   $wpdb->delete($wpdb->check_detail, array(\'cart_item_key\' => $key));
 }
}

相关推荐