I have added custom fields to Woocommerce/Add Product/General tab using this excellent tutorial http://www.remicorson.com/mastering-woocommerce-products-custom-fields/
The custom fields (height, width, multiplier) are being saved to the database OK.
I want to calculate a price based on the custom fields, and save the price to the database as the Regular Price.
The problem is the price is not being saved.
Here is my code, from functions.php in my child theme:
/* CUSTOM FIELDS: Add custom fields to Product/General pane in Woocommerce
from http://www.remicorson.com/mastering-woocommerce-products-custom-fields/ */
// Display Fields
add_action('woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields');
// Save Fields
add_action('woocommerce_process_product_meta', 'woo_add_custom_general_fields_save');
function woo_add_custom_general_fields()
{
global $woocommerce, $post;
echo '<div class="options_group">';
// Product Height
woocommerce_wp_text_input(
array(
'id' => '_product_height',
'label' => __('Product Height (inches)', 'woocommerce'),
'placeholder' => '',
'description' => __('Enter the product height in inches here.', 'woocommerce'),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
// Product Width
woocommerce_wp_text_input(
array(
'id' => '_product_width',
'label' => __('Product Width (inches)', 'woocommerce'),
'placeholder' => '',
'description' => __('Enter the product width in inches here.', 'woocommerce'),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
// Product Area Multiplier
woocommerce_wp_text_input(
array(
'id' => '_product_area_mult',
'label' => __('Product Area Multiplier', 'woocommerce'),
'placeholder' => '',
'description' => __('Enter Product Area Multiplier. ', 'woocommerce'),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
// Product Area Price: added this field to check if this value is being calculated
woocommerce_wp_text_input(
array(
'id' => '_product_area_price',
'label' => __('Product Area Price', 'woocommerce'),
'placeholder' => '',
'description' => __('Product Area Price. Calculated automatically', 'woocommerce'),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
echo '</div>';
}
function woo_add_custom_general_fields_save($post_id)
{
$woocommerce_product_height = $_POST['_product_height'];
$woocommerce_product_width = $_POST['_product_width'];
$woocommerce_product_area_mult = $_POST['_product_area_mult'];
$woocommerce_product_area_price = $_POST['_product_area_price'];
// save height, width, multiplier
if (!empty($woocommerce_product_height))
update_post_meta($post_id, '_product_height', esc_attr($woocommerce_product_height));
if (!empty($woocommerce_product_width))
update_post_meta($post_id, '_product_width', esc_attr($woocommerce_product_width));
if (!empty($woocommerce_product_area_mult))
update_post_meta($post_id, '_product_area_mult', esc_attr($woocommerce_product_area_mult));
// calculate and save _product_area_price, _regular_price, price as Height*Width*Mult
if (!empty($woocommerce_product_height) && !empty($woocommerce_product_width) && !empty($woocommerce_product_area_mult))
$woocommerce_product_area_price = $woocommerce_product_height * $woocommerce_product_width * $woocommerce_product_area_mult;
if (!empty($woocommerce_product_area_price))
update_post_meta($post_id, '_product_area_price', esc_attr($woocommerce_product_area_price));
if (!empty($woocommerce_product_area_price))
{
update_post_meta($post_id, '_regular_price', esc_attr($woocommerce_product_area_price));
update_post_meta($post_id, '_price', esc_attr($woocommerce_product_area_price));
}
}
Everything is working except updating the price fields in the database. My custom fields are updated with the same syntax. I confirmed that the variable $woocommerce_product_area_price
exists, and it is updating custom field, but the same variable is not updating _regular_price
or _price
fields. So these lines are not working, even though the same variable will update the _product_area_price
field:
if (!empty($woocommerce_product_area_price))
{
update_post_meta($post_id, '_regular_price', esc_attr($woocommerce_product_area_price));
update_post_meta($post_id, '_price', esc_attr($woocommerce_product_area_price));
}
Maybe there's some syntax or check I am missing for updating the price in Woocommerce?
What I thing that you have an issue with Price that is showing in frontend. You need to update _price
key if you want to reflect _regular_price
in frontend.
Try this code:
if (!empty($area_price)){
update_post_meta($post_id, '_regular_price', esc_attr($area_price));
update_post_meta($post_id, '_price', esc_attr($area_price)); //<-- Add this line.
}
UPDATED
Just add a higher priority to woocommerce_process_product_meta
action hook, it 'll do the trick.
add_action('woocommerce_process_product_meta', 'woo_add_custom_general_fields_save', 99);
I have tested it and it is working fine.
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With