Skip to content

Instantly share code, notes, and snippets.

@plugin-republic
Created November 5, 2018 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plugin-republic/5e01f97428073fe72dd6ae1ca73b490a to your computer and use it in GitHub Desktop.
Save plugin-republic/5e01f97428073fe72dd6ae1ca73b490a to your computer and use it in GitHub Desktop.
How to find the products associated with a WooCommerce subscription
/**
* How to get the product associated with a WooCommerce subscription
*/
function ct_checkout_subscription_created( $subscription, $order, $recurring_cart ) {
$id = $subscription->get_id(); // You can use this to set meta in the subscription
// Get the products in the order
$items = $order->get_items();
foreach( $items as $item ) {
$product = $item->get_product();
$product_id = $product->get_id();
// ... Do your stuff here
}
}
// Use any hook that passes the $subscription object
add_action( 'woocommerce_checkout_subscription_created', 'ct_checkout_subscription_created', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment