Last active
April 15, 2024 23:58
-
-
Save mlbd/27b91522e2bc984174e9538b6e5fa1d0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Get shipping info by using chosen method from cart | |
* | |
* @param string $type | |
* @return string | |
*/ | |
function ml_get_shipping_data($type = '') { | |
$current_shipping_method = WC()->session->get( 'chosen_shipping_methods' ); | |
if( ! is_array($current_shipping_method) ) { | |
return ''; | |
} | |
if( $type === 'method' ) { | |
return reset($current_shipping_method); | |
} | |
$packages = WC()->shipping()->get_packages(); | |
// Loop through each package | |
foreach ($packages as $package_key => $package) { | |
// Get the shipping rates for the package | |
$rates = $package['rates']; | |
// Loop through each shipping rate | |
foreach ($rates as $rate_key => $rate) { | |
// Get the shipping cost for the rate | |
if ($current_shipping_method[0] == $rate->id) { | |
$shipping_cost = $rate->cost; | |
$shipping_label = $rate->label; | |
if( $type === 'cost' ) { | |
return $shipping_cost; | |
} | |
return $shipping_label; | |
} | |
} | |
} | |
} | |
/** | |
* Create WooCommerce order programmatically | |
* | |
* @param array $data | |
* @return int | |
*/ | |
function ml_create_order($data) { | |
global $woocommerce; | |
$cart = $woocommerce->cart; | |
$cart->calculate_totals(); | |
$applied_coupons = WC()->cart->get_applied_coupons(); | |
$current_user = wp_get_current_user(); | |
$user_id = $current_user->ID; | |
$user_login = $current_user->user_login; | |
$products = $data['products']; | |
$customerInfo = $data['customerInfo']; | |
$cardNumber = isset( $data['cardNumber'] ) && ! empty( $data['cardNumber'] ) ? $data['cardNumber'] : ''; | |
$extraMeta = isset( $data['extraMeta'] ) ? $data['extraMeta'] : []; | |
$response = isset( $data['response'] ) ? $data['response'] : []; | |
$update = isset( $data['update'] ) ? true : false; | |
$fullname = isset( $customerInfo['name'] ) ? $customerInfo['name'] : ''; | |
// Assuming you have received payment response and details | |
$order = wc_create_order(); | |
// Loop through the products and add them to the order | |
foreach ($products as $product) { | |
$product_id = $product['product_id']; | |
$_product = wc_get_product( $product_id ); | |
$image_id = $_product->get_image_id(); | |
$quantity = $product['quantity']; | |
$color = isset( $product['color'] ) ? $product['color'] : ''; // Assuming 'color' is part of your product data | |
$size = isset( $product['size'] ) ? $product['size'] : ''; // Assuming 'size' is part of your product data | |
$price = isset( $product['price'] ) ? $product['price'] : ''; | |
$alarnd_color_key = isset( $product['alarnd_color_key'] ) ? $product['alarnd_color_key'] : ''; | |
$alarnd_custom_color = isset( $product['alarnd_custom_color'] ) ? $product['alarnd_custom_color'] : ''; | |
$alarnd_step_key = isset( $product['alarnd_step_key'] ) ? $product['alarnd_step_key'] : ''; | |
$extra_meta = []; | |
if( ! empty( $color ) ) { | |
$extra_meta['color'] = $color; | |
} | |
if( ! empty( $size ) ) { | |
$extra_meta['size'] = $size; | |
} | |
if( ! empty( $price ) ) { | |
$extra_meta['price'] = $price; | |
$extra_meta['subtotal'] = $price*$quantity; | |
$extra_meta['total'] = $price*$quantity; | |
} | |
$item_id = $order->add_product($_product, $quantity, $extra_meta); | |
// Add color and size as order item meta | |
if( ! empty( $color ) ) { | |
wc_add_order_item_meta($item_id, __('Color', 'hello-elementor'), $color); | |
} | |
if( ! empty( $alarnd_custom_color ) ) { | |
wc_add_order_item_meta($item_id, __('Color', 'hello-elementor'), $alarnd_custom_color); | |
} | |
if( ! empty( $size ) ) { | |
wc_add_order_item_meta($item_id, __('Size', 'hello-elementor'), $size); | |
} | |
if( ! empty( $image_id ) ) { | |
$gen_thumbnail = ml_get_wc_thumbnail_url( $image_id, $product_id, $user_id, $alarnd_color_key ); | |
if( ! empty( $gen_thumbnail ) ) { | |
// Add custom thumbnail URL as post meta for the product | |
update_post_meta($product_id, '_generated_thumbnail_url', $gen_thumbnail); | |
} | |
} | |
} | |
// Set billing and shipping addresses | |
$order->set_address($customerInfo, 'billing'); | |
$order->set_address($customerInfo, 'shipping'); | |
$order->set_customer_id( $user_id ); | |
// Set payment method (e.g., 'zcredit_checkout_payment' for zcredit) | |
$order->set_payment_method('zcredit_checkout_payment'); | |
$order->set_payment_method_title('Z-Credit Payment'); | |
// Get the chosen shipping method | |
$chosen_shipping_method = ml_get_shipping_data('method'); | |
// Set shipping method | |
if (!empty($chosen_shipping_method)) { | |
$shipping_cost = ml_get_shipping_data('cost'); | |
$shipping_title = ml_get_shipping_data(); | |
// Create a new shipping item | |
$shipping_item = new WC_Order_Item_Shipping(); | |
$shipping_item->set_method_title($shipping_title); // Replace with the shipping method title | |
$shipping_item->set_method_id($chosen_shipping_method); | |
$shipping_item->set_total($shipping_cost); | |
// Add the shipping item to the order | |
$order->add_item($shipping_item); | |
// Recalculate totals and save the order | |
$order->calculate_totals(); | |
} | |
if( ! empty( $response ) && isset( $response['referenceID'] ) ) { | |
$order->add_order_note( __( 'Z-Credit Payment Complete.', 'woocommerce_zcredit' ) ); | |
$order->add_order_note( "Refence Number: #".$response['referenceID']." for Z-Credit" ); | |
$order->payment_complete(); | |
} | |
if( ! empty( $response ) && isset( $response['referenceID'] ) ) { | |
if( isset( $response['token'] ) && ! empty( $response['token'] ) ) { | |
update_post_meta( $order->get_id(), 'zc_payment_token', $response['token'] ); | |
update_post_meta( $order->get_id(), 'zc_transaction_id', $response['referenceID'] ); | |
} | |
if( true === $update ) { | |
//TODO - update token and customer info if new or change input. | |
if( isset( $response['token'] ) && ! empty( $response['token'] ) ) { | |
// ACF field update | |
update_acf_usermeta( $user_id, 'token', $response['token'] ); | |
} | |
if( isset( $extraMeta['invoice'] ) && ! empty( $extraMeta['invoice'] ) ) { | |
update_acf_usermeta($user_id, 'invoice', $extraMeta['invoice']); | |
} | |
if( isset( $extraMeta['city'] ) && ! empty( $extraMeta['city'] ) ) { | |
update_user_meta_if_different($user_id, 'billing_city', $extraMeta['city']); | |
} | |
if( ! empty( $cardNumber ) ) { | |
$last_four_digit = ml_get_last_four_digit($cardNumber); | |
$card_type = ml_get_card_type($cardNumber); | |
$card_info = []; | |
$card_info['last_4_digit'] = $last_four_digit; | |
$card_info['card_type'] = $card_type; | |
update_acf_usermeta( $user_id, 'card_info', $card_info); | |
} | |
$phoneNumber = ml_get_phone_no( $customerInfo['phone'] ); | |
$countryCode = ml_get_country_code(); | |
// WcooCommerce user field update | |
update_user_meta_if_different($user_id, 'billing_address_1', $customerInfo['address_1']); | |
update_user_meta_if_different($user_id, 'billing_phone', $customerInfo['phone']); | |
update_user_meta_if_different($user_id, 'xoo_ml_phone_code', $countryCode); | |
update_user_meta_if_different($user_id, 'xoo_ml_phone_no', $phoneNumber); | |
// Email address | |
update_user_email_if_different($user_id, $customerInfo['email']); | |
// Display Name | |
update_user_name_if_different($user_id, $fullname); | |
} | |
} | |
// Set the applied coupons to the order | |
foreach ($applied_coupons as $coupon_code) { | |
$order->apply_coupon($coupon_code); | |
} | |
$woocommerce->cart->calculate_totals(); | |
$order->calculate_totals(); | |
// Mark the order as paid (change this status to match your payment method) | |
$order->set_status('wc-processing'); | |
// Save the order | |
$order->save(); | |
// Trigger order notification emails | |
wc_mail('new_order', __('New Order', 'woocommerce'), '', '', array('order' => $order)); | |
return $order->get_id(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment