Last active
January 25, 2022 14:17
-
-
Save andrewlimaza/bd22511d0811d10c286d9130deedd288 to your computer and use it in GitHub Desktop.
Add or Remove Addon Package access based on WooCommerce purchased products.
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 | |
/** | |
* Integrate Addon Packages with WooCommerce product purchased (based on the order status). | |
* Recommended to be used with PMPro WooCommerce to apply a default level for all products that give access to posts (Addon Packages). | |
* | |
* To get started, please adjust the $product_ids_to_posts global array which uses a product_id => post_id key/value pair. | |
* Extend this array for all products that will give access to posts. | |
* | |
* Adds access to the post once the order is completed. | |
* Removes access from the post if the order is set to cancelled, failed or refunded. | |
* | |
* | |
* You can add this recipe to your site by creating a custom plugin | |
* or using the Code Snippets plugin available for free in the WordPress repository. | |
* Read this companion article for step-by-step directions on either method. | |
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
*/ | |
global $product_ids_to_posts; | |
$product_ids_to_posts = array( | |
'63' => '217', // Product ID 63 gives access to post ID 217. | |
); | |
/** | |
* Assign access via Addon Packages when WooCommerce order completes. | |
* @param int $order_id The WooCommerce order ID available when changing order's status. | |
*/ | |
function my_pmproap_for_woo_add( $order_id ) { | |
global $product_ids_to_posts; | |
if ( ! function_exists( 'pmproap_addMemberToPost' ) ) { | |
return; | |
} | |
$order = new WC_Order( $order_id ); | |
$user_id = $order->get_user_id(); | |
if ( ! empty( $user_id ) && sizeof( $order->get_items() > 0 ) ) { | |
foreach ( $order->get_items() as $item ) { | |
if ( ! empty( $item['product_id'] ) ) { | |
if ( ! empty( $product_ids_to_posts[ $item['product_id'] ] ) ) { | |
pmproap_addMemberToPost( $user_id, $product_ids_to_posts[ $item['product_id'] ] ); | |
} | |
} | |
} | |
} | |
} | |
add_action( 'woocommerce_order_status_completed', 'my_pmproap_for_woo_add', 20, 1 ); | |
/** | |
* Remove access via Addon Packages when WooCommerce order status is failed, cancelled or refunded. | |
* @param int $order_id The WooCommerce order ID available when changing order's status. | |
*/ | |
function my_pmproap_for_woo_remove( $order_id ) { | |
global $product_ids_to_posts; | |
if ( ! function_exists( 'pmproap_addMemberToPost' ) ) { | |
return; | |
} | |
$order = new WC_Order( $order_id ); | |
$user_id = $order->get_user_id(); | |
if ( ! empty( $user_id ) && sizeof( $order->get_items() > 0 ) ) { | |
foreach ( $order->get_items() as $item ) { | |
if ( ! empty( $item['product_id'] ) ) { | |
if ( ! empty( $product_ids_to_posts[ $item['product_id'] ] ) ) { | |
pmproap_removeMemberFromPost( $user_id, $product_ids_to_posts[ $item['product_id'] ] ); | |
} | |
} | |
} | |
} | |
} | |
add_action( 'woocommerce_order_status_refunded', 'my_pmproap_for_woo_remove' ); | |
add_action( 'woocommerce_order_status_failed', 'my_pmproap_for_woo_remove' ); | |
add_action( 'woocommerce_order_status_cancelled', 'my_pmproap_for_woo_remove' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment