我在数据库中有一个自定义表,我需要使用钩子“ woocommerce_subscription_renewal_payment_complete”在订阅续订付款时将product_id,user_id和订单信息插入该表。这样可以实现吗?还是任何人都知道其他相同的想法?
我检查了以下代码,并且未将其插入表中
function data_woocommerce_renewal( $subscription, $last_order ) {
if ( ! $subscription )
return;
$order_id = $subscription->get_parent_id();
$order = wc_get_order( $order_id );
$user_id = $subscription->get_user_id();
$order_items = $subscription->get_items();
// Loop through order items
foreach ( $order_items as $item_id => $item ) {
// To get the subscription variable product ID and simple subscription product ID
$product_id[] = $item->get_product_id();
}
// if( 'completed' === $order->get_status() ) {
$user_details = array(
'token' => 'ygfvygrffburb',
'true_or_false' => TRUE,
'product_id' => $product_id[0],
'customer_id' => $user_id
);
data_insert_test_data($user_details);
// }
}
add_action( 'woocommerce_subscription_renewal_payment_complete', 'data_woocommerce_renewal', 10, 1 );
下面的插入功能
//insert test data
function data_insert_test_data($user_details) {
global $wpdb;
$table_name = $wpdb->prefix . "test_data";
$result = $wpdb->insert($table_name, $user_details);
if ($result) {
return true;
} else {
return false;
}
}