Skip to content

Commit

Permalink
v4.3.0 - Allows displaying badge on backorder products
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlieEtienne committed Mar 22, 2022
1 parent bc8d174 commit 18c8ced
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 13 deletions.
42 changes: 37 additions & 5 deletions classes/Badge.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public static function get_css(): string {
Badge::single_css_position()
);


// alternative method (pure CSS)
if ( Settings::use_alt_method() ) {
// Product Loop AND Single product CSS
Expand Down Expand Up @@ -118,16 +117,29 @@ public static function get_selectors( string $selector = "" ): string {
"single" => ".single-product .wcsob_soldout" // Single product
];

return Badge::get_css_classes($selector);
return Badge::get_css_classes( $selector );
}

public static function get_alt_selectors( string $selector = "" ): string {

$loop_selectors = [];
$single_selectors = [];

if( self::showOnOutofstock()) {
$loop_selectors[] = '.woocommerce .product.outofstock .woocommerce-LoopProduct-link:before';
$single_selectors[] = "." . WCSOB::$outofstock_class_single . " .woocommerce-product-gallery:before";
}
if( self::showOnBackorder()) {
$loop_selectors[] = '.woocommerce .product.onbackorder .woocommerce-LoopProduct-link:before';
$single_selectors[] = "." . WCSOB::$backorder_class_single . " .woocommerce-product-gallery:before";
}

Badge::$selectors = [
"loop" => ".woocommerce .product.outofstock .woocommerce-LoopProduct-link:before", // Products loop
"single" => "." . WCSOB::$outofstock_class_single . " .woocommerce-product-gallery:before" // Single product
"loop" => implode( ', ', $loop_selectors), // Products loop
"single" => implode( ', ', $single_selectors) // Single product
];

return Badge::get_css_classes($selector);
return Badge::get_css_classes( $selector );
}

public static function get_css_classes( string $selector = "" ): string {
Expand All @@ -137,4 +149,24 @@ public static function get_css_classes( string $selector = "" ): string {

return implode( ', ', Badge::$selectors );
}

/**
* @param $product
*
* @return bool
*/
public static function shoudDisplay( $product ): bool {
return (
( self::showOnOutofstock() && ! $product->is_in_stock() ) ||
( self::showOnBackorder() && $product->is_on_backorder() )
);
}

public static function showOnOutofstock(): bool {
return empty( Settings::get_behaviour() ) || ( is_array( Settings::get_behaviour() ) && in_array( 'out-of-stock', Settings::get_behaviour() ) );
}

public static function showOnBackorder(): bool {
return is_array( Settings::get_behaviour() ) && in_array( 'backorder', Settings::get_behaviour() );
}
}
11 changes: 11 additions & 0 deletions classes/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ public static function add_plugin_settings_page() {
Field::make( 'checkbox', 'wcsob_hide_sale_flash', __( 'Hide Sale badge?', 'sold-out-badge-for-woocommerce' ) )
->set_help_text( __( 'Do you want to hide the "Sale!" badge when a product is sold out?', 'sold-out-badge-for-woocommerce' ) . ' ' . __( 'Checked by default.', 'sold-out-badge-for-woocommerce' ) )
->set_default_value( true ),
Field::make( 'multiselect', 'wcsob_behaviour', __( 'Behaviour', 'sold-out-badge-for-woocommerce' ) )
->set_options( [
'out-of-stock' => __( 'Display on "out of stock" products (default)', 'sold-out-badge-for-woocommerce' ),
'backorder' => __( 'Display on "available on backorder" products', 'sold-out-badge-for-woocommerce' ),
] )
->set_default_value( 'out-of-stock' )
->set_help_text( __( 'Choose whether to display badge on "out of stock" products (default) or/and on "available on backorder" products', 'sold-out-badge-for-woocommerce' ) ),
Field::make( 'checkbox', 'wcsob_alt_method', __( 'Use alternative method? (pure CSS)', 'sold-out-badge-for-woocommerce' ) )
->set_help_text( __( 'Try this method in case of odd badge placement or if the badge does not show. Useful for some themes like Divi. The <code>.product</code> div needs to have a <code>.outofstock</code> class.', 'sold-out-badge-for-woocommerce' ) . ' ' . __( 'Unchecked by default.', 'sold-out-badge-for-woocommerce' ) )
->set_default_value( false ),
Expand All @@ -118,6 +125,10 @@ public static function get_text( string $option ): string {
return esc_html( I18n::get_theme_option( $option ) );
}

public static function get_behaviour() {
return self::get_option( 'wcsob_behaviour' );
}

/**
* Get value and append "px" if numeric, or "auto" if auto, or default value
*
Expand Down
11 changes: 8 additions & 3 deletions classes/WP.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@ public static function add_body_class( $classes ) {
$product = wc_get_product( $post->ID );

$outofstock_class = [];
$backorder_class = [];

if ( ! empty( $product ) && ! $product->is_in_stock() && ! Badge::is_hidden() ) {
if ( ! empty( $product ) && Badge::showOnOutofstock() && ! $product->is_in_stock() && ! Badge::is_hidden() ) {
$outofstock_class = [ WCSOB::$outofstock_class_single ];
}

return array_merge( $classes, $outofstock_class );
if ( ! empty( $product ) && Badge::showOnBackorder() && $product->is_on_backorder() && ! Badge::is_hidden() ) {
$backorder_class = [ WCSOB::$backorder_class_single ];
}

return array_merge( $classes, $outofstock_class, $backorder_class );
}

/**
Expand All @@ -46,7 +51,7 @@ public static function load_plugin_textdomain() {
public static function display_sold_out_in_search_loop( $html ) {
global $post, $product;

if ( is_search() && isset( $product ) && ! $product->is_in_stock() && ! Badge::is_hidden() ) {
if ( is_search() && isset( $product ) && Badge::shoudDisplay( $product ) && ! Badge::is_hidden() ) {
$badge = apply_filters( 'wcsob_soldout', '<span class="wcsob_soldout">' . Badge::get_text() . '</span>', $post, $product );
$html = $badge . $html;
}
Expand Down
4 changes: 2 additions & 2 deletions classes/WooCommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static function locate_template( $template, $template_name, $template_pat
public static function hide_sale_flash( string $content, $post, $product ): ?string {
global $post, $product;

return ( Settings::should_hide_sale_flash() && ! $product->is_in_stock() ) ? null : $content;
return ( Settings::should_hide_sale_flash() && Badge::showOnOutofstock() && ! $product->is_in_stock() ) ? null : $content;
}

/**
Expand All @@ -70,7 +70,7 @@ public static function hide_sale_flash( string $content, $post, $product ): ?str
* @return string
*/
public static function replace_out_of_stock_text( string $html, $product ): string {
if ( ! $product->is_in_stock() && ! Badge::is_hidden() ) {
if ( Badge::showOnOutofstock() && ! $product->is_in_stock() && ! Badge::is_hidden() ) {
return '<p class="wcsob_soldout_text">' . Badge::get_text() . '</p>';
}

Expand Down
7 changes: 6 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
=== Sold Out Badge for WooCommerce ===
Contributors: charlieetienne
Tags: woocommerce, sold out, out of stock, badge, wcsob
Stable tag: 4.2.0
Stable tag: 4.3.0
Requires at least: 5.2
Tested up to: 5.9
Requires PHP: 7.2
Expand All @@ -21,6 +21,8 @@ This plugin was initially created to help people and companies selling *unique*

However, this plugin can be used by **anyone** wanting to display **any text** in a badge when a product is out of stock.

It is also possible to display a badge on backorder products.

== Usage & Documentation ==

You can customize options in ***Settings > Sold Out Badge for WooCommerce***
Expand Down Expand Up @@ -67,6 +69,9 @@ Yes. Go to *Settings > Sold Out Badge for WooCommerce*, you'll find the setting

== Changelog ==

= 4.3.0 =
* Allows displaying badge on backorder products instead of/in addition to out of stock products

= 4.2.0 =
* Adds WPML compatibility

Expand Down
3 changes: 2 additions & 1 deletion sold-out-badge-for-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Plugin Name: Sold Out Badge for WooCommerce
* Description: Display a "Sold Out!" badge on out-of-stock products
* Version: 4.2.0
* Version: 4.3.0
* Requires at least: 5.2
* Requires PHP: 7.2
* WC requires at least: 4.0
Expand Down Expand Up @@ -42,6 +42,7 @@ class WCSOB {

private static $instance;
public static $outofstock_class_single = 'wcsob-outofstock-product';
public static $backorder_class_single = 'wcsob-backorder-product';

final public static function get_instance(): WCSOB {
if ( null === self::$instance ) {
Expand Down
3 changes: 2 additions & 1 deletion woocommerce/single-product/sold-out.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
global $post, $product;

?>
<?php if ( ! $product->is_in_stock() ) : ?>
<?php
if ( Badge::shoudDisplay( $product ) ) : ?>

<?php echo apply_filters( 'wcsob_soldout', '<span class="wcsob_soldout">' . Badge::get_text() . '</span>', $post, $product ); ?>

Expand Down

0 comments on commit 18c8ced

Please sign in to comment.