Last active
July 29, 2024 21:02
-
-
Save damiencarbery/044584c2a3f36daff9ab0287ab190137 to your computer and use it in GitHub Desktop.
Change Flatsome theme 'Out of Stock' label to 'Sold' - Change the 'Out of stock' banner in Flatsome theme to 'Sold'. https://www.damiencarbery.com/2020/06/change-flatsome-theme-out-of-stock-label-to-sold/
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 | |
/* | |
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.3 | |
*/ | |
// Return whether a product is in the Hire category. | |
function is_hire_category( $product_id ) { | |
if ( has_term( array( 'hire' ), 'product_cat', $product_id ) ) { | |
return true; | |
} | |
return false; | |
} | |
// Add 'Sold' banner if product is out of stock. CSS will hide the 'Out of Stock' div. | |
add_action( 'flatsome_woocommerce_shop_loop_images', 'dcwd_add_sold_label' ); | |
function dcwd_add_sold_label() { | |
global $product; | |
if ( ! $product->is_in_stock() ) { | |
$message = 'Sold'; | |
// Mark hire items as Unavailable instead of Sold. | |
if ( is_hire_category( $product->get_id() ) ) { | |
$message = 'Unavailable'; | |
} | |
echo '<div class="out-of-stock-label sold-label">'.$message.'</div>'; | |
// Add the CSS to hide the 'Out of Stock' div. | |
add_action( 'wp_footer', 'dcwd_hide_out_of_stock_banner' ); | |
} | |
} | |
// Change the 'Out of Stock' text in the single product page too. | |
add_filter( 'woocommerce_get_availability_text', 'dcwd_get_availability_text', 10, 2 ); | |
function dcwd_get_availability_text( $availability, $product ) { | |
if ( ! $product->is_in_stock() ) { | |
// Mark hire items as Unavailable instead of Sold. | |
if ( is_hire_category($product->get_id() ) ) { | |
return 'Unavailable'; | |
} | |
return 'Sold'; | |
} | |
return $availability; | |
} | |
// If the content has the [ux_products] shortcode then add the required CSS/JS to the footer. | |
add_filter( 'the_content', 'dcwd_check_ux_products_shortcode', 5 ); | |
function dcwd_check_ux_products_shortcode( $content ) { | |
if ( has_shortcode( $content, 'ux_products' ) ) { | |
add_action( 'wp_footer', 'dcwd_change_out_of_stock_banner' ); | |
add_filter( 'the_content', 'dcwd_change_out_of_stock_text', 20 ); | |
} | |
return $content; | |
} | |
// Filter the page content to change the 'Out of stock' div with a 'Sold' one. | |
function dcwd_change_out_of_stock_text( $content ) { | |
return str_replace( '<div class="out-of-stock-label">Out of stock</div>', '<div class="out-of-stock-label sold-label">Sold</div>', $content ); | |
} | |
// Rotate the label with CSS. | |
function dcwd_change_out_of_stock_banner() { | |
?> | |
<style> | |
/* Rotate the 'Sold' label. */ | |
.row-masonry .out-of-stock-label.sold-label { /*opacity: 0.9;*/ transform: rotate(-20deg); width: 110%; margin-left: -5%; z-index: 10; } | |
</style> | |
<?php | |
} | |
// Hide the 'Out of Stock' banner and show the 'Sold' one. | |
//add_action( 'wp_footer', 'dcwd_hide_out_of_stock_banner' ); | |
function dcwd_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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.