Last active
July 10, 2016 21:22
-
-
Save hiddenkirby/cf6af7c6292ae45663f97f681ca3e0cd to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// hooking woocommerce purchase to updating the users sub level | |
function get_package_title_from_product_slug($product_slug) { | |
// fastest way is manual way... also shittiest way .. very fragile | |
//_log('getting package title for: -->' . $product_slug . '<--'); | |
$result = ''; | |
switch ($product_slug) { | |
case 'Agent - 1 Property Listing': | |
$result = 'Agent (1 property)'; | |
break; | |
case 'Agent - 5 Property Listings': | |
$result = 'Agent (5 properties)'; | |
break; | |
case 'Agent - Unlimited Property Listings': | |
$result = 'Agent (unlimited)'; | |
break; | |
case 'Homeowner – 3 Month Listing': | |
$result = 'Homeowner (3 mo)'; | |
break; | |
case 'Homeowner – 6 Month Listing': | |
$result = 'Homeowner (6 mo)'; | |
break; | |
case 'Homeowner – 12 Month Listing': | |
$result = 'Homeowner (12 mo)'; | |
break; | |
default: | |
$result = 'Homeseeker'; | |
} | |
//_log('Returning package title as: ' . $result); | |
return $result; | |
} | |
function woocommerce_subscription_update_hook($order_id = 0){ | |
$user_id = get_current_user_id(); | |
//get product slug from order_id | |
$order = new WC_Order( $order_id ); | |
$items = $order->get_items(); | |
$product_slug = ''; | |
foreach ( $items as $item ){ | |
$product_slug = $item['name']; | |
} | |
// need $wc_package_title from wc slug | |
$wc_package_title = get_package_title_from_product_slug($product_slug); | |
$packages = Horizon_Packages_Logic::get_packages_choices(); | |
foreach ( $packages as $package_key => $package_value ){ | |
if ($wc_package_title == $package_value){ | |
_log('Setting ' . $user_id . '\'s package to: ' . $wc_package_title); | |
//$user_id, $package_id required | |
do_action('horizon_update_user_subscription', $user_id, $package_key ); | |
} | |
} | |
} | |
add_action('woocommerce_payment_complete', 'woocommerce_subscription_update_hook'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment