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'] );
    }
}
//

Wednesday, February 16, 2022

import database by php code

 Here I explain you how to import database by PHP code.


<?php
// Name of the data file
$filename = 'u6357583.zip';
// MySQL host
$mysqlHost = 'localhost';
// MySQL username
$mysqlUser = 'root';
// MySQL password
$mysqlPassword = '';
// Database name
$mysqlDatabase = 'u635758';
// Connect to MySQL server
$link = mysqli_connect($mysqlHost, $mysqlUser, $mysqlPassword, $mysqlDatabase) or die('Error connecting to MySQL Database: ' . mysqli_error());

$tempLine = '';
// Read in the full file
$lines = file($filename);
// Loop through each line
foreach ($lines as $line) {
    // Skip it if it's a comment
    if (substr($line, 0, 2) == '--' || $line == '')
        continue;
    // Add this line to the current segment
    $tempLine .= $line;
    // If its semicolon at the end, so that is the end of one query
    if (substr(trim($line), -1, 1) == ';')  {
        // Perform the query
        mysqli_query($link, $tempLine) or print("Error in " . $tempLine .":". mysqli_error());
        // Reset temp variable to empty
        $tempLine = '';
    }
}
 echo "Tables imported successfully";
?>


Find file and remove from whole folder by php code

 Hello

Here I explain find the specific file and remove it from whole folder. 

For example: There is many .htaccess files in every folder. Or any file created by virus or any script. You want to remove it. So you have to open every folder and remove it. Instead of this you can remove it by this code.


<?php
function getDirContents($dir, &$results = array()) {
    $files = scandir($dir);
    foreach ($files as $key => $value) {
        $path = realpath($dir . DIRECTORY_SEPARATOR . $value);
        if (!is_dir($path)) {
            $results[] = $path;
        } else if ($value != "." && $value != "..") {
            getDirContents($path, $results);
            $results[] = $path;
        }
    }
    return $results;
}
$i =1;
$files = getDirContents('FolderName');
foreach($files as $file){ // iterate files
  if (strpos($file, '.htaccess') !== false) {
    if (unlink($file)) {
  echo $i. "Delete file :".$file."<br>"; 
  $i++;
  }
}
}
?>


FolderName  means your website folder name