1
votes

That's a terrible title, sorry. It was the best I could come up with, though. I'm a bit stumped trying to accomplish this.

The relevant pieces of software are:

  • Woocommerce
  • Woocommerce Subscriptions Extension
  • Woocommerce Shipstation Integration Extension

My end goal is: each time a subscription is charged, shipstation should display the subscription's current "iteration" for that order, i.e. how many times the item has been charged as a part of the subscription.

So when a customer initially subscribes, shipstation should display a "1" associated with the item. If the subscription is renewing for the sixth month, and billed monthly, shipstation should display a "6" associated with the item.

Woocommerce Subscriptions does track this number, as shown on the 'Subscriptions' admin page. It's the number highlighted on the right side of this screenshot.

This number is generated by counting the number of related orders, so it's not technically counting renewals, but it winds up working out the same for this purpose.

This first problem is that this is on a 'subscription id' level, and to bring it to shipstation we'll have to move it to an 'order meta' level or an 'order item meta' level.

Since an order could theoretically have multiple subscriptions in it, I'm thinking that it wouldn't work out to put this on the 'order meta' level. So I'm thinking that it will have to be assigned to the meta information of the actual item within the order.

Shipstation looks like this, so it may not be feasible to display something on the order level, when you get down to the 'items ordered' view it does list some information like SKU and variations. I don't want to turn this number into an actual variation, but it seems like there should logically be some way to get the information to show up in the same row.

So I guess I have to:

1) On subscription purchase OR renewal, count the number of related orders to the associated subscription and store this somewhere in the 'order item meta'.

2) Then feed that information to shipstation each time an appropriate order is exported.

I've Googled endlessly, and found a few chunks of information that might be relevant in helping me to build the first step, but I'm really not sure on my course. I can't make this data an actual item variation, so should I be doing it as a custom item attribute?

Any help or pointers in the right direction would be greatly appreciated. I was a bit surprised that I couldn't find more people previously looking for similar functionality.

Thanks in advance!

1

1 Answers

1
votes

I figured this out. Took me way longer than it should have for something so simple, but it felt like a lot of documentation was missing and a lot of other use cases I could reference involved deprecated functions, etc.

I ended up putting this on the 'order meta' level rather than the 'order item meta' level after all since I realized that purchasing multiple subscription items simultaneously still bundles them into a single subscription with singular recurring orders.

For anyone that might want similar functionality in the future, this is working correctly for me now.

// woocommerce_subscription_payment_complete fires on both initial orders and renewals
add_action('woocommerce_subscription_payment_complete', 'add_iteration', 10, 2);
function add_iteration($subscription) {
  // $subscription_id is extraneous and can be removed
  $subscription_id = $subscription->id;
  // gather relevant data
  $payment_count = $subscription->get_completed_payment_count();
  $related_orders = $subscription->get_related_orders();
  $arrayKeys = array_keys($related_orders);
  $parent_order_id = end($related_orders);
  $current_order_id = $related_orders[$arrayKeys[0]];
  // add data to order meta
  update_post_meta($current_order_id, '_iterations', $payment_count);
}

// send it to shipstation
add_filter( 'woocommerce_shipstation_export_custom_field_2', 'shipstation_custom_field_2' );
function shipstation_custom_field_2() {
  return '_iterations';
}