In Woocommerce, I have add an extra price field "bestprice"
for my products in backend.
So now I have as possible price settings:
100$
), bestprice
(for example 90$
)I would like to add a button "click for better price" in product page. Normally the price in product page will be the regular price.
100$
will be added in the cart.bestprice
90$) and when he will press add to cart the product of 90$
will be added in cart.Any help on this will be appreciated.
Here is a complete solution code… I have replaced your custom field slug with _bestprice
as it should start with an underscore like _price
or _sale_price
.
In backend for simple products, the additional "Best price" custom field:
// Add a custom field to product in backend (for testing)
add_action( 'woocommerce_product_options_pricing', 'add_field_product_options_pricing' );
function add_field_product_options_pricing() {
global $post;
echo '<div class="options_group">';
woocommerce_wp_text_input( array(
'id' => '_bestprice',
'label' => __('Best price', 'woocommerce') . ' (' . get_woocommerce_currency_symbol() . ')',
'placeholder' => __('Set the best price…', 'woocommerce'),
'description' => __('Enter the custom value here.', 'woocommerce'),
'desc_tip' => 'true',
));
echo '</div>';
}
// Save product custom field to database when submitted in Backend (for testing)
add_action( 'woocommerce_process_product_meta', 'save_product_options_custom_fields', 30, 1 );
function save_product_options_custom_fields( $post_id ){
// Saving custom field value
if( isset( $_POST['_bestprice'] ) ){
update_post_meta( $post_id, '_bestprice', sanitize_text_field( $_POST['_bestprice'] ) );
}
}
In single product page we add a custom button (and a hidden field):
When customer hit "Get better price button", the displayed price change in a kind of price range just like when the product is on sale (When product is on sale, it replace the sale price):
Here (example) the product is on sale, the sale price $32.00
is replaced by the bestprice $30.00
.
// Add a 'Get better price' additional button and a hidden field below single add to cart button
add_action( 'woocommerce_after_add_to_cart_button', 'before_add_to_cart_button' );
function before_add_to_cart_button() {
global $product;
// Get your product 'bestpprice' custom field
$bestprice = get_post_meta( $product->get_id(), '_bestprice', true);
if( ! empty($bestprice) ):
$bestprice = wc_get_price_to_display( $product, array( 'price' => $bestprice ) );
$reg_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) );
$range = wc_format_sale_price( $reg_price, $bestprice );
?>
<!-- The button and hidden field -->
<div class="bestprice-wrapper"><br>
<a href="" class="get_bestprice button alt" id="get_bestprice"><?php _e('Get better price'); ?></a>
<input type="hidden" name="bestprice" id="bestprice" class="bestprice" value="" />
</div>
<!-- The jQuery code -->
<script type="text/javascript">
(function($){
var b = '<?php echo $bestprice; ?>',
i = 'input[name=bestprice]',
p = 'p.price',
r = '<?php echo $range; ?>',
t = 'a#get_bestprice'
u = true;
$(t).click( function(e){
e.preventDefault();
if(u){
$(p).html(r); // Replacing price with the range
$(i).val(b); // Set the best price in hidden input field
u = false; // Disable button
$(t).text('Better Price active'); // change button text
$(t).removeClass('alt'); // Remove button 'alt' class for styling
}
});
})(jQuery);
</script>
<?php
endif;
}
The data is added to cart object and change the cart item price when "bestprice" has been enabled:
// Add custom fields data to cart items
add_filter( 'woocommerce_add_cart_item_data', 'custom_add_cart_item_data', 20, 2 );
function custom_add_cart_item_data( $cart_item, $product_id ){
if( ! isset( $_POST['bestprice'] ) )
return $cart_item;
if( ! empty( $_POST['bestprice'] ) )
$cart_item['custom_data']['bestprice'] = (float) esc_attr( $_POST['bestprice'] );
return $cart_item;
}
// Replacing cart item price with 'bestprice'
add_action( 'woocommerce_before_calculate_totals', 'set_cart_item_bestprice', 20, 1 );
function set_cart_item_bestprice( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ){
if( isset( $cart_item['custom_data']['bestprice'] ) ){
// Set the calculated item price (if there is one)
$cart_item['data']->set_price( (float) $cart_item['custom_data']['bestprice'] );
}
}
}
This is an optional code to Change add to cart link by a link to the product in Shop and archives pages when bestprice is set in the product setting options:
// Change add to cart link by a link to the product in Shop and archives pages for bestprice enabled option
add_filter( 'woocommerce_loop_add_to_cart_link', 'bestprice_loop_add_to_cart_button', 10, 2 );
function bestprice_loop_add_to_cart_button( $button, $product ) {
// Get your product 'bestpprice' custom field
$bestprice = get_post_meta( $product->get_id(), '_bestprice', true);
// Only for enabled "bestprice" option price.
if( ! empty( $bestprice ) ){
$button_text = __( "View product", "woocommerce" );
$button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
}
return $button;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.