Assign conditional statement for session variables in php function code example

Example 1: set php varibianle session

<?php 
  // Start the session
  session_start();
?>
<!DOCTYPE html>
<html>
  <body>
  <?php
    // Set session variables
    $_SESSION["color"]= "blue";
    $_SESSION["animal"]= "dog";
    echo "The session variable are set up.";
  ?>
  </body>
</html>

Example 2: Woocommerce Getting Session Variable Value direct in Billing Address [php Session]

/**
 * Getting Session Variable Value direct in Billing Address..
 *
 * PHP SESSION  VARIABLE
 * 
 * 
 */

add_filter( 'woocommerce_checkout_fields' , 'ahmadyani_checkout_field_defaults', 20 );
function ahmadyani_checkout_field_defaults( $fields ) {
    // $user = get_user_meta(get_current_user_id());
    // $first_name = $user ? $user['shipping_first_name'][0] : '';
    // $last_name = $user ? $user['shipping_last_name'][0] : '';
    // $company = $user ? $user['shipping_company'][0] : '';
    // $shipping_address_1 = $user ? $user['shipping_address_1'][0] : '';
    // $shipping_address_2 = $user ? $user['shipping_address_2'][0] : '';
    // $shipping_city = $user ? $user['shipping_city'][0] : '';
    // $shipping_state = $user ? $user['shipping_state'][0] : '';
    // $shipping_postcode = $user ? $user['shipping_postcode'][0] : '';
    if($_SESSION['number'] !== 0){

    $fields['billing']['store_location']['default'] = $_SESSION['number'];
      
    }
    // else if($_SESSION['loc2'] !== 0){

    // $fields['billing']['billing_address_2']['default'] = $_SESSION['loc2'];
     
    // }
    return $fields;
}

Tags:

Php Example