-
-
Save damiencarbery/044584c2a3f36daff9ab0287ab190137 to your computer and use it in GitHub Desktop.
<?php | |
// Check for 'Hire' category for single product. | |
add_filter( 'dcwd_single_product_availability_text', 'dcwd_single_product_availability_check', 10, 2 ); | |
function dcwd_single_product_availability_check( $availability_text, $product ) { | |
if ( has_term( array( 'hire' ), 'product_cat', $product->get_id() ) ) { | |
return 'Unavailable'; | |
} | |
else { | |
return 'Sold'; | |
} | |
} | |
// Check for 'Hire' category for archives and Products element. | |
add_filter( 'dcwd_ux_products_sold_label', 'dcwd_archive_sold_label_blanked', 10, 2 ); | |
add_filter( 'dcwd_archive_sold_label', 'dcwd_archive_sold_label_blanked', 10, 2 ); | |
function dcwd_archive_sold_label_blanked( $message, $product ) { | |
if ( has_term( array( 'hire' ), 'product_cat', $product->get_id() ) ) { | |
return 'Unavailable'; | |
} | |
return 'Sold'; | |
} |
<?php | |
// Check for 'Retired' product tag for single product. | |
add_filter( 'dcwd_single_product_availability_text', 'dcwd_single_product_availability_check', 10, 2 ); | |
function dcwd_single_product_availability_check( $availability_text, $product ) { | |
if ( has_term( array( 'Retired' ), 'product_tag', $product_id ) ) { | |
return 'Retired'; | |
} | |
return $availability_text; | |
} | |
// Check for 'Retired' product tag for archives and Products element. | |
add_filter( 'dcwd_ux_products_sold_label', 'dcwd_archive_sold_label_blanked', 10, 2 ); | |
add_filter( 'dcwd_archive_sold_label', 'dcwd_archive_sold_label_blanked', 10, 2 ); | |
function dcwd_archive_sold_label_blanked( $message, $product ) { | |
if ( has_term( array( 'Retired' ), 'product_tag', $product->get_id() ) ) { | |
return 'Retired'; | |
} | |
return $message; | |
} |
<?php | |
/* | |
Plugin Name: Change Flatsome theme 'Out of Stock' label to 'Sold' | |
Plugin URI: https://www.damiencarbery.com/2020/06/change-flatsome-theme-out-of-stock-label-to-sold/ | |
Description: Change the 'Out of stock' banner in Flatsome theme to 'Sold'. Also works when [ux_products] shortcode is used. | |
Author: Damien Carbery | |
Author URI: https://www.damiencarbery.com | |
Version: 0.4 | |
*/ | |
defined( 'ABSPATH' ) || exit; | |
class FlatsomeOutOfStockToSold { | |
// Returns an instance of this class. | |
public static function get_instance() { | |
if ( null == self::$instance ) { | |
self::$instance = new self; | |
} | |
return self::$instance; | |
} | |
// Initialize the plugin variables. | |
public function __construct() { | |
// Wait until wp_head so that is_archive and similar conditionals are available. | |
add_action( 'wp_head', array( $this, 'init' ) ); | |
} | |
// Set up WordPress specfic actions. | |
public function init() { | |
// Declare that this plugin supports WooCommerce HPOS. | |
// This plugin does not interact with WooCommerce orders so it doesn't have to do anything special to support HPOS. | |
add_action( 'before_woocommerce_init', function() { | |
if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) { | |
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true ); | |
} | |
} ); | |
// Change the 'Out of Stock' text in the single product page. | |
if ( is_product() ) { | |
add_filter( 'woocommerce_get_availability_text', array( $this, 'get_availability_text' ), 10, 2 ); | |
} | |
// If the content has the [ux_products] shortcode then modify the markup and add the required CSS/JS to the footer. | |
if ( is_page() ) { | |
add_action( 'flatsome_product_box_after', array( $this, 'add_banner_after_product_box' ) ); | |
} | |
// In archives, add 'Sold' banner if product is out of stock. CSS will hide the 'Out of Stock' div. | |
if ( is_product_category() || is_product_tag() ) { | |
add_action( 'flatsome_woocommerce_shop_loop_images', array( $this, 'add_sold_label' ) ); | |
} | |
} | |
// Return whether a product has the Retired tag. | |
private function has_retired_tag( $product_id ) { | |
if ( has_term( array( 'Converse' ), 'product_tag', $product_id ) ) { | |
return true; | |
} | |
return false; | |
} | |
// In archives, add 'Sold' banner if product is out of stock. CSS will hide the 'Out of Stock' div. | |
public function add_sold_label() { | |
global $product; | |
if ( ! $product->is_in_stock() ) { | |
$message = 'Sold'; | |
$message = apply_filters( 'dcwd_archive_sold_label', $message, $product ); | |
// If the filter returns an empty string the default Out of Stock text is left untouched. | |
if ( ! empty( trim( $message ) ) ) { | |
echo '<div class="out-of-stock-label sold-label">' . esc_html( $message ) . '</div>'; | |
// Add the CSS to hide the 'Out of Stock' div. | |
add_action( 'wp_footer', array( $this, 'hide_out_of_stock_banner' ) ); | |
} | |
} | |
} | |
// Change the 'Out of Stock' text in the single product page too. | |
public function get_availability_text( $availability, $product ) { | |
if ( ! $product->is_in_stock() ) { | |
$availability = 'Sold'; | |
} | |
// Developers can use this filter to add their own criteria for the text. | |
return apply_filters( 'dcwd_single_product_availability_text', $availability, $product ); | |
} | |
// If the content has the [ux_products] shortcode then modify the markup and add the required CSS/JS to the footer. | |
public function add_banner_after_product_box() { | |
global $product; | |
if ( ! $product->is_in_stock() ) { | |
$message = 'Sold'; | |
$message = apply_filters( 'dcwd_ux_products_sold_label', $message, $product ); | |
// If the filter returns an empty string the default Out of Stock text is left untouched. | |
if ( ! empty( trim( $message ) ) ) { | |
echo '<div class="out-of-stock-label sold-label">' . esc_html( $message ) . '</div>'; | |
// Hide Out of Stock banner, style new one (including moving and rotating it). | |
add_action( 'wp_footer', array( $this, 'change_out_of_stock_banner' ) ); | |
} | |
} | |
//echo '<p>dcwd_product_box_after(): ' . $product->get_id() . '</p>'; | |
} | |
// ux_products shortcode: Hide Out of Stock banner, style new one (including moving and rotating it). | |
public function change_out_of_stock_banner() { | |
?> | |
<style> | |
/* Hide 'Out of stock' message and show new 'Sold' one. */ | |
.box-image .out-of-stock-label { opacity: 0; } | |
/* Style/position the new banner. */ | |
/* As the banner is a grandchild of the element we want to be relative to, need position:fixed - https://stackoverflow.com/a/25768682/8605943 */ | |
.product-small.box { transform: rotate(0deg); } | |
.product-small.box .box-text { position: absolute; } | |
/* Style the new banner - almost a copy of ".box-image .out-of-stock-label" styles. */ | |
.out-of-stock-label.sold-label { position: fixed; background-color: hsla(0,0%,100%,.9); color: #333; font-weight: 700; top: 40%; padding: 20px 0; width: 99%; transform: rotate(-20deg); left: 0px; text-align: center; text-transform: uppercase; } | |
</style> | |
<?php | |
} | |
// Archuves: Hide the 'Out of Stock' banner and show the 'Sold' one. | |
public function hide_out_of_stock_banner() { | |
?> | |
<style> | |
/* Hide 'Out of stock' message and show new 'Sold' one. */ | |
.box-image .out-of-stock-label { opacity: 0; } | |
/* Rotate the 'Sold' label. */ | |
.box-image .out-of-stock-label.sold-label { opacity: 0.9; transform: rotate(-20deg); width: 110%; margin-left: -5%; z-index: 10; } | |
</style> | |
<?php | |
} | |
} | |
$FlatsomeOutOfStockToSold = new FlatsomeOutOfStockToSold(); |
@damiencarbery yes, that works but you can have just one between "Sold" and "Unavailable" and not both. I would like to get both like in shop page.
@absentio : Can you contact me via https://www.damiencarbery.com/contact/ - I would like to see a page with both Sold and Unavailable to see how the markup differs. That will help me figure out what code to change.
Something broke with latest WP update (6.6). Doesn't work anymore on items that are being lazy-loaded.
Something broke with latest WP update (6.6). Doesn't work anymore on items that are being lazy-loaded.
I cannot reproduce this error. Here's screenshots of an Out of Stock product when the plugin is active and when it is in active.
https://snipboard.io/OW6odx.jpg
Please contact me via https://www.damiencarbery.com/contact/ if you want to investigate this further.
@absentio: That should already be working - the dcwd_change_out_of_stock_text() function changes 'Out of stock' to 'Sold'. You can change 'Sold' to 'Unavailable'.