Created
June 11, 2023 11:00
-
-
Save Archie22is/62c0ca5db036ec378dce760421f56056 to your computer and use it in GitHub Desktop.
Disable certain prodicts from being sold based on a custom ACF date field.
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
<?php | |
/** | |
* | |
* | |
* | |
*/ | |
function set_end_order_date ( $is_purchasable, $product ){ | |
// Set time zone | |
//$timezone = date_default_timezone_set('Europe/London'); | |
$timezone = get_option('timezone_string'); | |
date_default_timezone_set($timezone); | |
// Get product Id | |
$product_id = $product->get_id(); | |
// For product variations (from variable products) | |
if ( $product->is_type('variation') ){ | |
$parent = wc_get_product( $product->get_parent_id() ); | |
$product_id = $parent->get_id(); | |
} | |
// Get ACF date field from product | |
//$end_order_date = get_field( 'end_order_date', $product_id ); | |
$end_order_date = get_post_meta($product_id, 'end_order_date', true ); | |
// Check logic if the end date is set | |
if( $end_order_date ) { | |
// Convert to new end date format because ACF is poop | |
$end_order_date_convert = date( "d-m-Y", strtotime($end_order_date) ); | |
// And then convert to timestamp | |
$new_end_order_date = abs( strtotime( $end_order_date_convert ) ); | |
// get today's date in timestamp format | |
$today_date = date("d-m-Y"); | |
$today_date = abs( strtotime($today_date) ); | |
// And then convert to timestamp | |
$new_end_order_date = abs( strtotime( $end_order_date_convert ) ); | |
// check to see if ACF date has passed - if it has, remove "Add to cart" | |
if ( $new_end_order_date < $today_date ) { | |
//return false; | |
$is_purchasable = false; | |
} | |
} | |
return $is_purchasable; | |
} | |
add_filter('woocommerce_is_purchasable', 'set_end_order_date', 23, 2 ); | |
//add_filter('woocommerce_variation_is_purchasable', 'set_end_order_date', 23, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment