Last active
May 12, 2022 17:31
-
-
Save Preciousomonze/c0d49effe8e3d613f838aa35f352bfc9 to your computer and use it in GitHub Desktop.
This MU-Plugin helps change "out of stock" text to what you want either on single product page or loop page, or both. make sure you edit the $out_of_stock_span_class to your use case. Use at your risk, enjoy! I mean, what's life without taking risks? π Inspired by: https://woocommercecommunity.slack.com/archives/CV9AF72PR/p1651253141682729
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 | |
/** | |
* WooCommerce Change "out of stock" text. | |
* | |
* @package pekky-wc-change-out-of-stock-text | |
* @author Precious Omonzejele (CodeXplorer π€Ύπ½ββοΈπ₯π¦π€‘) | |
* | |
* @wordpress-plugin | |
* Plugin Name: WooCommerce Change "out of stock" text | |
* Plugin URI: https://gist.github.com/Preciousomonze/c0d49effe8e3d613f838aa35f352bfc9 | |
* Description: This MU-Plugin helps change "out of stock" text to what you want either on single product page or loop page, or both. make sure you edit the $out_of_stock_span_class to your use case. | |
* Author: Precious Omonzejele (CodeXplorer π€Ύπ½ββοΈπ₯π¦π€‘) | |
* Author URI: https://codexplorer.ninja | |
* Version: 1.0.0 | |
* Requires at least: 5.0 | |
* Tested up to: 5.9 | |
* WC requires at least: 5.0 | |
* WC tested up to: 6.0 | |
*/ | |
class Pekky_WC_Change_Out_Of_Stock_Text { | |
/** | |
* Alt Text to replace it with. | |
* | |
* Note: change this in init function, so it can support translation. | |
* | |
* @var string | |
*/ | |
public static $alt_text = ''; | |
/** | |
* Your out of stock parent class on loop page. | |
* | |
* Should be the closest parent element. | |
* | |
* @var string | |
*/ | |
public static $out_of_stock_parent_class = 'product'; | |
/** | |
* Your out of stock span class on loop page. | |
* | |
* This can be different depending on your theme. | |
* Mostly used for the shop/loop page. | |
* | |
* @var string | |
*/ | |
public static $out_of_stock_span_class = 'out-of-stock'; | |
/** | |
* Enable for Single Page. | |
* | |
* @var bool | |
*/ | |
public static $enable_single_page = true; | |
/** | |
* Enable for loop/shop page. | |
* | |
* @var bool | |
*/ | |
public static $enable_loop_page = true; | |
/** | |
* Boostrap and shoot. | |
*/ | |
public static function init() { | |
// Change out of text alt here. | |
self::$alt_text = __( 'Coming Soon', 'woocommerce' ); | |
if ( self::$enable_single_page ) { | |
add_filter( 'woocommerce_get_availability_text', array( __CLASS__, 'change_out_of_stock_text' ), 10, 2 ); | |
} | |
if ( self::$enable_loop_page ) { | |
add_action( 'woocommerce_after_shop_loop_item_title', array( __CLASS__, 'out_of_stock_alt' ), 10 ); | |
} | |
if ( ! empty( trim( self::$out_of_stock_span_class ) ) && ! empty( trim( self::$out_of_stock_parent_class ) ) ) { | |
// Load JS in WP Footer. | |
add_action( 'wp_footer', array( __CLASS__, 'js_script' ), 10 ); | |
} | |
} | |
/** | |
* Changes out of stock text. | |
* | |
* @param string $availability | |
* @param WC_Product $product | |
* @return string | |
*/ | |
public static function change_out_of_stock_text( $availability, $product ) { | |
if( ! $product->is_in_stock() ) { | |
$availability = self::$alt_text; | |
} | |
return $availability; | |
} | |
/** | |
* Alt text for "out of stock" | |
*/ | |
public function out_of_stock_alt() { | |
global $product; | |
if ( ! $product->is_in_stock() ) { | |
printf( '<span class="cx-pekky-coost-alt-text">%s</span>', self::$alt_text ); | |
} | |
} | |
/** | |
* Js script for manipulation. | |
* | |
* @hooked | |
*/ | |
public static function js_script() { | |
?> | |
<script type="text/javascript"> | |
jQuery( document ).ready( function( $ ) { | |
/** | |
* Change out of stock text. | |
*/ | |
function pekkyCOOSTAlternator() { | |
let $outOfStock = $( '<?php echo '.' . self::$out_of_stock_parent_class . ' .' . self::$out_of_stock_span_class; ?>' ); | |
let $altOutOfStock = $( '.<?php echo self::$out_of_stock_parent_class; ?> .cx-pekky-coost-alt-text' ); | |
// Do the change. | |
if ( 0 < $outOfStock.length && 0 < $altOutOfStock.length ) { | |
$outOfStock.text( $altOutOfStock.html() ); | |
$altOutOfStock.hide(); | |
} | |
} | |
pekkyCOOSTAlternator(); | |
}); | |
</script> | |
<?php | |
} | |
} | |
// Launch!!! | |
Pekky_WC_Change_Out_Of_Stock_Text::init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment