Tuesday, February 22, 2022

Wordpress plugin for divide total amount in percentage wise

 Suppose you have to take 30% amount from all the total bill. This plugin will help you to count percentage on total amount.


<?php
/* Plugin name: Deposit payment for woocommerce order
Plugin URI: https://example.com/wordpress-extra-post-info
Description: A simple plugin to add extra info to posts.
Author: Chirag
Author URI: https://google.com Version: 1.0
*/

/*if( !function_exists("extra_post_info") ) {  
 function extra_post_info($content)   {
      $extra_info = "EXTRA INFO";     
      return $content . $extra_info;   }
      add_filter('the_content', 'extra_post_info');
       }*/
//add menu in admin panel
add_action( 'admin_menu', 'extra_post_info_menu' );  
function extra_post_info_menu(){    
    $page_title = 'WordPress Extra Post Info';
    $menu_title = 'Deposit Percentage';  
    $capability = 'manage_options';   
    $menu_slug  = 'extra-post-info';   
    $function   = 'extra_post_info_page';   
    $icon_url   = 'dashicons-media-code';   
    $position   = 4;   

add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function,                   $icon_url, $position );

add_action( 'admin_init', 'update_extra_post_info' );

}
if( !function_exists("update_extra_post_info") ) {
function update_extra_post_info() {   

    register_setting( 'extra-post-info-settings', 'extra_post_info' ); } }

// for wp optin deposit amount store from wp-admin dashboard

function extra_post_info_page(){ ?><h1>Deposit Setting</h1>
<form method="post" action="options.php">
<?php settings_fields( 'extra-post-info-settings' ); ?>
<?php do_settings_sections( 'extra-post-info-settings' ); ?>
    <table class="form-table"><tr valign="top"><th scope="row">Deposit Percentage:</th>
    <td><input type="number" name="extra_post_info" value="<?php echo get_option( 'extra_post_info' ); ?>"/></td></tr></table>
<?php submit_button(); ?>
</form>
<?php }

/*$extra_info = get_option( 'extra_post_info' ); */
/*if( !function_exists("extra_post_info") ) {    
function extra_post_info($content)   {     
$extra_info = get_option( 'extra_post_info' );     
return $content . $extra_info;   }  
add_filter( 'the_content', 'extra_post_info' );  }
*/
//checkout radio deposit button
add_action( 'woocommerce_review_order_before_payment','bbloomer_checkout_radio_choice' );
 
function bbloomer_checkout_radio_choice() {
     
   $chosen = WC()->session->get( 'radio_chosen' );
   $chosen = empty( $chosen ) ? WC()->checkout->get_value( 'radio_choice' ) : $chosen;
   $chosen = empty( $chosen ) ? '0' : $chosen;
   $extra_info = get_option( 'extra_post_info' );       
   $args = array(
   'type' => 'radio',
   'class' => array( 'form-row-wide', 'update_totals_on_change' ),
   'options' => array(
   '0' => 'Pay Full Amount',
   $extra_info => "Pay ".$extra_info."% now and ".  (100-$extra_info)."% on delivery",
     /* '30' => 'Option 2 ($30)',*/
   ),
   'default' => $chosen
   );
     
   echo '<div id="checkout-radio">';
   echo '<h3>Customize Your Order!</h3>';
   woocommerce_form_field( 'radio_choice', $args, $chosen );
   echo '</div>';
     
}
 
// Part 2
// Add Fee and Calculate Total
   
add_action( 'woocommerce_cart_calculate_fees', 'bbloomer_checkout_radio_choice_fee', 20, 1 );
 
function bbloomer_checkout_radio_choice_fee( $cart ) {
   
   $extra_info = get_option( 'extra_post_info' );
   if ( is_admin() && ! defined( 'DOING_AJAX' ) )
   return;
    
   $radio = WC()->session->get( 'radio_chosen' );

   if($radio > 0){
    $subtotal = WC()->cart->subtotal;
    /* $total = WC()->cart->get_cart_total();*/
       $radio = ($subtotal * $extra_info) / 100;
   /*    $balance = $subtotal - $radio;*/
   $radio1 = $subtotal - $radio;
   }



  if ( $radio ) {
      $cart->add_fee( "Pay ".(100-$extra_info)."% On Delivery", -$radio1 );
     /* $cart_object->add_fee( __( 'Balance', 'woocommerce'), $balance, true );*/
   }
   
}
 
// Part 3
// Add Radio Choice to Session
 
add_action( 'woocommerce_checkout_update_order_review', 'bbloomer_checkout_radio_choice_set_session' );
 
function bbloomer_checkout_radio_choice_set_session( $posted_data ) {
    parse_str( $posted_data, $output );
    if ( isset( $output['radio_choice'] ) ){
        WC()->session->set( 'radio_chosen', $output['radio_choice'] );
    }
}
//

No comments:

Post a Comment