Woocommerce Calculates and Saves Price

I added custom fields to the Woocommerce / Add Product / General tab using this great tutorial http://www.remicorson.com/mastering-woocommerce-products-custom-fields/

Custom fields (height, width, multiplier) are stored in the OK database. I want to calculate the price based on custom fields and save the price in the database as a regular price . The problem is that the price is not 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 works except updating the price fields in the database. My custom fields are updated with the same syntax. I have confirmed that the $woocommerce_product_area_price variable exists, and it updates the custom field, but the same variable does not update the _regular_price or _price . Thus, these lines do not work, although 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 is some kind of syntax or check that I skip to update the price in Woocommerce?

+6
source share
1 answer

What I do is that you have a problem with the price that is displayed in the interface. You need to update the _price key if you want to reflect _regular_price in the interface.

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 action hook to woocommerce_process_product_meta , it will do the trick.

 add_action('woocommerce_process_product_meta', 'woo_add_custom_general_fields_save', 99); 

I tested it and it works great.
Hope this helps!

+2
source

Source: https://habr.com/ru/post/1014042/


All Articles