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:
This is the desired Output without prices (yellow underlined):
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.
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.
Add this code Functions.php
add_filter( 'woocommerce_get_price_html', 'react2wp_woocommerce_hide_product_price' );
function react2wp_woocommerce_hide_product_price( $price ) {
return 'Rate on Call';
Related
I am using the code found here, provided by LoicTheAztec. It adds stock status related text to the variation selector on WooCommerce products. It works as required.
I would, however, like to also add a custom attribute to each <option ... > in the dropdown selector.
My reason for this is that I am writing some jQuery that will be triggered by whether or not the selected option is in stock. That will be cleanest if each option has a stock attribute. For example, <option value=“1kg” class=“ ... “ stock-status=“true”>1kg - (in stock)</option>. In this example I’ve added a custom attribute called stock-status, which will either be true or false.
The code in use is:
// Function that will check the stock status and display the corresponding additional text
function get_stock_status_text( $product, $name, $term_slug ){
foreach ( $product->get_available_variations() as $variation ){
if($variation['attributes'][$name] == $term_slug ) {
$stock = $variation['is_in_stock'];
break;
}
}
return $stock == 1 ? ' - (In Stock)' : ' - (Out of Stock)';
}
// The hooked function that will add the stock status to the dropdown options elements.
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'show_stock_status_in_dropdown', 10, 2);
function show_stock_status_in_dropdown( $html, $args ) {
// Only if there is a unique variation attribute (one dropdown)
if( sizeof($args['product']->get_variation_attributes()) == 1 ) :
$options = $args['options'];
$product = $args['product'];
$attribute = $args['attribute']; // The product attribute taxonomy
$name = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title( $attribute );
$id = $args['id'] ? $args['id'] : sanitize_title( $attribute );
$class = $args['class'];
$show_option_none = $args['show_option_none'] ? true : false;
$show_option_none_text = $args['show_option_none'] ? $args['show_option_none'] : __( 'Choose an option', 'woocommerce' );
if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
$attributes = $product->get_variation_attributes();
$options = $attributes[ $attribute ];
}
$html = '<select id="' . esc_attr( $id ) . '" class="' . esc_attr( $class ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" data-show_option_none="' . ( $show_option_none ? 'yes' : 'no' ) . '">';
$html .= '<option value="">' . esc_html( $show_option_none_text ) . '</option>';
if ( ! empty( $options ) ) {
if ( $product && taxonomy_exists( $attribute ) ) {
$terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) );
foreach ( $terms as $term ) {
if ( in_array( $term->slug, $options ) ) {
// HERE Added the function to get the text status
$stock_status = get_stock_status_text( $product, $name, $term->slug );
$html .= '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) . $stock_status ) . '</option>';
}
}
} else {
foreach ( $options as $option ) {
$selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false );
// HERE Added the function to get the text status
$stock_status = get_stock_status_text( $product, $name, $option );
$html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) . $stock_status ) . '</option>';
}
}
}
$html .= '</select>';
endif;
return $html;
}
My knowledge of PHP isn’t sufficient to know the best way to go about this. One crude method would be to test if the result of get_stock_status_text contains the string (In or (No, and then add stock-status=“true” (or false) too the $html output accordingly. I can likely come up with a way to do that, but I suspect that’s not the best way to go about it. I am thinking it would be better to set a variable as true or false within the get_stock_status_text function, and then based on the value of that a stock-status custom attribute would have a value of true|false accordingly.
What would be an efficient way to add a custom attribute to the $html output, with a true|false value?
With a bit of playing around, it looks like I’ve come up with a solution. If there’s a more efficient or tidier way to achieve the result, I’d be very interested to know. This solution does at least have the desired output.
I have modified the code (from my question) to the following:
/******* ADD STOCK STATUS TO DROP DOWN SELECTOR ******/
// Function that will check the stock status and display the corresponding additional text
function get_stock_status_text( $product, $name, $term_slug ){
foreach ( $product->get_available_variations() as $variation ){
if($variation['attributes'][$name] == $term_slug ) {
$stock = $variation['is_in_stock'];
break;
}
}
return $stock == 1 ? ' - (In Stock)' : ' - (No Stock)';
}
function get_stock_status_bool( $product, $name, $term_slug ){
foreach ( $product->get_available_variations() as $variation ){
if($variation['attributes'][$name] == $term_slug ) {
$stock_bool = $variation['is_in_stock'];
break;
}
}
return $stock_bool == 1 ? 'true' : 'false';
}
// The hooked function that will add the stock status to the dropdown options elements.
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'show_stock_status_in_dropdown', 10, 2);
function show_stock_status_in_dropdown( $html, $args ) {
// Only if there is a unique variation attribute (one dropdown)
if( sizeof($args['product']->get_variation_attributes()) == 1 ) :
$options = $args['options'];
$product = $args['product'];
$attribute = $args['attribute']; // The product attribute taxonomy
$name = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title( $attribute );
$id = $args['id'] ? $args['id'] : sanitize_title( $attribute );
$class = $args['class'];
$show_option_none = $args['show_option_none'] ? true : false;
$show_option_none_text = $args['show_option_none'] ? $args['show_option_none'] : __( 'Choose an option', 'woocommerce' );
if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
$attributes = $product->get_variation_attributes();
$options = $attributes[ $attribute ];
}
$html = '<select id="' . esc_attr( $id ) . '" class="' . esc_attr( $class ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" data-show_option_none="' . ( $show_option_none ? 'yes' : 'no' ) . '">';
$html .= '<option value="">' . esc_html( $show_option_none_text ) . '</option>';
if ( ! empty( $options ) ) {
if ( $product && taxonomy_exists( $attribute ) ) {
$terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) );
foreach ( $terms as $term ) {
if ( in_array( $term->slug, $options ) ) {
// HERE Added the function to get the text status
$stock_status = get_stock_status_text( $product, $name, $term->slug );
$stock_attr = get_stock_status_bool( $product, $name, $term->slug );
$html .= '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . ' stock-status="' . $stock_attr . '">' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) . $stock_status ) . '</option>';
}
}
} else {
foreach ( $options as $option ) {
$selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false );
// HERE Added the function to get the text status
$stock_status = get_stock_status_text( $product, $name, $option );
$stock_attr = get_stock_status_bool( $product, $name, $term->slug );
$html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . ' stock-status="' . $stock_attr . '">' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) . $stock_status ) . '</option>';
}
}
}
$html .= '</select>';
endif;
return $html;
}
The main addition is a new function, get_stock_status_bool. Which is a replica of get_stock_status_text except that it returns true or false.
This function is then called right after the get_stock_status_text function is called, and its value is assigned to $stock_attr.
$stock_attr has then been added into the $html value (e.g. <option value=“1 kg” selected=“selected” stock-status=“true” class=“ ...”>1kg - $150 - (In Stock)</option>
my shop have 3 products with fixed prices ( 0$ , 18$ and 36$ ).
im trying to make a filter in the admin panel that will give me all the orders by a fixed price.
this is what i have tried so far :
/**
* Add price bulk filter for orders
*/
function add_filter_by_payment_price_orders() {
global $typenow;
if ( 'shop_order' === $typenow ) {
$amount_array = array(0,18,36);
?>
<select name="_shop_order_payment_price" id="dropdown_shop_order_payment_price">
<option value=""><?php esc_html_e( 'All Payment prices', 'text-domain' ); ?></option>
<?php foreach ( $amount_array as $key => $amount ) : ?>
<option value="<?php echo esc_attr( $key ); ?>" <?php echo esc_attr( isset( $_GET['_shop_order_payment_price'] ) ? selected( $key, $_GET['_shop_order_payment_price'], false ) : '' ); ?>>
<?php echo $amount ; ?>
</option>
<?php endforeach; ?>
</select>
<?php
}
}
add_action( 'restrict_manage_posts', 'add_filter_by_payment_price_orders', 99 );
/**
* Process bulk filter order for payment method
*
*/
function add_filter_by_payment_price_orders_query( $vars ) {
global $typenow;
if ( 'shop_order' === $typenow && isset( $_GET['_shop_order_payment_price'] ) ) {
$vars['meta_key'] = 'total';
$vars['meta_value'] = wc_clean( $_GET['_shop_order_payment_price'] );
}
return $vars;
}
add_filter( 'request', 'add_filter_by_payment_price_orders_query', 99 );
/**
* Add price bulk filter for orders
*/
function add_filter_by_payment_price_orders() {
global $typenow;
if ( 'shop_order' === $typenow ) {
$amount_array = array(0,18,36);
?>
<select name="_shop_order_payment_price" id="dropdown_shop_order_payment_price">
<option value=""><?php esc_html_e( 'All Payment prices', 'text-domain' ); ?></option>
<?php foreach ( $amount_array as $key => $amount ) : ?>
<option value="<?php echo esc_attr( $amount); ?>" <?php echo esc_attr( isset( $_GET['_shop_order_payment_price'] ) ? selected( $key, $_GET['_shop_order_payment_price'], false ) : '' ); ?>>
<?php echo $amount ; ?>
</option>
<?php endforeach; ?>
</select>
<?php
}
}
add_action( 'restrict_manage_posts', 'add_filter_by_payment_price_orders', 99 );
/**
* Process bulk filter order for payment method
*
*/
function add_filter_by_payment_price_orders_query( $vars ) {
global $typenow;
if ( 'shop_order' === $typenow && isset( $_GET['_shop_order_payment_price'] ) ) {
$vars['meta_key'] = '_order_total';
$vars['meta_value'] = wc_clean( $_GET['_shop_order_payment_price'] );
}
return $vars;
}
add_filter( 'request', 'add_filter_by_payment_price_orders_query', 99 );
I have a page like this:
I have manage stocks enabled in variations, and I want to filter out items that are not in stock. For example, if size 9.5 is not available in variations, it should not appear in the front end as well. The questions posted here are all quite old and I could not get any of the suggestions posted in them to work. Here is what I have done so far:
// Get terms if this is a taxonomy - ordered
if ( taxonomy_exists( $attribute_name ) ) {
$is_attr_color = false;
$attribute_color = wc_sanitize_taxonomy_name( 'color' );
if( $attribute_name == wc_attribute_taxonomy_name( $attribute_color ) ){
$is_attr_color = true;
}
$terms = wc_get_product_terms( $post->ID, $attribute_name, array( 'fields' => 'all' ) );
foreach ( $terms as $term ) {
if ( ! in_array( $term->slug, $options ) ) {
continue;
}
if( $is_attr_color ){
$datas = get_term_meta( $term->term_id, 'ts_product_color_config', true );
if( strlen( $datas ) > 0 ){
$datas = unserialize( $datas );
}else{
$datas = array(
'ts_color_color' => "#ffffff"
,'ts_color_image' => 0
);
}
}
$class = sanitize_title( $selected_value ) == sanitize_title( $term->slug ) ? 'selected' : '';
$class .= ' option';
if( $is_attr_color ){
$class .= ' color';
}
echo '<div data-value="' . esc_attr( $term->slug ) . '" class="' . $class . '">';
if( $is_attr_color ){
if( absint($datas['ts_color_image']) > 0 ){
echo '' . wp_get_attachment_image( absint($datas['ts_color_image']), 'ts_prod_color_thumb', true, array('title'=>$term->name, 'alt'=>$term->name) ) . '';
}
else{
echo '' . apply_filters( 'woocommerce_variation_option_name', $term->name ) . '';
}
}
else{
// !!!!!!!!!!MAIN ISSUE HERE!!!!!!!!!!!!
if($term->count > 0){
echo '' . apply_filters( 'woocommerce_variation_option_name', $term->name ) . '';
}
}
echo '</div>';
}
} else {
foreach ( $options as $option ) {
$class = sanitize_title( $selected_value ) == sanitize_title( $option ) ? 'selected' : '';
$class .= ' option';
echo '<div data-value="' . esc_attr( $option ) . '" class="' . $class . '">' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) ) . '</div>';
}
}
Any idea how I could fix this? I have been at it for hours and I cannot find a solution for it. It's really frustrating.
I'm using "https://stackoverflow.com/questions/47180058/how-to-add-variation-stock-status-to-woocommerce-product-variation-dropdown/47189725#47189725" answer code to show the text "In Stock" or "Out of stock" according to the stock status of the variation.
This is what I get:
I need to add a custom class only to these variants that showing text "Out of Stock".
How can this be done?
The following code will add the class outofstock to <option> html tag when the variation is "Out of Stock":
// Function that will check the stock status and display the corresponding additional text
function get_stock_status_text( $product, $name, $term_slug ){
foreach ( $product->get_available_variations() as $variation ){
if($variation['attributes'][$name] == $term_slug )
$stock = $variation['is_in_stock'];
}
return $stock == 1 ? ' - (In Stock)' : ' - (Out of Stock)';
}
// The hooked function that will add the stock status to the dropdown options elements.
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'show_stock_status_in_dropdown', 10, 2);
function show_stock_status_in_dropdown( $html, $args ) {
// Only if there is a unique variation attribute (one dropdown)
if( sizeof($args['product']->get_variation_attributes()) == 1 ) :
$options = $args['options'];
$product = $args['product'];
$attribute = $args['attribute']; // The product attribute taxonomy
$name = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title( $attribute );
$id = $args['id'] ? $args['id'] : sanitize_title( $attribute );
$class = $args['class'];
$show_option_none = $args['show_option_none'] ? true : false;
$show_option_none_text = $args['show_option_none'] ? $args['show_option_none'] : __( 'Choose an option', 'woocommerce' );
if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
$attributes = $product->get_variation_attributes();
$options = $attributes[ $attribute ];
}
$html = '<select id="' . esc_attr( $id ) . '" class="' . esc_attr( $class ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" data-show_option_none="' . ( $show_option_none ? 'yes' : 'no' ) . '">';
$html .= '<option value="">' . esc_html( $show_option_none_text ) . '</option>';
if ( ! empty( $options ) ) {
if ( $product && taxonomy_exists( $attribute ) ) {
$terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) );
foreach ( $terms as $term ) {
if ( in_array( $term->slug, $options ) ) {
// HERE Added the function to get the text status
$stock_status = get_stock_status_text( $product, $name, $term->slug );
// HERE we add a custom class to <option> that are "Out of stock"
$option_class = $stock_status === ' - (Out of Stock)' ? ' class="outofstock"' : '';
$html .= '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . $option_class . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) . $stock_status ) . '</option>';
}
}
} else {
foreach ( $options as $option ) {
$selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false );
// HERE Added the function to get the text status
$stock_status = get_the_stock_status( $product, $name, $option );
// HERE we add a custom class to <option> that are "Out of stock"
$option_class = $stock_status === ' - (Out of Stock)' ? ' class="outofstock"' : '';
$html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . $option_class . '>' .
esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) . $stock_status ) . '</option>';
}
}
}
$html .= '</select>';
endif;
return $html;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Is it possible to get a normal text instead to use a slug? I need to use this custom code for Wordpress WooCommerce product page. The small problem the output get small letters and line "-" between words.
<?php /*CUSTOM MID*/ else: ?>
<td class="value">
<select id="<?php echo esc_attr( sanitize_title($name) ); ?>"
name="attribute_<?php echo sanitize_title($name); ?>">
<option value="">
<?php echo __('Choose an option', 'woocommerce') ?>…
</option>
<?php
if ( is_array( $options ) ) {
if ( empty( $_POST ) )
$selected_value = ( isset( $selected_attributes[ sanitize_title( $name ) ] ) ) ? $selected_attributes[ sanitize_title( $name ) ] : '';
else
$selected_value = isset( $_POST[ 'attribute_' . sanitize_title( $name ) ] ) ? $_POST[ 'attribute_' . sanitize_title( $name ) ] : '';
// Get terms if this is a taxonomy - ordered
if ( taxonomy_exists( sanitize_title( $name ) ) ) {
$terms = get_terms( sanitize_title($name), array('menu_order' => 'ASC') );
foreach ( $terms as $term ) {
if ( ! in_array( $term->slug, $options ) ) continue;
echo '<option value="' . $term->slug . '" ' . selected( $selected_value, $term->slug, false ) . '>' . apply_filters( 'woocommerce_variation_option_name', $term->name ) . '</option>';
}
} else {
foreach ( $options as $option )
echo '<option value="' . $option . '" ' . selected( $selected_value, $option, false ) . '>' . apply_filters( 'woocommerce_variation_option_name', $option ) . '</option>';
}
}
?>
</select> <?php
if ( sizeof($attributes) == $loop )
echo '<a class="reset_variations" href="#reset">'.__('Clear selection', 'woocommerce').'</a>';
?></td>
remove - you can do it by pure php using str_replace
You have to give us more of your code to understand it.