Last active
December 27, 2021 14:50
-
-
Save 10horizons/53386c7cce9fa47c8f738ab0f9e1ab54 to your computer and use it in GitHub Desktop.
[Woocommerce] Change customer's download expiry date based on quantity purchased by customer, where quantity equals year. Fires after successful checkout but right before showing the thank you page.
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 thp_wc_change_download_expiry_based_on_quantity( $order_id ) { | |
$order = wc_get_order( $order_id ); | |
foreach ($order->get_items() as $item_id => $item ) { | |
$product = $item->get_product(); | |
$product_id = $product->get_id(); | |
if ($product_id == 119) { //product ID | |
$item_quantity = $item->get_quantity(); | |
$data_store = WC_Data_Store::load( 'customer-download' ); | |
$download_permissions = $data_store->get_downloads( | |
[ | |
'order_id' => $order->get_id() | |
] | |
); | |
if ( $download_permissions && count( $download_permissions ) > 0 ) { | |
foreach ( $download_permissions as $download ) { | |
//$new_expiry = date( 'Y-m-d H:i:s', strtotime( '+ '. ( $item_quantity * 365 ) .' days' ) ); | |
$new_expiry = date( 'Y-m-d H:i:s', strtotime( '+ '.$item_quantity.' years' ) ); | |
$download->set_access_expires( $new_expiry ); | |
$download->save(); | |
} | |
} | |
} | |
} | |
} | |
add_action( 'woocommerce_before_thankyou', 'thp_wc_change_download_expiry_based_on_quantity' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment