2014-06-30 42 views
0

我试图让我的用户能够从我的网站上的控制面板编辑他们的订单历史记录。目前,当用户下订单执行该脚本:在PHP中分解大字符串

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; 
    } 
} 
?> 

这给了我一个对象,它看起来像这样,当我使用wordpressget_post($orderID);从我database检索。

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) 

我希望我的用户能够编辑他们的订单还等什么我目前做的是让在上述目的的order_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: Test Comment 123 
' (length=254) 

我会那么基本上需要每一块这种String的存储variables和使用这些variables填充编辑订单。我的问题是以高效和有效的方式分解此String的最佳方式是什么?例如,为了让我的客户的名字,我会非常喜欢这样做

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

为什么它必须是一个大字符串?你能否将数据作为隐含变量进行发布?这将使验证/验证/存储/检索更容易。 – Husman

+0

忽略我上面的评论,可湿性粉剂是一个可怕的方式来做一个购物车,它是一个博客平台毕竟。也许你应该考虑拆分“客户名称”和“客户电子邮件”之间的字符串,然后是“客户电子邮件”和“下一天交付”之间的字符串,并将这些字符串存储到变量中。一个杂种,是的;可行,是的。 – Husman

回答

0

什么,你向我们展示的是已经被分开分成独立性质的\WP_Post对象。 WordPress的帖子也有Revision system。您只需在您的wp-config.php中启用它们即可。

define('WP_POST_REVISIONS', 3); 

每次更新(自定义)岗位型,WordPress的需要旧版本,它在你的{$prefix}_posts表作为revision保存为新行。您还有一个UI来区分版本和回滚到旧版本。