Skip to content

Instantly share code, notes, and snippets.

@somewherewarm-snippets
Last active November 17, 2021 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save somewherewarm-snippets/18a4ce138696854f7e558f6c61057311 to your computer and use it in GitHub Desktop.
Save somewherewarm-snippets/18a4ce138696854f7e558f6c61057311 to your computer and use it in GitHub Desktop.
<?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: WooCommerce
* Author URI: https://woocommerce.com/
* Developer: Manos Psychogyiopoulos
*
* Requires at least: 3.8
* Tested up to: 5.3
*
* Copyright: © 2021 Automattic.
* 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