skip to Main Content

I have this code below for assigning numbers to some rental carts(vehicle). I want the numbers to be different for every single cart i.e. every carts has its own unique number. Once the cart number is assigned then no other carts are allowed to have that number. The number is unique for every carts. I am not able to do this by myself, any help from your side will be highly appreciated. Thanks in advance.

// Adding Meta field in the meta container admin shop_order pages //
if ( ! function_exists( 'mv_licence_fields_callback' ) )
{
function mv_licence_fields_callback()
{
    global $post;

    //get order details //
    $order_id=$_GET['post'];
    $order = wc_get_order( $order_id ); 
    // Iterating through each "line" items in the order //
    $order_count=count($order->get_items());
    echo '<div class="driver_licences">';
    $count=1;
    foreach ($order->get_items() as $item_id => $item_data) {
        $item_quantity = $item_data->get_quantity();
        for($i=1; $i<=$item_quantity; $i++){
            $driver_assigned_cart = get_post_meta( $post->ID, 'driver_cart'.$count, true ) ? get_post_meta( $post->ID, 'driver_cart'.$count, true ) : '';

            echo '
                <lable>Driver '.$count.' Licence</label>
            <div style="border-bottom:solid 2px #23282d;padding-bottom:13px;margin-bottom:20px;">
                <input type="file"  name="driver_licence'.$count.'">';
            // assign cart number here  
            echo '<lable>Driver '.$count.' assigned cart NO#</label>
            <input type="text"  name="driver_cart'.$count.'" value="'.$driver_assigned_cart.'">';

            echo '<img src="'.get_post_meta($post->ID, "driver_licence".$count, true).'" style="max-width:400px">';
            echo "<h5>Return Cart<h5>";
            if(get_post_meta($post->ID, "return_cart".$count, true)=='on'){
                echo "<span style='color:red;'>Cart Returned</span>";
            }else{
                echo '<input type="checkbox"  name="return_cart'.$count.'"> Check if this cart is returned'. get_post_meta($post->ID, "return_cart".$count, true);
            }
            echo '<input type="hidden" name="product_id'.$count.'" value="'.$item_data->get_product_id().'">';
            echo '</div>';
            $count++;
        }
    }

    echo '<input type="hidden" name="prod_count" value="'.($count - 1).'">';
    echo "</div>";  }  }

2

Answers


  1. just add at the end of the loop

    $count++ 
    

    so for every loop it increment it by one

    or

     $count = rand(0,100);
    

    if you want to make it random numbers for every loop

    on your’r code it will be as follows:

      $count=1;
     foreach ($order->get_items() as $item_id => $item_data) {
    $item_quantity = $item_data->get_quantity();
    for($i=1; $i<=$item_quantity; $i++){
    $driver_assigned_cart = get_post_meta( $post->ID, 'driver_cart'.$count, 
    true ) ? get_post_meta( $post->ID, 'driver_cart'.$count, true ) : '';
    
        echo '
            <lable>Driver '.$count.' Licence</label>
        <div style="border-bottom:solid 2px #23282d;padding-bottom:13px;margin-bottom:20px;">
            <input type="file"  name="driver_licence'.$count.'">';
    
        echo '<lable>Driver '.$count.' assigned cart NO#</label>
        //this field is for cart number //
        <input type="text"  name="driver_cart'.$count.'" 
    value="'.$driver_assigned_cart.'">';  
    $count++ 
    //or 
    //$count = rand(0,100); 
    } 
    
    Login or Signup to reply.
  2. You Can use the vairable $i as a counter inside the 2nd loop instead of defining a new variable $count, so the code will be as follow:

    foreach ($order->get_items() as $item_id => $item_data) {
    $item_quantity = $item_data->get_quantity();
    for($i=1; $i<=$item_quantity; $i++){
        $driver_assigned_cart = get_post_meta( $post->ID, 'driver_cart'.$i, true ) ? get_post_meta( $post->ID, 'driver_cart'.$i, true ) : '';
    
        echo '
            <lable>Driver '.$i.' Licence</label>
        <div style="border-bottom:solid 2px #23282d;padding-bottom:13px;margin-bottom:20px;">
            <input type="file"  name="driver_licence'.$count.'">';
    
        echo '<lable>Driver '.$i.' assigned cart NO#</label>
        //this field is for cart number //
        <input type="text"  name="driver_cart'.$i.'" value="'.$driver_assigned_cart.'">';   
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search