I am trying to enable my users to edit their order history from a control panel on my site. Currently when a user places an order this script is executed:

function process_order_form_handler() {
    //Get form Data from POST array
    $orderArray = json_decode(urldecode($_POST['product_list']),true);
    $orderHistory = $orderArray;
    $comment = $_POST['comments'];
    $delivery = $_POST['next-day-delivery'];
    $id = $_POST['CMP_Member'];
    //Loop through the Array
    if(is_array($orderArray)){
    //Order Code
    $orderRequestCode = date('Y-m-d-H-i-s');
    //Get customer information here - email....
    $user = wp_get_current_user();
    $customerName =  $user->user_firstname . " " . $user->user_lastname;
    $customerEmail = $user->user_email;
    $user_ID = $user->ID;
    //Order history functionality
         add_user_meta($user->ID, "Order", $orderHistory, true);
    //Get the order Items
    echo "This is the order: ";
    $orderContent = "Order Code: " . $orderRequestCode."rn";
    $orderContent .= "Customer Name: " . $customerName . "rn";
    $orderContent .= "Customer Email: " . $customerEmail . "rn";
    if($delivery == 'yes'){
        $orderContent .= "Next Day Delivery: " . $delivery . "rn";
    }
    $orderContent .= "Order Items: " . "rn";
    foreach($orderArray as $item){
        if($item['bulk'] == 'true'){
        $orderContent .= "Stock Code: " . $item['stockCode'] . " Quantity: " . $item['quantity'] . " Bulk Order";
        if($item['sbulk']){//if true then sharing split bulk
           $orderContent .= " Sharing: " . $item['share'] . " Qty: " . $item['sharequantity'] . "rn";
        }else{
            $orderContent .= "rn";
        }
        }else{
        $orderContent .= "Stock Code: " . $item['stockCode'] . " Quantity: " . $item['quantity'] . "rn";  
        }
    }
    //Comments
    $orderContent .= "Comments: " . "rn";
    $orderContent .= $comment;
    $orderContent = wordwrap($orderContent, 70, "rn");
    //Once everything is done send to the users email address
    $headers =  'From: sales@mushrooms.ie' . "rn" .       
                    'X-Mailer: PHP/' . phpversion();
    // Send to Customer
    mail($customerEmail, 'Order Confirmation', $orderContent, $headers);
    //Send to the Sales dept.
    //mail('xxx@xxx.xxx', 'Order ' . $orderRequestCode, $orderContent, $headers);
    //Create Custom Post of the order
    $my_post = array(
            'post_title' => $orderRequestCode.' - ' . $customerName,
            'post_content' => $orderContent,
            'post_status' => 'publish',
            'post_author' => $user_ID,
            'post_type' => 'orders'
        );
    var_dump($my_post);
    // Insert the post into the database
    wp_insert_post( $my_post);
    //Delete Cookie
        unset($_COOKIE['order_cookie']);
    setcookie('order_cookie', '', time() - 3600);
    //Redirect to Dashboard
    wp_redirect( home_url().'?processed=true');
    exit;
    }
}
?>

This gives me an object that looks like this, when I retrieve it from my database using wordpress get_post($orderID);.

object(WP_Post)[325]
  public 'ID' => int 1058
  public 'post_author' => string '1' (length=1)
  public 'post_date' => string '2014-06-18 08:05:23' (length=19)
  public 'post_date_gmt' => string '2014-06-18 08:05:23' (length=19)
  public 'post_content' => string 'Order Code: 2014-06-18-08-05-20
Customer Name: Jack Coldrick
Customer Email: jack@xxx.ie
Next Day Delivery: yes
Order Items: 
Stock Code: G992/75BLK Quantity: 10
Stock Code: NETT FILM JMC Quantity: 6
Stock Code: WW42 Quantity: 4
Comments: 
' (length=254)
  public 'post_title' => string '2014-06-18-08-05-20 - Jack Coldrick' (length=35)
  public 'post_excerpt' => string '' (length=0)
  public 'post_status' => string 'publish' (length=7)
  public 'comment_status' => string 'open' (length=4)

I want my users to be able to edit their order so what I am currently doing is getting a reference to the order_content information in the above object.

string 'Order Code: 2014-06-18-08-05-20
Customer Name: Jack Coldrick
Customer Email: jack@xxx.ie
Next Day Delivery: yes
Order Items: 
Stock Code: G992/75BLK Quantity: 10
Stock Code: NETT FILM JMC Quantity: 6
Stock Code: WW42 Quantity: 4
Comments: Test Comment 123
' (length=254)

I would then essentially need to store each piece of this String in variables and populate an edit order form using these variables. My question is what is the best way to break down this String in an efficient and effective way? For example to get my Customers Name I’d ideally like to do something like

`$customerName = ???`
`echo $customerName;` //returns Jack Coldrick

Read more here: Breaking down a large String in PHP


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Related Wordpress search:

, , ,

Wordpress related questions and answers: