Woocommerce subscription hook after renewal
-
I’m very new to WordPress and PHP, so I’ve been reading a ton and trying to absorb as much as I can. I know I’m quite close with what I want to achieve but not quite there.
So I have a web store where users can purchase subscriptions (1 month, 6 months, 1 year). My store uses Woocommerce Subscriptions v2.0.8. What I want to achieve is when a subscription is renewed, I want to update a separate mysql database with some information. Basically my database has a list of all active subscriptions, with their subscription id and days left. I believe this needs to be done through actions and hooks. My function is below
add_action( 'processed_subscription_payment', 'updatedays', 10, 1 ); function updatedays(string $subscription_id) { //gets product_id after the underscore $productid = substr($subscription_id, strpos($subscription_id, "_") + 1); $days = 0; if ($productid == 126){ $days = 1; }else if ($productid == 49){ $days = 30; }else if ($productid == 54){ $days = 183; }else if ($productid == 59){ $days = 365; } $servername = "localhost"; $username = "root"; $password = "pass"; $dbname = "database"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "UPDATE users SET days_left= '$days' WHERE subscription_key = '$subscription_id'"; if ($conn->query($sql) === TRUE) { echo "Record updated successfully"; } else { echo "Error updating record: " . $conn->error; } $conn->close(); }
So my function will accept the subscription key which is made up of the order id + product id. I split this so I can do some work with the product id. Depending on the product, we will set a specific number of days left. Afterwards I want to update the database with this number. The function on it’s own works correctly if I pass a predefined subscription id. I placed the above in my theme’s functions.php file.
You can also see I have my add_action which hooks into the processed_subscription_payment. However, in order to pass parameters to the function, I need to have a do_action which will take the parameter.
do_action('processed_subscription_payment', $subscription_id);
I do not know where to place this line. I tried adding it as well to the functions.php using a predefined subscription_id as a parameter which I know exists in my database, but then the function runs every time I refresh my site’s pages, rather than only when a subscription is processed.
And like I mentioned, I was testing with a predefined sub_id, I do not know how to pass the actual subscription_id of the renewed subscription.
I came across this to get the subscription id
https://docs.woothemes.com/document/subscriptions/develop/functions/management-functions/
but it does not show how to integrate it into my code.
Any help is appreciated!
[ No bumping please. ]
- The topic ‘Woocommerce subscription hook after renewal’ is closed to new replies.