Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save quasel/2fee6f227e12ba666052 to your computer and use it in GitHub Desktop.
Save quasel/2fee6f227e12ba666052 to your computer and use it in GitHub Desktop.
Streichpreisen in WooCommerce einen Hinweistext hinzufügen: Dieses Mini-Plugin fügt WooCommerce-Produkten mit Streichpreisen einen eigene Hinweistext hinzu, der zu Beginn des Plugin-Codes, innerhalb der Funktion wc_min_max_price_suffix__defaults(), angepasst werden kann. Das Plugin nutzt die filterbare WooCommerce-Option ›Zusatz zur Preisangabe‹…
<?php
/**
* Plugin Name: WooCommerce|Min-Max Price Suffix
* Description: Append custom text to min-max prices in WooCommerce.
* Version: 2015.12
* Author: Caspar Hübinger, Inpsyde
* Author URI: https://marketpress.com
* License: GNU General Public License v3 or later
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* EDIT YOUR CUSTOM TEXT IN THIS FUNCTION.
*
* @return array Default text and link URL used later on
*/
function wc_min_max_price_suffix__defaults() {
$defaults = array();
/* Enter a page ID of page linked in custom text.
* Here: defined by WGM option.
*/
$defaults[ 'suffix-link-page-id' ] = absint( get_option( 'woocommerce_bestellvorgang_page_id' ) );
// Retrieve URL from page ID.
$defaults[ 'suffix-link-url' ] = get_permalink( $defaults[ 'suffix-link-page-id' ] );
/* Enter custom text to be displayed after min-max prices.
* Optionally including a link to page specified above.
* Use %s as a placeholder within sprintf() for URL retrieved above .
*/
$defaults[ 'suffix-text' ] = '* Eigener Text zu Streichpreisen hierhin.';
$defaults[ 'suffix-text' ] .= sprintf( ' Ggf. auch mit <a href="%s" target="_blank">Link</a>.',
esc_url( $defaults[ 'suffix-link-url' ] )
);
/* STOP EDITING HERE. */
return $defaults;
}
/**
* Load plugin.
*
* @return void
*/
function wc_min_max_price_suffix__plugins_loaded() {
if ( ! class_exists( 'WC_Product' ) ) {
return;
}
// Filter WooCommerce price suffix and min-max prices.
add_filter( 'woocommerce_get_price_suffix', 'wc_min_max_price_suffix__render_price_suffix', 10, 2 );
add_filter( 'woocommerce_get_price_html_from_to', 'wc_min_max_price_suffix__render_min_max_price', 10, 4 );
}
add_action( 'plugins_loaded', 'wc_min_max_price_suffix__plugins_loaded' );
/**
* Render custom price suffix.
*
* @param mixed $price_display_suffix Option value, boolean or string
* @param object $product Product object
* @return mixed String or boolean option value
*/
function wc_min_max_price_suffix__render_price_suffix( $price_display_suffix, $product ) {
// Stop here if this is admin, OR $product does not have min-max prices.
if ( ! wc_min_max_price_suffix__is_min_max_price_shop( $product ) ) {
return $price_display_suffix;
}
// Get plugin defaults.
$defaults = wc_min_max_price_suffix__defaults();
// Wrap text in HTML like WooCommerce does.
// See https://github.com/woothemes/woocommerce/blob/2.4.10/includes/abstracts/abstract-wc-product.php#L916
$price_display_suffix = sprintf(
' <small class="woocommerce-price-suffix wgm-min-max-price-suffix clear">%s</small>',
sanitize_post_field( 'post_content', $defaults[ 'suffix-text' ], 0, 'display' )
);
return $price_display_suffix;
}
/**
* Append asterisk to min-max price.
*
* @param string $price Price HTML
* @param mixed $from String or float to wrap with 'from' text
* @param mixed $to String or float to wrap with 'to' text
* @param object $product Product object
* @return string Price HTML
*/
function wc_min_max_price_suffix__render_min_max_price( $price, $from, $to, $product ) {
if ( wc_min_max_price_suffix__is_min_max_price( $product ) ) {
$price .= '<span class="wgm-min-max-price-indicator">*</span>';
}
return $price;
}
/**
* Helper: Make sure we’re in the front-end AND
* WooCommerce product has a min-max price.
*
* @param object $product Product object
* @uses wc_min_max_price_suffix__is_min_max_price()
* @return bool TRUE for products with a min-max price outside WP admin area
*/
function wc_min_max_price_suffix__is_min_max_price_shop( $product ) {
return (bool) ! is_admin() && wc_min_max_price_suffix__is_min_max_price( $product );
}
/**
* Helper: Check if WooCommerce product has a min-max price.
*
* @param object $product Product object
* @return bool TRUE for products with a min-max price
*/
function wc_min_max_price_suffix__is_min_max_price( $product ) {
/* Returns TRUE if
* - $product is a simple product with a sale price
* -OR-
* - $product is a variable product AND prices of variations are not identical
*/
return (bool) $product->get_sale_price() || ( $product->min_variation_price && $product->min_variation_price !== $product->max_variation_price );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment