Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save flayder/308d4d42e5b8bf2538ef39275dce9c0c to your computer and use it in GitHub Desktop.
Save flayder/308d4d42e5b8bf2538ef39275dce9c0c to your computer and use it in GitHub Desktop.
add_action( 'wpcf7_before_send_mail', 'create_order_function', 20, 3 );
function create_order_function( $contact_form, &$abort, $that ){
$data = $that->get_posted_data();
$url = $_SERVER["REQUEST_URI"];
if( $data['order-form'] == "Y" ) {
/*Array
(
[your-city] =>
[your-name] =>
[your-email] => asda@asdad.ty
[your-phone] => 1231231
[your-diapason-from] =>
[your-diapason-to] =>
[your-text-message] =>
[heating] => Array
(
[0] =>
)
[located] => Array
(
[0] =>
)
[multilinefile] =>
)*/
$address = array(
'email' => $data['your-email'],
'phone' => $data['your-phone'],
);
if( ! empty($data['your-city']) )
$address['city'] = $data['your-city'];
if( ! empty($data['your-name']) )
$address['first_name'] = $data['your-name'];
if( ! empty($data['your-diapason-from']) )
$address['your-diapason-from'] = $data['your-diapason-from'];
if( ! empty($data['your-diapason-to']) )
$address['your-diapason-to'] = $data['your-diapason-to'];
if( ! empty($data['your-text-message']) )
$address['your-text-message'] = $data['your-text-message'];
if( ! empty($data['heating'][0]) )
$address['heating'] = implode(", ", $data['heating']);
if( ! empty($data['located'][0]) )
$address['located'] = implode(", ", $data['located']);
// Now we create the order
$order = wc_create_order();
global $woocommerce;
$cart = WC()->cart;
include_once WC_ABSPATH . 'includes/wc-cart-functions.php';
include_once WC_ABSPATH . 'includes/class-wc-cart.php';
if ( is_null( $cart ) ) {
wc_load_cart();
}
$cart = WC()->cart;
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
$order->add_product( get_product($cart_item['product_id']), $cart_item['quantity']);
}
$order->set_address( $address, 'billing' );
$order->calculate_totals();
$order->update_status('completed');
$order->save();
WC()->cart->empty_cart(true);
WC()->session->set('cart', array());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment