WooCommerce Payment Gateway: Subscription Renewal Date Not Updated - woocommerce

I'm writing a payment gateway that handles WooCommerce subscriptions. Here is the sample code that handles the renewal:
public function mcc_subscription_renewal($amount, $order) {
$tid = 123;
$order->payment_complete( $tid );
$order->update_status( 'completed' );
WC_Subscriptions_Manager::process_subscription_payments_on_order( $order );
}
This is for a simple subscription that charges the customer a fixed fee every year. When I manually run the renewal process, a new order and scheduled action are created successfully, but the date isn't updated to the next year. What am I missing? Why isn't a successfully completed renewal order updating the expiry date of a subscription?

Assuming you have not found an answer yet, a couple of things. Are you just writing the gateway OR are you writing the gateway and a subscription process?
Woocommerce has a addon called Woocommerce subscription. It does all the billing to a gateway, which at the moment are only Stripe/PayPal. If you are writing a new gateway and have a woocommerce subscription add on, then all you need to do is to look at the subscription id and the next payment date:
public function mcc_subscription_renewal($amount, $order)
{
$order = new WC_Order( $order );
$OrderNumber = $order->parent_id;
$ParentOrder = new WC_Order( $OrderNumber ); // ORDER NUMBER
$SubscriptionNumber = $order->get_order_number();
$PaymentDate = $order->get_date_created()->format ('Y-m-d');
$items = $order->get_items();
foreach( $items as $item_id => $product )
{
$ProductName = $product->get_name();
$ProductId = $product->get_product_id();
$ExpireDate = WC_Subscriptions_Order::get_next_payment_date ( $ParentOrder, $ProductId );
}
}
You don't have to do anything else. If you are writing your own subscription package, then you need to write the calls and store the purchases ..thus calculate your own expiration dates.
The woocommerce subscription, the length of time and the renewal period is all set under the products definition.

Related

Run a custom code after a product is sold in woocommerce

I need to know how to assign custom actions (or run custom code) when someone buys something in the website and the payment has been completed. I need this so our system can send order's data to an API.
i am using wordpress and woocommerce for the website.
how can i find the order dynamics variable to put it in my API's code ?
if there,s anyone who use wordpress and woocommerce, please tell me where can i find order's dynamic variables such as:
product id which the customer has been ordered
product count and etc.
You could write a function that connects to the woocommerce_order_status_processing hook. At this point, the payment has been accepted and WooCommerce is waiting for the store to fulfill the order.
add_action( 'woocommerce_order_status_processing', 'my_order_complete_function', 10, 1 );
function my_order_complete_function( $order_id ) {
$order = wc_get_order( $order_id );
foreach($order->get_items() as $item) {
$product_id = $item->get_product_id();
$product = wc_get_product( $product_id );
// Add your API call here.
}
}

Gifting for WooCommerce Subscriptions Add User Role

I'm using Gifting for WooCommerce Subscriptions which makes it possible for one person to purchase a subscription product for someone else.
When the subscription is purchased, I add a user role to the user who purchased the product:
add_action( 'woocommerce_order_status_completed', 'add_role_on_purchase' );
function add_role_on_purchase( $order_id ) {
$order = wc_get_order( $order_id );
$items = $order->get_items();
$products_to_check = array( '12345' );
foreach ( $items as $item ) {
if ( $order->user_id > 0 && in_array( $item['product_id'], $products_to_check ) ) {
$user = new WP_User( $order->user_id );
// Add role
$user->add_role( 'purchaser' );
// Exit the loop
break;
}
}
}
Gifting for WooCommerce Subscriptions creates a user and a user account for the recipient.
I need to add a user role to the recipient as well. Can this be done after the purchaser's subscription is set to active?
Buying a subscription as a gift, a buyer provides a recipient's email address. You can take the provided email and find the recipient using the get_user_by() function. Having a WP_User instance of the recipient, you can repeat the same logic you do for buyers.
A recipient email address provided by a buyer is probably stored somewhere in an order's meta.

woocommerce get 'billing_phone' field on checkout page

add_action('woocommerce_order_status_completed', 'my_order_status_completed_sms', 10, 1);
function my_order_status_completed_sms($order_id){
$order = wc_get_order($order_id);
$user_id = $order->get_customer_id();
if($user_id){
$phone_number = get_user_meta($user_id, 'billing_phone', true);
if($phone_number){
cosmosfarm_members_sms_send($phone_number, '#'.$order_id.' is complete. thank you!');
}
}
}
When someone purchases from a woocommerce, woocommerce send sms by plug-in.
but there's some issues.
If the buyer is a member, sms will be sent out normally, no problem.
However, if the buyer is not a guest, no sms will be sent.
What's the problem?
Your function is dependend upon having user_id and getting the user_meta for that user.
function my_order_status_completed_sms( $order_id ) {
// Get the order object
$order = wc_get_order( $order_id );
// Get the billing phone number from the order
$phone_number = $order->get_billing_phone();
if ( $phone_number ) {
cosmosfarm_members_sms_send( $phone_number, '#' . $order_id . ' is complete. thank you!' );
}
}
Using the order object, the biling phone is there if entered for either customer or guest.

Add customer as recipient to woocommerce subscriptions cancelled subscription email (notification)

By default woocommerce subscriptions send the email notification that a subscription has been cancelled only to the admin of the store, NOT to the customer. This is not user friendly. I am trying to add the customer as a recipient to this specific email notification, without success so far.
I have tried writing add a filter with PhP to the action cancelled_subscription_notification . Which i believe handles the email notification for cancelled subscriptions.
add_filter( 'cancelled_subscription_notification',
'wc_cancelled_order_add_customer_email', 10, 2 );
function wc_cancelled_order_add_customer_email( $recipient, $order ){
// Avoiding errors in backend (mandatory when using $order argument)
if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;
// Get the customer ID
$user_id = $order->get_user_id();
// Get the user data
$user_data = get_userdata( $user_id );
return $recipient;
}
When using this code (added in functions.php) i get an error 500 when i cancel a subscription (the actual email to admin is also not send!).
Useful maybe: https://github.com/wp-premium/woocommerce-subscriptions/blob/master/includes/emails/class-wcs-email-cancelled-subscription.php (link to cancelled-subscription calls)

Woocommerce: Which function create an order?

I'm working on an android app based on a 3 Tier architecture. I have set up a products page and the Pay Now button integrated with PayPal. I also receive Success message after successful payment.
I'm now stuck at the order function. Which function of Woocommerce makes the order?
I'm simply trying to pass the price, product_id and other details to that particular function which will take care of the rest of the process.
Any help would be much appreciated. Thank you.
Here is some code that will generate an order. You'll have to substitute in your payment gateway class
// create a new checkout instance and order id
global $current_user;
$product_id = $_POST['productId'];
$product = wc_get_product($product_id);
$order = wc_create_order(array(
'customer_id' => $current_user->ID,
));
$order->add_product( $product, 1 );
$order->calculate_totals();
$my_gateway = new WC_Gateway_WhateverGatewayYouAreUsing();
$payment = $my_gateway->process_payment($order->id);
if($payment["result"] == "success") {
$order->update_status('completed');
wc_add_notice("Thank you!");
}
else {
$order->update_status("cancelled");
wc_add_notice("oh no! plz send me da moneez", 'notice');
}

Resources