我想更改我的 WooCommerce 网站中的 webhook 数据,但它不起作用。我想确定是否有某种产品(ID 为 20627),该产品不会出现在 webhook 的响应中。问题是,它仍然在 webhook 响应的“line_items”部分显示该产品。怎么会?
这是我的代码:
add_action( 'woocommerce_payment_complete', 'my_custom_webhook_handler' );
add_action( 'woocommerce_order_status_completed', 'my_custom_webhook_handler' );
function my_custom_webhook_handler( $order_id ) {
// get the order object
$order = new WC_Order( $order_id );
// modify the webhook data
add_filter( 'woocommerce_rest_prepare_webhook', function( $response, $webhook ) use ( $order ) {
// get the data that is being sent in the webhook
$data = $response->get_data();
// remove products from the data that match certain criteria
foreach ( $data['line_items'] as $key => $line_item ) {
$product_id = $line_item['product_id'];
// check if the product matches the criteria to be removed
if ( $product_id == 20627 ) {
unset( $data['line_items'][ $key ] );
}
}
// set the modified data back into the response object
$response->set_data( $data );
return $response;
}, 10, 2 );
}
当我改变“$response->set_data($data);”到 $response->set_data('');" 例如,它甚至不显示空字符串。