wordpress - Custom Woocommerce create_order api modification -
i have created custom create_order() api function in wordpress woocommerce. realize take in singular product not dynamically. if have product id 1 ~ 5 (single product) 6 10 variable products,this code add 1 product each time.
how change code able add multiple different kinds/groups of products? whether or not single product or variable product or bundle products 1 order?
sorry bad english :)
public function create_order() { if ($_request['dev']) { $address = array( 'first_name' => 'zayle', 'last_name' => 'ong', 'company' => 'timios', 'email' => 'crystalizewy@hotmail.com', 'phone' => '777-777-777-777', 'address_1' => '31 main street', 'address_2' => '', 'city' => 'simei', 'state' => 'sg', 'postcode' => '520151', 'country' => 'singapore' ); $userid = 1; $productid = 196; // put here id of product available on woocommerce->products $pointstouse = 100; } 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']; $productid = $_post['productidid']; $pointstouse = $_post['pointstouse']; if (! $_post['first_name'] && ! $_post['last_name'] && ! $_post['email'] && ! $_post['adddress1'] & ! $_post['city']) { return array( "error" => "please fill first name, last name, address , city", "orderstatus" => "error" ); } if (!$userid) { return array( "error" => "need specify userid", "orderstatus" => "error" ); } if (!$productid) { return array( "error" => "need specify product id", "orderstatus" => "error" ); } if (!$pointstouse) { return array( "error" => "need specify points use", "orderstatus" => "error" ); } } $pointsuser = wc_points_rewards_manager::get_users_points($userid); if ($pointsuser >= $pointstouse) { $order = wc_create_order(); $product = new wc_product($productid); if (!$product->is_type('variable')) { update_post_meta($productid, "_stock", (get_post_meta($productid, "_stock", true) - 1)); }else{ } $order->add_product($product, 1); $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($pointstouse); // 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' => 1, '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_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', 'cheque'); update_post_meta($order_id, '_created_via', 'checkout'); update_post_meta($order_id, '_customer_user', $userid); add_post_meta($order_id, '_payment_method_title', 'cheque payment'); update_post_meta($order->id, '_wc_points_redeemed', $pointstouse); wc_points_rewards_manager::decrease_points($userid, $pointstouse, 'order', "coupon " . $coupon_code . " used order " . $order_id, $order_id); return array( "orderid" => $order_id, "orderstatus" => "ok" ); } else { return array( "error" => "you not have enought points", "orderstatus" => "error" ); } }
solved
$order = wc_create_order(); if (count($products)) { foreach ($products $key => $value) { if ($value['variation']) { $product = new wc_product_variable($value['id']); $product->variation_id = $value['variation']; $variation = $product->get_available_variations(); foreach ($variation $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'])); } } }
Comments
Post a Comment