How can i insert the data of the subscription renewal order using “woocommerce_subscription_renewal_payment_complete” hook

Question

I have a custom table in my db and i need to insert the product_id, user_id & order information to that table on the time of subscription renewal payment using the hook “woocommerce_subscription_renewal_payment_complete”. is that possible to implement this way? or anyone knows any other idea for the same??

i checked with the below code, and its not inserting into the table

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 );

Insertion function below

//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;
    }
}
0
JunDevWordp 4 months 0 Answers 24 views 0

Leave an answer