Based on Product custom field to enable a specific fee in Woocommerce answer, I have the following code:
// Create and display Backend Product custom field
add_action('woocommerce_product_options_general_product_data', 'ba_adding_custom_product_general_field');
function ba_adding_custom_product_general_field()
{
global $woocommerce, $post;
echo '<div class="options_group">';
// Custom fields will be created here...
woocommerce_wp_text_input(array(
'id' => '_number_field',
'label' => __('Environmental fee', 'woocommerce'),
'placeholder' => '',
'description' => __('Enter the custom value here.', 'woocommerce'),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
));
echo '</div>';
}
// Save the submitted data from Backend Product custom field
add_action('woocommerce_process_product_meta', 'ba_saving_custom_product_general_field');
function ba_saving_custom_product_general_field($post_id)
{
// Get the submitted value
$woocommerce_number_field = isset($_POST['_number_field']) ? $_POST['_number_field'] : '';
// Create/Update the submitted value in postmeta table for this product
update_post_meta($post_id, '_number_field', esc_attr($woocommerce_number_field));
}
// Adding a custom fee to cart based on a product custom field value calculation
add_action('woocommerce_cart_calculate_fees', 'ba_custom_cart_fee');
function ba_custom_cart_fee($cart_object)
{
// Initializing variables
$fee = 0;
// Iterating through each cart items
foreach ($cart_object->get_cart() as $cart_item) {
$item_fee = 0;
if ($cart_item['variation_id'] > 0) {
// If it's a variation, get its custom field value
$item_id = $cart_item['variation_id'];
$item_fee = get_post_meta($item_id, '_number_field_variation', true);
} else {
// If it's a simple product, get its custom field value
$item_id = $cart_item['product_id'];
$item_fee = get_post_meta($item_id, '_number_field', true);
}
// Adding the calculation to the fee
$fee += $cart_item['quantity'] * $item_fee;
}
// Adding fee to cart if it's not zero
if ($fee != 0) {
$cart_object->add_fee(__('Handling fee', 'woocommerce'), $fee, true, 'standard');
}
}
// Adding custom text on product page for the custom fee
add_action('woocommerce_single_product_summary', 'ba_display_custom_product_text', 25);
function ba_display_custom_product_text()
{
global $product;
// Getting the corresponding product custom field value
$item_fee = get_post_meta($product->get_id(), '_number_field', true);
// Getting the currency symbol
$currency_symbol = get_woocommerce_currency_symbol();
// Displaying custom text if fee is set
if ($item_fee) {
echo '<p style="margin-top: 10px; color: red;">You pay ' . $item_fee . ' ' . $currency_symbol . ' warranty for this product.</p>';
}
}
// Create and display Backend Product custom field for variations
add_action('woocommerce_product_after_variable_attributes', 'ba_adding_custom_variation_field', 10, 3);
function ba_adding_custom_variation_field($loop, $variation_data, $variation)
{
woocommerce_wp_text_input(array(
'id' => '_number_field_variation[' . $variation->ID . ']',
'label' => __('Environmental fee', 'woocommerce'),
'placeholder' => '',
'description' => __('Enter the custom value here.', 'woocommerce'),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
),
'value' => get_post_meta($variation->ID, '_number_field_variation', true)
));
}
// Save the submitted data from Backend Product custom field for variations
add_action('woocommerce_save_product_variation', 'ba_saving_custom_variation_field', 10, 2);
function ba_saving_custom_variation_field($variation_id, $i)
{
if (isset($_POST['_number_field_variation'][$variation_id])) {
update_post_meta($variation_id, '_number_field_variation', sanitize_text_field($_POST['_number_field_variation'][$variation_id]));
}
}
// Adding custom text on product loop for the custom fee
add_action('woocommerce_after_shop_loop_item', 'ba_display_custom_product_text_loop', 9);
function ba_display_custom_product_text_loop()
{
global $product;
// Getting the corresponding product custom field value
$item_fee = get_post_meta($product->get_id(), '_number_field', true);
// Getting the currency symbol
$currency_symbol = get_woocommerce_currency_symbol();
// Displaying custom text if fee is set
if ($item_fee) {
echo '<p style="margin-top: 10px; color: red;">You pay ' . $item_fee . ' ' . $currency_symbol . ' warranty for this product.</p>';
}
}
The code allows me to add a fee for simple and variable products (including variations). The total amount of the tax is displayed in the cart and checkout. In the store and on the product page, the amount of the fee per item is displayed. How to make that for variable products to display the fee of the selected variation?
I tried using Replace WooCommerce product short description by selected variation description answer, but I didn’t manage to make it functional.
2
Answers
I managed to modify @LoicTheAztec's code and made it work almost as I wanted. The problem I still have: if I don't set the additional fee (warranty) at the product level (leave the field empty), the message with zero euro additional fee is displayed in the index. In order not to display that message, I have to press the delete key when editing the product (although nothing was filled in). I did not manage to find any solution to not display that text if the field is not filled. This problem only happens for variable products (even for those that do not have an additional fee added to the variations).
My code:
Since WooCommerce 3, your code is a bit outdated, with some mistakes. I have completely revised your code, adding the missing code to display the selected variation warranty fee.
Code goes in functions.php file of your child theme (or in a plugin). Tested and works.