duankeng1911 2015-10-14 06:02
浏览 89
已采纳

如何在Php中读取数组结构并获取某些数据

I want to get the product_id,variation_id,and quantity from this cart array. By the way this cart array was return by woocommerce get_cart function. How do I read this array in Php so I can get the data? I need those 3 data to create an order with it.

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

i have customize order creation code below how can i change it?

public function Order_creation()
    {
        if ($_REQUEST['dev']) {
            $address = array(
            'first_name' => 'Zayle',
            'last_name' => 'Ong',
            'company' => 'Timios',
            'email' => 'Crystalize@hotmail.com',
            '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"
            );
        }
    }
  • 写回答

1条回答 默认 最新

  • duanhuan1147 2015-10-14 06:39
    关注
    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']
         );
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)