wordpress - Breaking down a large String in PHP

282

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."\r\n";
    $orderContent .= "Customer Name: " . $customerName . "\r\n";
    $orderContent .= "Customer Email: " . $customerEmail . "\r\n";
    if($delivery == 'yes'){
        $orderContent .= "Next Day Delivery: " . $delivery . "\r\n";
    }

    $orderContent .= "Order Items: " . "\r\n";


    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'] . "\r\n";
        }else{
            $orderContent .= "\r\n";
        }
        }else{
        $orderContent .= "Stock Code: " . $item['stockCode'] . " Quantity: " . $item['quantity'] . "\r\n";  
        }
    }

    //Comments
    $orderContent .= "Comments: " . "\r\n";
    $orderContent .= $comment;

    $orderContent = wordwrap($orderContent, 70, "\r\n");

    //Once everything is done send to the users email address
    $headers =  'From: [email protected]' . "\r\n" .       
                    'X-Mailer: PHP/' . phpversion();

    // Send to Customer
    mail($customerEmail, 'Order Confirmation', $orderContent, $headers);


    //Send to the Sales dept.
    //mail('[email protected]', '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 mydatabase usingwordpressget_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: [email protected]
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 theorder_content information in the above object.

string 'Order Code: 2014-06-18-08-05-20
Customer Name: Jack Coldrick
Customer Email: [email protected]
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 thisString invariables and populate an edit order form using thesevariables. 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
143

Answer

Solution:

What you are showing us is a\WP_Post object that already is split apart into individual properties. WordPress also has a Revision system for posts. You just have to enable them in yourwp-config.php.

define( 'WP_POST_REVISIONS', 3 );

Every time you update a (custom) post type, WordPress takes the old version and saves it as new row in your{$prefix}_posts table asrevision. You also have a UI to diff versions and rollback to old versions.

People are also looking for solutions to the problem: php - How to get all the videos link from a youtube playlist .

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.