0

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' => '[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"
            );
        }
    }
4
  • Put the array in the variable, $cart_array = get_cart(); and then use foreach to go through the array and get what you need. Can you post the code (it's not exactly visible what's the full structure of the array)... Commented Oct 14, 2015 at 6:07
  • @dingo_d thx for reminding i edited my post. i don't really get what u mean $cart_array = get_cart(); Commented Oct 14, 2015 at 6:31
  • 2
    You should really edit-out personal information and use dummy data instead when asking questions on stackoverflow. Commented Oct 14, 2015 at 7:11
  • @Ultimater Thanks for bring that up. the static data of address are fake of cos :D Commented Oct 14, 2015 at 7:24

1 Answer 1

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']
     );
}
Sign up to request clarification or add additional context in comments.

1 Comment

i try to add the code return array($item['product_id'],$item['variation_id'],$item['quantity']); it only return me 1 array of product? did i missing some loop?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.