2015-10-14 24 views
0

我想從此購物車陣列獲取product_id,variation_id和數量。順便說一下這個購物車數組是由woocommerce get_cart函數返回的。我如何在PHP中讀取這個數組,以便獲取數據?我需要這3個數據來創建一個訂單。如何讀取陣列結構並獲取Php中的某些數據

public function get_cart(){ 
      $user_id = 1; 
      $key = '_woocommerce_persistent_cart'; 
      $single = true; 
      $carts = get_user_meta($user_id, $key, $single); 
      return $carts; 
     } 

enter image description here

我有自定義以下我能怎樣改變訂單創建的代碼?

public function Order_creation() 
    { 
     if ($_REQUEST['dev']) { 
      $address = array(
      'first_name' => 'Zayle', 
      'last_name' => 'Ong', 
      'company' => 'Timios', 
      'email' => '[email protected]', 
      'phone' => '777-777-777-777', 
      'address_1' => '31 Main Street', 
      'address_2' => '', 
      'city' => 'Simei', 
      'state' => 'SG', 
      'postcode' => '520151', 
      'country' => 'Singapore' 
      ); 
      $userid = 1; 
      /* 
       * Example product 1 simple 
      */ 
      $pointsEarn = 88; 
      $products[] = array(
      "id" => 9, // id of product 
      "variation" => '', // id of variaton 
      "quantity" => 1 
      ) // quantity 
      ; 
      /* 
       * Example product variation 
      */ 
      $products[] = array(
      "id" => 76, // id of product 
      "variation" => 97, // id of variaton 
      "quantity" => 2 
      ); // quantity 
      $products[] = array(
      "id" => 76, // id of product 
      "variation" => 98, // id of variaton 
      "quantity" => 1 
      ); 
      $redeemedPoints = 100; 
      $note = "Test Note"; 
      } else { 
      $address = array(
      'first_name' => $_POST['first_name'], 
      'last_name' => $_POST['last_name'], 
      'company' => $_POST['company'], 
      'email' => $_POST['email'], 
      'phone' => $_POST['phone'], 
      'address_1' => $_POST['adddress1'], 
      'address_2' => $_POST['adddress2'], 
      'city' => $_POST['city'], 
      'state' => $_POST['state'], 
      'postcode' => $_POST['postcode'], 
      'country' => $_POST['country'] 
      ); 
      $userid = $_POST['userid']; 
      /* 
       * POST products should be like 
       * array(array("id"=>1,"variation"=>"","quantity"),array("id"=>1,"variation"=>"","quantity"=>1),array("id"=>1,"variation"=>"","quantity"=>3)) 
      */ 
      $pointsEarn = $_POST['PointsEarn']; 
      $products = $_POST['products']; 
      $redeemedPoints = $_POST['redeemedPoints']; 
      $note = $_POST['note']; 
      if (! $_POST['first_name'] && ! $_POST['last_name'] && ! $_POST['email'] && ! $_POST['adddress1'] & ! $_POST['city']) { 
       return array(
       "error" => "Please fill First name, Last Name, Address and City", 
       "orderstatus" => "error" 
       ); 
      } 
      if (! $userid) { 
       return array(
       "error" => "Need to specify a userid", 
       "orderstatus" => "error" 
       ); 
      } 
      if (! $productid) { 
       return array(
       "error" => "Need to specify a product id", 
       "orderstatus" => "error" 
       ); 
      } 
      if (! $redeemedPoints) { 
       return array(
       "error" => "Need to specify points to use", 
       "orderstatus" => "error" 
       ); 
      } 
     } 

     $pointsuser = WC_Points_Rewards_Manager::get_users_points($userid); 
     if ($pointsuser >= $$redeemedPoints) { 
      $order = wc_create_order(); 
      if (count($products)) { 
       foreach ($products as $key => $value) { 
        if ($value['variation']) { 
         $product = new WC_Product_Variable($value['id']); 
         $product->variation_id = $value['variation']; 
         $variation = $product->get_available_variations(); 
         foreach ($variation as $key2 => $value2) { 
          if ($value2['variation_id'] == $value['variation']) { 
           $valdata['variation'] = $value2['attributes']; 
          } 
         } 
         $order->add_product($product, $value['quantity'], $valdata); 
         update_post_meta($value['variation'], "_stock", (get_post_meta($productid, "_stock", true) - $value['quantity'])); 
         } else { 
         $product = new WC_Product($value['id']); 
         $order->add_product($product, $value['quantity']); 
         update_post_meta($value['id'], "_stock", (get_post_meta($productid, "_stock", true) - $value['quantity'])); 
        } 
       } 
      } 

      if (! $product->is_type('variable')) {} else {} 

      $order->set_address($address, 'billing'); 
      $order->set_address($address, 'shipping'); 
      $discount_code = str_replace("--userid--", $userid, "wc_points_redemption_--userid--_" . date("d-m-Y") . "_" . rand(0, 99999)); 
      /* 
       * Create coupon 
      */ 
      $coupon_code = $discount_code; // Code 
      $amount = WC_Points_Rewards_Manager::calculate_points_value($redeemedPoints); // Amount 
      $discount_type = 'fixed_cart'; // Type: fixed_cart, percent, fixed_product, percent_product 

      $coupon = array(
      'post_title' => $coupon_code, 
      'post_content' => '', 
      'post_status' => 'publish', 
      'post_author' => $userid, 
      'post_type' => 'shop_coupon' 
      ); 

      $new_coupon_id = wp_insert_post($coupon); 

      // Add meta 
      update_post_meta($new_coupon_id, 'discount_type', $discount_type); 
      update_post_meta($new_coupon_id, 'coupon_amount', $amount); 
      update_post_meta($new_coupon_id, 'individual_use', 'no'); 
      update_post_meta($new_coupon_id, 'product_ids', ''); 
      update_post_meta($new_coupon_id, 'exclude_product_ids', ''); 
      update_post_meta($new_coupon_id, 'usage_limit', '1'); 
      update_post_meta($new_coupon_id, 'expiry_date', ''); 
      update_post_meta($new_coupon_id, 'apply_before_tax', 'yes'); 
      update_post_meta($new_coupon_id, 'free_shipping', 'no'); 
      $order->add_order_note($note); 
      $order->add_coupon($discount_code, $amount); 
      $order->calculate_totals(); 
      $order->set_total($order->calculate_totals() - $amount); 
      $order->set_total($amount, 'cart_discount'); 
      $orderid = new WC_Order($order->ID); 
      $order_id = trim(str_replace('#', '', $order->get_order_number())); 

      add_post_meta($order_id, '_payment_method', 'Pending Payment'); 
      update_post_meta($order_id, '_created_via', 'checkout'); 
      update_post_meta($order_id, '_customer_user', $userid); 
      add_post_meta($order_id, '_payment_method_title', 'Pending Payment'); 
      update_post_meta($order->id, '_wc_points_redeemed', $redeemedPoints); 
      WC_Points_Rewards_Manager::decrease_points($userid, $redeemedPoints, 'order-redeem', "coupon " . $coupon_code . " used for order " . $order_id, $order_id); 
      update_post_meta($order->id, '_wc_points_earned', $pointsEarn); 
      WC_Points_Rewards_Manager::increase_points($order->user_id, $pointsEarn, 'order-placed', null, $order->id); 
      return array(
      "orderid" => $order_id, 
      "points earn" =>$pointsEarn, 
      "orderstatus" => "ok" 
      ); 
      } else { 
      return array(
      "error" => "You do not have enought points", 
      "orderstatus" => "error" 
      ); 
     } 
    } 
+0

把陣列中的變量,'$ cart_array = get_cart();',然後用[的foreach(http://php.net/manual/en/control-structures.foreach。 PHP)來通過數組,並得到你所需要的。你可以發佈代碼(它不完全可見什麼是數組的完整結構)... –

+0

@dingo_d thx提醒我編輯我的帖子。我真的不明白你的意思$ cart_array = get_cart(); – xSea

+2

當您在提問問題時,您應該真正編輯出個人信息並使用虛擬數據。 – Ultimater

回答

0
global $woocommerce; 
$items = $woocommerce->cart->get_cart(); 

foreach($items as $item) { 
    $products[] = array(
     "id" => $item['product_id'], // id of product 
     "variation" => $item['variation_id'], // id of variaton 
     "quantity" => $item['quantity'] 
    ); 
} 
+0

我嘗試添加代碼 返回數組($ item ['product_id'],$ item ['variation_id'],$ item ['quantity']); 它只返回我1個產品陣列?我錯過了一些循環? – xSea