Skip to content

Instantly share code, notes, and snippets.

@manospsyx
Last active February 29, 2020 20:39
Show Gist options
  • Save manospsyx/a5cfc877e594f3642c3b to your computer and use it in GitHub Desktop.
Save manospsyx/a5cfc877e594f3642c3b to your computer and use it in GitHub Desktop.
Use this snippet to define your own custom price strings for Composite products, for instance when the "Hide Price" option is checked to reduce server load.
<?php
/**
* Plugin Name: WooCommerce Composite Products - Custom Composite Price Strings
* Plugin URI: https://woocommerce.com/products/composite-products/
* Description: Use this snippet to define your own custom price strings for Composite products, for instance when the "Hide Price" option is checked to reduce server load.
* Version: 1.0
* Author: SomewhereWarm
* Author URI: https://somewherewarm.gr/
* Developer: Manos Psychogyiopoulos
*
* Requires at least: 3.8
* Tested up to: 5.3
*
* Copyright: © 2017-2020 SomewhereWarm SMPC ([email protected]).
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
// To use this snippet, download this file into your plugins directory and activate it, or copy the code under this line into the functions.php file of your (child) theme.
add_filter( 'woocommerce_get_price_html', 'wc_cp_custom_price_html', 10, 2 );
add_action( 'woocommerce_product_options_general_product_data', 'wc_cp_composite_custom_price_html', 11 );
add_action( 'woocommerce_admin_process_product_object', 'wc_cp_process_custom_composite_price_string' );
function wc_cp_custom_price_html( $price_html, $product ) {
if ( $product->is_type( 'composite' ) && ( $custom_price_string = $product->get_meta( '_custom_composite_price_html', true ) ) ) {
$price_html = $custom_price_string;
}
return $price_html;
}
function wc_cp_composite_custom_price_html() {
echo '<div class="options_group show_if_composite">';
woocommerce_wp_text_input( array( 'id' => '_custom_composite_price_html', 'class' => 'short', 'label' => __( 'Custom Price String', 'woocommerce-composite-products' ), 'description' => __( 'Enter a custom Price String to override the auto-generated Composite product price displayed in the shop.', 'woocommerce-composite-products' ), 'desc_tip' => true ) );
echo '</div>';
}
function wc_cp_process_custom_composite_price_string( $product ) {
if ( ! empty( $_POST[ '_custom_composite_price_html' ] ) ) {
$product->update_meta_data( '_custom_composite_price_html', stripslashes( $_POST[ '_custom_composite_price_html' ] ) );
} else {
$product->delete_meta_data( '_custom_composite_price_html' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment