I’m trying to add a dropdown select field to WooCommerce product pages.
With % based fee or surcharge on the product price weather that’s inc or exc tax, ie both may be possible as it depends on customer location, and the colour label must also be retained as part of the value for later use, customer and admin must know the amount paid and the colour, at checkout and in order needs/emails etc, and it needs to inc tax if the product is taxable, there can be duplicates of each %, so we need to project the label as well.
I have tried to adapt How can I get the select field option value from select dropdown as well as custom fee percentage of cart item
using How to add a radio button with price on product page? WordPress woocommerce as a guide and for inspiration.
But the value is not being added to the cart.
Here is my code.
Thanks in advance if anyone can help.
add_action('woocommerce_before_add_to_cart_button', 'product_option_custom_field', 30 );
function product_option_custom_field(){
global $product;
//$custom_subtotal = get_related_items_subtotal( WC()->cart );
$active_price = (float) $product->get_price();
// surcharge is % of item value, exc tax or inc tax
$colour_surcharge = strip_tags( wc_price( wc_get_price_to_display( $product, array('price' => $custom_subtotal ) ) ) );
// $colour_price_html = strip_tags( wc_price( wc_get_price_to_display( $product, array('price' => $custom_subtotal ) ) ) );
$active_price_html = wc_price( wc_get_price_to_display( $product ) );
$disp_price_sum_html = wc_price( wc_get_price_to_display( $product, array('price' => $active_price + $custom_subtotal ) ) );
$custom_subtotal = (float) $product->get_price();
if ( $custom_subtotal > 0 ) {
// $value = WC()->session->get( 'colour_surcharge' );
// $value = empty( $value ) ? WC()->checkout->get_value( 'colour_surcharge' ) : $value;
$value = empty( $value ) ? '0' : $value;
$domain = 'woocommerce';
// echo '<div id="checkout-select">
// <h4>' . __("Select Colour option") .'</h4>';
woocommerce_form_field( 'colour_surcharge', array(
'type' => 'select',
'class' => array( 'form-row-wide', 'update_totals_on_change' ),
'label' => __( 'Colour options, standard or premium', 'woocommerce' ),
'desc_tip' => 'true',
'description' => __( 'Select your chosen colour', 'woocommerce' ),
'options' => array(
// 0 would be hidden, using 0.001 works to retain the values
'0.001 black' => __( 'Black - no fee', 'woocommerce' ) . ' (' . strip_tags( wc_price( 0.001 * $custom_subtotal / 100 ) ) . ')',
'15 white' => __( 'White 15% surcharge', 'woocommerce' ) . ' (' . strip_tags( wc_price( 15 * $custom_subtotal / 100 ) ) . ')',
'25 red' => __( 'Red 25% surcharge', 'woocommerce' ) . ' (' . strip_tags( wc_price( 25 * $custom_subtotal / 100 ) ) . ')',
),
), $value );
// Jquery: Update displayed price
?>
<script type="text/javascript">
jQuery(function($) {
var cb = 'input[name="colour_surcharge"]'
pp = 'p.price';
// On change / select a variation
$('form.cart').on( 'change', cb, function(){
needs to be selected
if( $(cb).prop('selected') === true )
// if( $(cb).prop('checked') === true )
$(pp).html('<?php echo $disp_price_sum_html; ?>');
else
$(pp).html('<?php echo $active_price_html; ?>');
})
});
</script>
<?php
}
}
function save_value_add_cart_item_data( $cart_item_data, $product_id, $variation_id ) {
$custom_fees = filter_input( INPUT_POST, 'custom_fees' );
if ( empty( $custom_fees ) ) {
return $cart_item_data;
}
$cart_item_data['colour_surcharge'] = $custom_fees;
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'save_value_add_cart_item_data', 99, 3 );
function calculate_add_cart_fee() {
global $woocommerce;
$percentage = (float) WC()->session->get( 'colour_surcharge' );
$cart_items = $woocommerce->cart->get_cart();
foreach( $cart_items as $key => $item ) {
if ( $surcharge_fee === '0.001 black' ) {
$label = sprintf( __('Black no fee %d%', 'woocommerce'), $percentage == 0.001 ? 0.001 : 15);
$cart->add_fee( $label, $custom_subtotal * $percentage / 100 );
}
elseif ( $surcharge_fee === '15 white' ) {
$label = sprintf( __('Blue standard %d% fee', $domain), $percentage == 15 ? 15 : 35);
$cart->add_fee( $label, $custom_subtotal * $percentage / 100 );
}
elseif ( $surcharge_fee === '35 red' ) {
$label = sprintf( __('Red %d% fee', $domain), $percentage == 35 ? 15 : 35);
$cart->add_fee( $label, $custom_subtotal * $percentage / 100 );
}
if( !isset( $item['colour_surcharge'] ) && empty( $item['colour_surcharge'] ) ) continue;
$woocommerce->cart->add_fee( __('Colour Surcharge', 'woocommerce'), $item['colour_surcharge'] );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'calculate_add_cart_fee', 99 );
// Display custom data in checkout page
add_filter( 'woocommerce_get_item_data', 'display_custom_cart_item_data', 10, 2 );
function display_custom_cart_item_data( $cart_data, $cart_item ) {
$domain = 'woocommerce';
if ( isset( $cart_item['colour_surcharge'] ) ){
$cart_data[] = array('name' => __( 'Colour Surcharge', $domain ),
'value' => $cart_item['colour_surcharge'] );
}
return $cart_data;
}
add_action( 'woocommerce_admin_order_data_after_order_details', 'display_order_custom_meta_data_in_admin' );
function display_order_custom_meta_data_in_admin( $order ){
$colour_surcharge = colour_surcharge_array();
$key = get_post_meta( $order->get_id(), 'colour_surcharge', true );
?>
<div class="order_data_column">
<h4><?php _e( 'Colour Surcharge' ); ?></h4>
<p><strong><?php _e( 'Colour Surcharge: ' ); ?></strong><?php echo $colour_surcharge[$key]; ?></p>
</div>
<?php
}