<?php
/**
* Functions.php
*
* @package WSA_Customisations
* @author WpGenie
* @since 1.0.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
add_action( 'woocommerce_simple_auction_won', 'custom_add_action_to_order' );
function custom_add_action_to_order( $product_id ) {
$product = wc_get_product( $product_id );
if ( $product && ! $product->get_order_id() ) {
$user_id = $product->get_auction_current_bider();
$order = wc_create_order(
array(
'customer_id' => $user_id,
'created_via' => 'automatic',
)
);
if ( is_wp_error( $order ) ) {
throw new Exception( sprintf( esc_html__( 'Error %d: Unable to create order. Please try again.', 'woocommerce' ), 520 ) );
} elseif ( false === $order ) {
throw new Exception( sprintf( esc_html__( 'Error %d: Unable to create order. Please try again.', 'woocommerce' ), 521 ) );
} else {
$order_id = $order->get_id();
do_action( 'woocommerce_new_order', $order_id );
}
$billing_addres = array(
'first_name' => get_user_meta( $user_id, 'billing_first_name', true ),
'last_name' => get_user_meta( $user_id, 'billing_last_name', true ),
'company' => get_user_meta( $user_id, 'billing_company', true ),
'address_1' => get_user_meta( $user_id, 'billing_address_1', true ),
'address_2' => get_user_meta( $user_id, 'billing_address_2', true ),
'city' => get_user_meta( $user_id, 'billing_city', true ),
'state' => get_user_meta( $user_id, 'billing_state', true ),
'postcode' => get_user_meta( $user_id, 'billing_postcode', true ),
'country' => get_user_meta( $user_id, 'billing_country', true ),
'email' => get_user_meta( $user_id, 'billing_email', true ),
'phone' => get_user_meta( $user_id, 'billing_phone', true ),
);
$shipping_addres = array(
'first_name' => get_user_meta( $user_id, 'shipping_first_name', true ),
'last_name' => get_user_meta( $user_id, 'shipping_last_name', true ),
'company' => get_user_meta( $user_id, 'shipping_company', true ),
'address_1' => get_user_meta( $user_id, 'shipping_address_1', true ),
'address_2' => get_user_meta( $user_id, 'shipping_address_2', true ),
'city' => get_user_meta( $user_id, 'shipping_city', true ),
'state' => get_user_meta( $user_id, 'shipping_state', true ),
'postcode' => get_user_meta( $user_id, 'shipping_postcode', true ),
'country' => get_user_meta( $user_id, 'shipping_country', true ),
'email' => get_user_meta( $user_id, 'shipping_email', true ),
'shipping_phone' => get_user_meta( $user_id, 'shipping_phone', true ),
);
$order->add_product( $product, 1 );
$order->set_address( $billing_addres, 'billing' );
if ( ! array_filter( $shipping_addres ) ) {
$shipping_addres = $billing_addres;
}
$order->set_address( $shipping_addres, 'shipping' );
$order->calculate_totals();
$order->update_status( 'processing' );
wc_update_product_stock( $product_id, '0' );
}
}
Comments