Woo-commerce simple product add custom field for vendor price  and commission price on MRP
Home / Woo-Commerce / Woo-commerce simple product add custom field for vendor price and commission price on MRP

Woo-commerce simple product add custom field for vendor price and commission price on MRP

Code copy and past in them functions.php , its work fine.

add_action( 'woocommerce_product_options_pricing', 'wc_vendor_price_product_field' );
function wc_vendor_price_product_field() {
woocommerce_wp_text_input(
array(
'id' => 'vendor_price',
'class' => 'wc_input_price short',
'label' => __( 'Vendor Price', 'woocommerce' ) . ' (RS) ' ) );
}
add_action( 'save_post', 'wc_vendor_price_save_product' );
function wc_vendor_price_save_product( $product_id ) {
// If this is a auto save do nothing, we only save when update button is clicked
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if ( isset( $_POST['vendor_price'] ) ) {
if ( is_numeric( $_POST['vendor_price'] ) )
update_post_meta( $product_id, 'vendor_price', $_POST['vendor_price'] );
} else delete_post_meta( $product_id, 'vendor_price' );
}
add_action( 'woocommerce_single_product_summary', 'wc_vendor_price_show', 5 );
function wc_vendor_price_show() {
global $product;
// Do not show this on variable products
if ( $product->product_type <> 'variable' ) {
$vendor_price = get_post_meta( $product->id, 'vendor_price', true );
woocommerce_price( $vendor_price );
}
}
add_action( 'woocommerce_product_options_general_product_data', 'Commission_add_custom_general_fields' );
// Save Fields
add_action( 'woocommerce_process_product_meta', 'Commission_add_custom_general_fields_save' );
function Commission_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
woocommerce_wp_text_input(
array(
'id' => 'commission_percentage',
'label' => __( '<strong>Commission Percentage :</strong>', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Please Enter Commission in % of Regular Price ', 'woocommerce' ),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);

echo '</div>';

echo '<div class="options_group">';

woocommerce_wp_text_input(
array(
'id' => 'plus_commission_selling_price',
'label' => __( '<strong style="color:#239804">Plus Commission with Selling Price</strong>', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Percentage of Price', 'woocommerce' ),
'type' => 'number',
'readonly' => 'readonly',
'custom_attributes' => array(
'step' => 'any',
'min' => '0',
'readonly' => 'readonly'
)
)
);

echo '</div>';

}//so28712303_rohil_add_custom_general_fields

function Commission_add_custom_general_fields_save( $post_id ){

$woocommerce_commission_percentage = $_POST['commission_percentage'];
//Value of Extra field 1

$woocommerce_plus_commission_selling_price = $_POST['plus_commission_selling_price'];
//No use of this..you can delete

$regular_price = $_POST['_regular_price'];
//Value of regular price

if( !empty( $woocommerce_commission_percentage )):
update_post_meta( $post_id, 'commission_percentage', esc_attr( $woocommerce_commission_percentage ) );
//Save value of Extra Field 1
endif;
$plus_commission_selling_price = ( $woocommerce_commission_percentage * $regular_price ) / 100 ;
//Calculation goes here ...
//if(empty($woocommerce_result_field))
update_post_meta( $post_id, 'plus_commission_selling_price', esc_attr( $plus_commission_selling_price ) );
//Save result here ...

}
add_action( 'save_post', 'wc_commission_selling_price_save_product' );
function wc_commission_selling_price_save_product( $product_id ) {
// If this is a auto save do nothing, we only save when update button is clicked
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if ( isset( $_POST['vendor_price'] ) ) {
if ( is_numeric( $_POST['vendor_price'] ) )
$vendor_price = $_POST['vendor_price'];
$final_plus_commission_selling_price = $_POST['plus_commission_selling_price'];
$final_selling_price = $vendor_price + $final_plus_commission_selling_price;
update_post_meta( $product_id, 'sale_price', esc_attr( $final_selling_price ) );
update_post_meta( $product_id, '_sale_price', esc_attr( $final_selling_price ) );
update_post_meta( $product_id, 'price', esc_attr( $final_selling_price ) );
update_post_meta( $product_id, '_price', esc_attr( $final_selling_price ) );
}
}

add_filter( 'woocommerce_get_price_html', 'wpa83367_price_html', 100, 2 );
function wpa83367_price_html( $price, $product ){

if ( $product->product_type <> 'variable' ) {
$vendor_price = get_post_meta( $product->id, 'vendor_price', true );
$vendor_price = woocommerce_price( $vendor_price );
}
//print_r($vendor_price);
$mrp_price = $product->regular_price;
$saleprice = $product->sale_price;

if ($mrp_price == $saleprice){
echo '<p style="font-size:85%;height:49px;">M.R.P. <i class="fa fa-inr"></i> : ' .$mrp_price. '</p>';
}
else{
echo '<p style="text-decoration: line-through;font-size:85%;" title="Maximum retail price">M.R.P. <i class="fa fa-inr"></i> : ' .$mrp_price. '</p>';
if(!empty($saleprice)){
echo '<p class="discont-price" title="Discounted Price">D.P. <i class="fa fa-inr"></i> : ' .$saleprice. ' </p>';
}

}

return;
}
/*********End for sample product*****************/

Woo-commerce simple product add custom field for vendor price and commission price on MRP

About Sushil_kumar

Check Also

aws

how to setting WordPress on Amazon EC2

Step 1: Create an AWS Account Step 2: Create an Instance To create a new …

Leave a Reply

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

0

December 9, 2016

Clear all