Provide the ability to have subscriptions purchased with a qty. such as 25 users for email at 5 each months etc. it would be nice to have max and min or price furring on qty as well
Open
Last updated: January 18, 2013
Log in to comment on this feature request.
‘Hi Dan K,
If you or the community is still after a solution here are some pointers. Start by creating a few functions and bind these to hooks (“add_action”).
Note: I have removed the plugin name due to NDA. Replace [pluginname] with your own tag i.e. “MYPLUGIN”
The code below performs the following tasks:
– adds a field to a simple subscription product details panel, via “woocommerce_product_options_general_product_data” hook
– captures the saving of the post data “save_post” hook
– saves the meta data of the subscription to the order item meta, via the “woocommerce_order_status_completed” hook
/* code start */
/* Check if WooCommerce is active */
if ( in_array( ‘woocommerce/woocommerce.php’, apply_filters( ‘active_plugins’, get_option( ‘active_plugins’ ) ) ) ) {
/* WP Subscription Hooks */
add_action( ‘woocommerce_product_options_general_product_data’, ‘simple_[pluginname]_product_fields’ );
add_action( ‘save_post’, ‘[pluginname]_save_meta’, 11 );
add_action( ‘woocommerce_order_status_completed’, ‘[pluginname]_order_status_completed’ );
}
function simple_[pluginname]_product_fields() {
global $post;
echo ‘
// User Seat Count
woocommerce_wp_text_input( array(
‘id’ => ‘_[pluginname]_seat_license_count’,
‘class’ => ‘wc_input_subscription_price’,
‘wrapper_class’ => ‘_subscription_trial_length_field’,
‘label’ => __( ‘License Seat Count’, $[pluginname]_options[‘text_domain’] ),
‘placeholder’ => __( ‘e.g. 10’, $[pluginname]_options[‘text_domain’] ),
‘desc_tip’ => true,
‘description’ => __( ‘With each subscription you can set the amount of seats or licenses that are offered. Enter the required license count for this subscription.’, $[pluginname]_options[‘text_domain’] ),
)
);
echo ‘
‘;
echo ‘
‘;
}
function [pluginname]_save_meta( $post_id ) {
if ( ! WC_Subscriptions_Product::is_subscription( $post_id ) ) {
return;
}
if ( isset($_POST[‘_[pluginname]_seat_license_count’]) && !is_array( $_POST[‘_[pluginname]_seat_license_count’] ) ) {
$[pluginname]_meta_seat_license = preg_replace(‘/[^0-9]/’, ”, $_POST[‘_[pluginname]_seat_license_count’]);
if ( !empty($[pluginname]_meta_seat_license) ) {
$[pluginname]_meta_seat_license = absint($[pluginname]_meta_seat_license);
update_post_meta( $post_id, ‘_[pluginname]_seat_license_count’, $[pluginname]_meta_seat_license );
}
}
}
function [pluginname]_order_status_completed($order_id) {
global $wpdb;
$order_items = $wpdb->get_results( $wpdb->prepare(“SELECT order_item_id FROM ” . $wpdb->prefix . “woocommerce_order_items WHERE order_id = %d”, $order_id) );
foreach ( $order_items as $order_item ) {
error_log(‘order_item_id: ‘ . $order_item->order_item_id);
$order_item_id = $order_item->order_item_id;
// If a variation is defined get the id
$[pluginname]_meta_product_tag = ”;
$[pluginname]_meta_seat_license = 0;
$product_id = WC_Order::get_item_meta( $order_item_id, ‘_product_id’, true );
$variation_id = WC_Order::get_item_meta( $order_item_id, ‘_variation_id’, true );
if ( !empty($variation_id) ) {
$[pluginname]_meta_seat_license = get_post_meta($variation_id, ‘_[pluginname]_seat_license_count’, true);
} else if ( !empty($product_id) ) {
$[pluginname]_meta_seat_license = get_post_meta($product_id, ‘_[pluginname]_seat_license_count’, true);
}
if ( !empty($[pluginname]_meta_product_tag) && $[pluginname]_meta_seat_license != 0 ) {
woocommerce_add_order_item_meta( $order_item_id, ‘_[pluginname]_seat_license_count’, $[pluginname]_meta_seat_license, true );
}
}
}
/* code end */
You can then grap the seat count/subscription data using the following code.
// Get the Subscriptions for this user
$userobject_subscriptions = array();
$user_id = 1; // user id or you current_user()
$woo_subscriptions = WC_Subscriptions_Manager::get_users_subscriptions( $user_id );
foreach ($woo_subscriptions as $active_subscription) {
$woo_subscription_detail = array(
‘product_id’ => intval($active_subscription[‘product_id’]),
‘order_id’ => intval($active_subscription[‘order_id’]),
‘status’ => $active_subscription[‘status’],
‘product_tag’ => ‘UNKNOWN_PRODUCT’,
‘seat_license_count’ => 0,
);
$order_items = $wpdb->get_results( $wpdb->prepare(“SELECT order_item_id FROM ” . $wpdb->prefix . “woocommerce_order_items WHERE order_id = %d”, $woo_subscription_detail[‘order_id’]) );
foreach ( $order_items as $order_item ) {
$order_item_id = $order_item->order_item_id;
$[pluginname]_meta_seat_license = 0;
$product_id = WC_Order::get_item_meta( $order_item_id, ‘_product_id’, true );
$variation_id = WC_Order::get_item_meta( $order_item_id, ‘_variation_id’, true );
if ( !empty($variation_id) ) {
$woo_subscription_detail[‘seat_license_count’] = get_post_meta($variation_id, ‘_[pluginname]_seat_license_count’, true);
} else if ( !empty($product_id) ) {
$woo_subscription_detail[‘seat_license_count’] = get_post_meta($product_id, ‘_[pluginname]_seat_license_count’, true);
}
}
// Push the subscription information to the array
$userobject_subscriptions[] = $woo_subscription_detail;
}
Any questions, just post them here. I will try and answer them.