Modify Product Price Before Saving or Updating Using Functions file
Question
I am trying to use add_filter to modify the price, regular price and sale price of a product prior to saving to the database.
I am using dokan plugin and each category is assigned a commission meta value. I have retrieved this commission and created a new price using the retrieved commission.
Now the problem is to modify the prices when adding or updating to use this new price set from the formula.
function get_category_commission(){
$commission_set=0;
$worked=0;
global $woo_options;
global $post;
$term_vals = NULL;
$primary_cat_id=get_post_meta($post->ID,'_yoast_wpseo_primary_product_cat',true);
if($primary_cat_id){
$primary_category = $primary_cat_id;
}
if($primary_category!=""){
$term_vals1 = get_term_meta($primary_category);
foreach($term_vals1 as $key1=>$val1){
if($key1=="per_category_admin_commission_type"&&$val1[0]!=""){
$commission_set = 1;
}
if($key1=="per_category_admin_commission"){
$amount = $val1[0];
}
}
if($commission_set=="1"&&$amount!=""){
return $amount;
//continue;
$worked=1;
}
}
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
$term_id = $term->term_id;
$term_vals = get_term_meta($term_id);
foreach($term_vals as $key=>$val){
if($key=="per_category_admin_commission_type"&&$val[0]!=""){
$commission_set = 1;
}
if($key=="per_category_admin_commission"){
$amount = $val[0];
}
}
}
if($worked!="1"){
return $amount;
}
}
function custom_price( $price) {
$commission = $price/100 * get_category_commission();
return $price + $commission;
}
add_filter( 'wp_insert_post_data' , 'filter_post_data' , '99', 2 );
function filter_post_data( $data , $postarr ) {
$data['meta_input']['_regular_price'] = custom_price($data['meta_input']['_price']);
$data['meta_input']['_regular_price'] = custom_price($data['meta_input']['_regular_price']);
$data['meta_input']['_sale_price'] = custom_price($data['meta_input']['_sale_price']);
return $data;
}
Can someone point me in the right direction.
0
plugins
3 years
2019-10-28T21:25:43-05:00
2019-10-28T21:25:43-05:00 0 Answers
84 views
0
Leave an answer