-
-
Save viniciusbig/028fbc1ad6392f8dfc106bcc3cc931a3 to your computer and use it in GitHub Desktop.
Create WooCommerce Subscription and activate Subscription for a user programatically.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function cpm_create_order($customer_data, $the_customer) { | |
global $woocommerce; | |
$product_id = $customer_data['sub_product']; | |
$variation_id = $customer_data['sub_variation']; | |
$user_first_name = $customer_data['first_name']; | |
$user_last_name = $customer_data['last_name']; | |
$user_email = $customer_data['user_email']; | |
$billing_email = $customer_data['user_email']; | |
$billing_state = $customer_data['cpm_state']; | |
$billing_post_code = $customer_data['cpm_zip']; | |
$billing_country = $customer_data['cpm_country']; | |
$variation_factory = new WC_Product_Variation($variation_id); | |
$variation_obj = $variation_factory->get_variation_attributes(); | |
$quantity = 1; | |
$user_id = $the_customer->user_id; | |
$order = wc_create_order(); | |
update_post_meta( $order->id, '_billing_first_name', $user_first_name ); | |
update_post_meta( $order->id, '_billing_last_name', $user_last_name ); | |
update_post_meta( $order->id, '_billing_email', $billing_email ); | |
update_post_meta( $order->id, '_billing_state', $billing_state ); | |
update_post_meta( $order->id, '_billing_postcode', $billing_post_code ); | |
update_post_meta( $order->id, '_shipping_first_name', $user_first_name ); | |
update_post_meta( $order->id, '_shipping_last_name', $user_last_name ); | |
$product_to_add = get_product( $product_id ); | |
$price = $variation_factory->get_price(); | |
$price_params = array( 'variation' => $variation_obj, 'totals' => array( 'subtotal' => $price*$quantity, 'total' => $price*$quantity, 'subtotal_tax' => 0, 'tax' => 0 ) ); | |
$order->add_product( wc_get_product( $product_id ), $quantity, $price_params ); // pid 8 & qty 1 | |
$order->set_address( $address, 'billing' ); | |
$order->set_address( $address, 'shipping' ); | |
$order->set_total( $price*$quantity , 'order_discount'); // not pennies (use dollar amount) | |
$order->set_payment_method($this); | |
$order->calculate_totals(); | |
update_post_meta( $order->id, '_customer_user', $user_id ); | |
cpm_create_subscription( $order, $product_id, $price_params, $variation_id ); | |
} | |
function cpm_create_subscription( $order, $product_id, $price_args, $variation_id ) { | |
$quantity = 1; | |
$start_date = $order->order_date; | |
$period = WC_Subscriptions_Product::get_period( $variation_id ); | |
$interval = WC_Subscriptions_Product::get_interval( $variation_id ); | |
$sub = wcs_create_subscription( array( 'order_id' => $order->id, 'billing_period' => $period, 'billing_interval' => $interval, 'start_date' => $start_date )) ; | |
$sub->add_product( wc_get_product( $product_id ), $quantity, $price_args ); | |
$sub->calculate_totals(); | |
WC_Subscriptions_Manager::activate_subscriptions_for_order($order); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment