自定义 Woocommerce create_order api 修改
Custom Woocommerce create_order api modification
我在 wordpress woocommerce 中创建了自定义 create_order() api 函数。但我意识到它只接受非动态的单一产品。目前如果我有一个产品id 1 ~ 5 (单一产品) 6 到 10 是可变产品,这段代码每次只会添加一个产品。
如何更改我的代码,以便它能够添加 MULTIPLE 种不同的 kinds/groups 产品?是否是单一产品或可变产品或将产品捆绑为 1 个订单?
对不起我的英语不好:)
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 an id of a product that is 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 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 (!$pointstouse) {
return array(
"error" => "Need to specify points to 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 for order " . $order_id, $order_id);
return array(
"orderid" => $order_id,
"orderstatus" => "ok"
);
} else {
return array(
"error" => "You do not have enought points",
"orderstatus" => "error"
);
}
}
已解决
$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']));
}
}
}
我在 wordpress woocommerce 中创建了自定义 create_order() api 函数。但我意识到它只接受非动态的单一产品。目前如果我有一个产品id 1 ~ 5 (单一产品) 6 到 10 是可变产品,这段代码每次只会添加一个产品。
如何更改我的代码,以便它能够添加 MULTIPLE 种不同的 kinds/groups 产品?是否是单一产品或可变产品或将产品捆绑为 1 个订单?
对不起我的英语不好:)
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 an id of a product that is 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 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 (!$pointstouse) {
return array(
"error" => "Need to specify points to 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 for order " . $order_id, $order_id);
return array(
"orderid" => $order_id,
"orderstatus" => "ok"
);
} else {
return array(
"error" => "You do not have enought points",
"orderstatus" => "error"
);
}
}
已解决
$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']));
}
}
}