Search code examples
phpwordpresswoocommerceproduct

Hide displayed product prices from Woocommerce Product Add-ons Fields


In Woocommerce I am using Woocommerce Product Add-Ons plugin and I am trying to figure out how I can hide the prices from Add-ons fields.

This is the current output with prices:

with prices

This is the desired Output without prices (yellow underlined):

without the yellow marked price

I figured out that the the select.php in product-addons/templates/addons/select.php could be the the solution

I'm bad in php so i try to comment out some things ... if I comment out line 27 then I have the desired output, but the Price from the addons does not count to the product total price:

$price_raw    = apply_filters( 'woocommerce_product_addons_option_price_raw', $price, $option );

The select.php code:

<?php
/**
 * The Template for displaying select field.
 *
 * @version 3.0.0
 */

$loop = 0;
$field_name = ! empty( $addon['field_name'] ) ? $addon['field_name'] : '';
$required   = ! empty( $addon['required'] ) ? $addon['required'] : '';
$current_value = isset( $_POST['addon-' . sanitize_title( $field_name ) ] ) ? wc_clean( $_POST[ 'addon-' . sanitize_title( $field_name ) ] ) : '';
?>
<p class="form-row form-row-wide wc-pao-addon-wrap wc-pao-addon-<?php echo sanitize_title( $field_name ); ?>">
    <select class="wc-pao-addon-field wc-pao-addon-select" name="addon-<?php echo sanitize_title( $field_name ); ?>" <?php if ( WC_Product_Addons_Helper::is_addon_required( $addon ) ) { echo 'required'; } ?>>

        <?php if ( empty( $required ) ) { ?>
            <option value=""><?php esc_html_e( 'None', 'woocommerce-product-addons' ); ?></option>
        <?php } else { ?>
            <option value=""><?php esc_html_e( 'Select an option...', 'woocommerce-product-addons' ); ?></option>
        <?php } ?>

        <?php foreach ( $addon['options'] as $i => $option ) {
            $loop++;
            $price        = ! empty( $option['price'] ) ? $option['price'] : '';
            $price_prefix = 0 < $price ? '+' : '';
            $price_type   = ! empty( $option['price_type'] ) ? $option['price_type'] : '';
            $price_raw    = apply_filters( 'woocommerce_product_addons_option_price_raw', $price, $option );
            $label        = ( '0' === $option['label'] ) || ! empty( $option['label'] ) ? $option['label'] : '';

            if ( 'percentage_based' === $price_type ) {
                $price_for_display = apply_filters( 'woocommerce_product_addons_option_price',
                    $price_raw ? '(' . $price_prefix . $price_raw . '%)' : '',
                    $option,
                    $i,
                    'select'
                );
            } else {
                $price_for_display = apply_filters( 'woocommerce_product_addons_option_price',
                    $price_raw ? '(' . $price_prefix . wc_price( WC_Product_Addons_Helper::get_product_addon_price_for_display( $price_raw ) ) . ')' : '',
                    $option,
                    $i,
                    'select'
                );
            }

            $price_display = WC_Product_Addons_Helper::get_product_addon_price_for_display( $price_raw );

            if ( 'percentage_based' === $price_type ) {
                $price_display = $price_raw;
            }
            ?>
            <option data-raw-price="<?php echo esc_attr( $price_raw ); ?>" data-price="<?php echo esc_attr( $price_display ); ?>" data-price-type="<?php echo esc_attr( $price_type ); ?>" value="<?php echo sanitize_title( $label ) . '-' . $loop; ?>" data-label="<?php echo esc_attr( wptexturize( $label ) ); ?>"><?php echo wptexturize( $label ) . ' ' . $price_for_display; ?></option>
        <?php } ?>

    </select>
</p>

Any help is appreciated.


Solution

  • Without overriding any template or changing core code, you could try to use one of the following hooked functions:

    add_filter( 'woocommerce_product_addons_option_price', '__return_empty_string' );
    

    Or may be if you need to add some IF statements like in this example:

    add_filter( 'woocommerce_product_addons_option_price', 'filter_product_addons_option_price', 10, 4 );
    function filter_product_addons_option_price( $price, $option, $i, $type ){
        global $product;
    
        if( $product->get_id() == 123 && $type = 'select' ) {
            $price '';
        }
        return $price;
    }
    

    Code goes in function.php file of your active child theme (or active theme). It should works.