Woocomerce Save Price

I have a problem to save sale price , here my code

// 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 ”;

// Product purchase
woocommerce_wp_text_input(
array(
‘id’ => ‘_product_purchase’,
‘label’ => __(‘Product purchase (dinar)’, ‘woocommerce’),
‘placeholder’ => ”,
‘description’ => __(‘Enter the product purchase’, ‘woocommerce’),
‘type’ => ‘number’,
‘custom_attributes’ => array(
‘step’ => ‘any’,
‘min’ => ‘0’
)
)
);

// Product Tmarg
woocommerce_wp_text_input(
array(
‘id’ => ‘_product_Tmarg’,
‘label’ => __(‘Product margin rate (number)’, ‘woocommerce’),
‘placeholder’ => ”,
‘description’ => __(‘Enter the product margin.’, ‘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>’;

}
add_action(‘woocommerce_process_product_meta’, ‘woo_add_custom_general_fields_save’, 99);
function woo_add_custom_general_fields_save($post_id)
{
$woocommerce_product_purchase = $_POST[‘_product_purchase’];
$woocommerce_product_Tmarg = $_POST[‘_product_Tmarg’];
$woocommerce_product_area_price = $_POST[‘_product_area_price’];

// save purchase,Tmarg
if (!empty($woocommerce_product_purchase))
update_post_meta($post_id, ‘_product_purchase’, esc_attr($woocommerce_product_purchase));

if (!empty($woocommerce_product_Tmarg))
update_post_meta($post_id, ‘_product_Tmarg’, esc_attr($woocommerce_product_Tmarg));

// calculate and save _product_area_price, _regular_price, price as purchase*Tmarg

if (!empty($woocommerce_product_purchase) && !empty($woocommerce_product_Tmarg) )
$woocommerce_product_area_price = ( $woocommerce_product_purchase * $woocommerce_product_Tmarg ) ;

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, ‘_sale_price’, esc_attr($woocommerce_product_area_price));
update_post_meta($post_id, ‘_price’, esc_attr($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($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));
}

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));
}

Read more here:: Woocomerce Save Price

Leave a Reply

Your email address will not be published. Required fields are marked *