Update product price from custom fields value
-
Hi,
i need to update the product price based on values from some custom fields
this is how i’ve build and saving the custom fields, at the end i’m trying to update _regular_price based on the values of the customs fields when i save, update, publish the product but nothing is happening, what am i missing?
/BUILD AND SAVE FIELDS //meta // Display Fields add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' ); function woo_add_custom_general_fields() { global $woocommerce, $post; echo '<div class="options_group">'; // Product URL woocommerce_wp_checkbox( array( 'label' => __('Product URL', 'woocommerce' ), $post_id = $_GET['post'], $produrl = get_post_meta( $post_id, 'productOriginalUrl', true), 'description' => '<a href="' . $produrl . '" class="button button-primary button-large" target="_blank">' . __('View Product', 'woocommerce') . '</a>', ) ); // Number Field woocommerce_wp_text_input( array( 'id' => '_price_usd', 'label' => __( 'Price in USD', 'woocommerce' ), 'placeholder' => '', 'description' => __( 'Enter the custom value here.', 'woocommerce' ), 'type' => 'number', 'custom_attributes' => array( 'step' => 'any', 'min' => '0' ) ) ); woocommerce_wp_text_input( array( 'id' => '_price_tax', 'label' => __( 'TAX', 'woocommerce' ), 'placeholder' => '', 'description' => __( 'Enter the custom value here.', 'woocommerce' ), 'type' => 'number', 'custom_attributes' => array( 'step' => 'any', 'min' => '0' ) ) ); woocommerce_wp_text_input( array( 'id' => '_price_shipping', 'label' => __( 'Shipping', 'woocommerce' ), 'placeholder' => '', 'description' => __( 'Enter the custom value here.', 'woocommerce' ), 'type' => 'number', 'custom_attributes' => array( 'step' => 'any', 'min' => '0' ) ) ); // Select woocommerce_wp_select( array( 'id' => '_shipping_weight', 'label' => __( 'Shipping Weight', 'woocommerce' ), 'options' => array( '3.75' => __( 'Less Than 0.5 KG', 'woocommerce' ), '7.5' => __( '0.5 KG', 'woocommerce' ), '15' => __( '1 KG', 'woocommerce' ) ) ) ); // Hidden field woocommerce_wp_hidden_input( array( 'id' => '_usd_rate', 'value' => '18' ) ); echo '</div>'; }; // Save Fields add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' ); function woo_add_custom_general_fields_save( $post_id ){ // Number Field $woo_price_usd = $_POST['_price_usd']; if( !empty( $woo_price_usd ) ) update_post_meta( $post_id, '_price_usd', esc_attr( $woo_price_usd ) ); $woo_price_tax = $_POST['_price_tax']; if( !empty( $woo_price_tax ) ) update_post_meta( $post_id, '_price_tax', esc_attr( $woo_price_tax ) ); $woo_price_shipping = $_POST['_price_shipping']; if( !empty( $woo_price_shipping ) ) update_post_meta( $post_id, '_price_shipping', esc_attr( $woo_price_shipping ) ); // Select $woo_shipping_weight = $_POST['_shipping_weight']; if( !empty( $woo_shipping_weight ) ) update_post_meta( $post_id, '_shipping_weight', esc_attr( $woo_shipping_weight ) ); // Hidden Field $woo_usd_rate = $_POST['_usd_rate']; if( !empty( $woo_usd_rate ) ) update_post_meta( $post_id, '_usd_rate', esc_attr( $woo_usd_rate ) ); } //UPDATE PRICE REGULAR function wpa104760_default_price( $post_id, $post ) { if ( isset( $_POST['_regular_price'] )) { $post_id = $_GET['post']; $woo_price_usd = get_post_meta($post_id, '_price_usd', true); $woo_price_tax = get_post_meta($post_id, '_price_tax', true); $woo_price_shipping = get_post_meta($post_id, '_price_shipping', true); $woo_shipping_weight = get_post_meta($post_id, '_shipping_weight', true); $woo_usd_rate = get_post_meta($post_id, '_usd_rate', true); $woo_new_product_price = (($woo_price_usd + (($woo_price_usd*$woo_price_tax)/100) + $woo_price_shipping + $woo_shipping_weight) * $woo_usd_rate ); update_post_meta( $post_id, '_regular_price', $woo_new_product_price ); } } add_action( 'woocommerce_process_product_meta', 'wpa104760_default_price' );
- The topic ‘Update product price from custom fields value’ is closed to new replies.