Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A snippet of code for use with WooCommerce and Advanced Custom Fields to make a product discontinued or forthcoming.
<?php
function is_released($id) {
$status = get_field('product_status', $id);
// We consider null to be for sale, as this indicates an old product
// where we haven't made a selection yet
if (is_null($status) || $status === "forsale") {
return True;
} else if ($status === "forthcoming") {
/* A product can also be released if it was forthcoming
with an 'available from' date that has passed */
$release_date = strtotime(get_field('available_from', $id));
// Check that a release date was set, and that the date has passed.
return $release_date && time() > $release_date;
}
return False;
}
function is_forthcoming($id) {
$status = get_field('product_status', $id);
if ($status === "forthcoming") {
$release_date = strtotime(get_field('available_from', $id));
// Either no release date was set, or the date has not passed.
return !$release_date || time() < $release_date;
}
return False;
}
function is_discontinued($id) {
$status = get_field('product_status', $id);
return $status === "discontinued";
}
/* Prevent purchase of products that have not been released */
add_filter('woocommerce_is_purchasable', 'lifecycle_woocommerce_is_purchasable', 10, 2);
function lifecycle_woocommerce_is_purchasable($is_purchasable, $product) {
return $is_purchasable && is_released($product->get_id());
}
add_filter('woocommerce_variation_is_purchasable', 'lifecycle_woocommerce_variation_is_purchasable', 10, 2);
function lifecycle_woocommerce_variation_is_purchasable($is_purchasable, $product) {
return $is_purchasable && is_released($product->get_parent_id());
}
/* Remove the add to cart buttons... We add it back later when needed */
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
/* Replace the add to cart buttons with our custom text */
add_filter( 'woocommerce_single_product_summary', 'lifecycle_single_add_to_cart', 30 );
/* Helper functions to generate the discontinued replacement products list: */
function id_to_link($product_id){
$product = wc_get_product( $product_id );
return '<a href="'. get_permalink( $product_id ) .'">' . $product->get_title() . '</a>';
}
function lifecycle_single_add_to_cart() {
global $product;
$id = $product->get_id();
$status = get_field('product_status', $id);
if ( is_released($id) ) {
/* Product is released, so use the default add to cart buttons */
woocommerce_template_single_add_to_cart();
} else if ( is_discontinued($id) ) {
// Replaced with is an array containing the ids of replacement products
$replaced_with = get_field('replaced_with', $id);
if($replaced_with) {
// Turn the array of id's of replacement products into an array of links
$titles_as_links = array_map(function ($product_id) {return id_to_link($product_id);}, $replaced_with);
// We want to join the array to follow the rules of sentences, like this:
// item 1, item 2, and item 3
// ... rather than just item 1, item 2, item 3, like regular implode would.
if ( count($titles_as_links) < 3 ) {
$sentence = implode (" and ", $titles_as_links);
} else {
$last = array_pop($titles_as_links);
$sentence = implode(", ", $titles_as_links);
$sentence .= ", and " . $last;
}
// Echo it
echo '<span class="discontinued">This product has been discontinued. It has been replaced by ' . $sentence . '.</span>';
} else {
echo '<span class="discontinued">This product has been discontinued.</span>';
}
} else {
// Product is forthcoming
// Release date here depends on ACF return value for available_from being set
// appropriately to display nicely.
$release_date = get_field('available_from', $id);
if ($release_date !== "") {
echo '<span class="forthcoming">This product is forthcoming and will be available from ' . $release_date . '</span>';
} else {
echo '<span class="forthcoming">This product is forthcoming and will be available soon!</span>';
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.