WOO-COMMERCE VARIABLE PRODUCT ADD CUSTOM FIELD IN VARIATIONS FOR VENDOR PRICE AND COMMISSION PRICE ON MRP
Home / Woo-Commerce / WOO-COMMERCE VARIABLE PRODUCT ADD CUSTOM FIELD IN VARIATIONS FOR VENDOR PRICE AND COMMISSION PRICE ON MRP

WOO-COMMERCE VARIABLE PRODUCT ADD CUSTOM FIELD IN VARIATIONS FOR VENDOR PRICE AND COMMISSION PRICE ON MRP

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

/*************for variable product**************/
// Add Variation Settings
add_action( 'woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3 );
/**
* Create new fields for variations
*
*/
function variation_settings_fields( $loop, $variation_data, $variation ) {
// Number Field
woocommerce_wp_text_input(
array(
'id' => '_vendor_price[' . $variation->ID . ']',
'label' => __( 'Vendor Price', 'woocommerce' ),
'desc_tip' => 'true',
'description' => __( 'Enter the custom number here.', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_vendor_price', true ),
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
// Number Field
woocommerce_wp_text_input(
array(
'id' => 'rgular_price_commission_variation[' . $variation->ID . ']',
'label' => __( 'Commission %', 'woocommerce' ),
'desc_tip' => 'true',
'description' => __( 'Enter the commission here.', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, 'rgular_price_commission_variation', true ),
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
}

/**
* Save new fields for variations
* Save Variation Settings
*/
add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 );
function save_variation_settings_fields( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
// Text Field
$text_field = $_POST['_vendor_price'][ $post_id ];
if( ! empty( $text_field ) ) {
update_post_meta( $post_id, '_vendor_price', esc_attr( $text_field ) );
}
echo $number_field = $_POST['rgular_price_commission_variation'][ $post_id ];
if( ! empty( $number_field ) ) {
update_post_meta( $post_id, 'rgular_price_commission_variation', esc_attr( $number_field ) );
}

}
add_action( 'woocommerce_product_after_variable_attributes', 'get_rgular_price_commission_variation_settings_fields', 10, 3 );

function get_rgular_price_commission_variation_settings_fields($loop, $variation_data, $variation){
$rgular_commission_price = ( get_post_meta( $variation->ID, 'rgular_price_commission_variation', true ) * get_post_meta( $variation->ID, '_regular_price', true ) ) / 100;
echo ''.$commission_plus_variation_sale_price = $rgular_commission_price + get_post_meta( $variation->ID, '_vendor_price', true );
woocommerce_wp_text_input(
array(
'id' => 'rgular_commission_price',
'label' => __( '<strong style="color:#239804">Commission of Regular Price</strong>', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Percentage of Price', 'woocommerce' ),
'type' => 'number',
'value' => $rgular_commission_price,
'readonly' => 'readonly',
'custom_attributes' => array(
'step' => 'any',
'min' => '0',
'readonly' => 'readonly'
)
)
);
woocommerce_wp_text_input(
array(
'id' => 'commission_plus_variation_sale_price',
'label' => __( '<strong style="color:#239804">Plus Commission with Selling Price</strong>', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Percentage of Price', 'woocommerce' ),
'type' => 'number',
'value' => $commission_plus_variation_sale_price,
'readonly' => 'readonly',
'custom_attributes' => array(
'step' => 'any',
'min' => '0',
'readonly' => 'readonly'
)
)
);

update_post_meta( $variation->ID, 'variation_sale_price', esc_attr($commission_plus_variation_sale_price) );
update_post_meta( $variation->ID, '_sale_price', esc_attr($commission_plus_variation_sale_price) );
update_post_meta( $variation->ID, 'sale_price', esc_attr($commission_plus_variation_sale_price) );
update_post_meta( $variation->ID, '_price', esc_attr($commission_plus_variation_sale_price) );
update_post_meta( $variation->ID, 'price', esc_attr($commission_plus_variation_sale_price) );

}
add_action( 'woocommerce_save_product_variation', 'get_rgular_price_commission_variation_settings_fields', 10, 2 );

/****************end commission part***************/

 

 

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 10, 2016

Clear all