Advertisement
wpgenie

automatically create order for user's won auctions

May 10th, 2018
982
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.06 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Functions.php
  4.  *
  5.  * @package  WSA_Customisations
  6.  * @author   WpGenie
  7.  * @since    1.0.0
  8.  */
  9.  
  10. if ( ! defined( 'ABSPATH' ) ) {
  11.     exit; // Exit if accessed directly.
  12. }
  13.  
  14. add_action( 'woocommerce_simple_auction_won', 'custom_add_action_to_order' );
  15.  
  16. function custom_add_action_to_order( $product_id ) {
  17.  
  18.     $product = wc_get_product( $product_id );
  19.     if ( $product && ! $product->get_order_id() ) {
  20.         $user_id = $product->get_auction_current_bider();
  21.         $order   = wc_create_order(
  22.             array(
  23.                 'customer_id' => $user_id,
  24.                 'created_via' => 'automatic',
  25.             )
  26.         );
  27.         if ( is_wp_error( $order ) ) {
  28.             throw new Exception( sprintf( esc_html__( 'Error %d: Unable to create order. Please try again.', 'woocommerce' ), 520 ) );
  29.         } elseif ( false === $order ) {
  30.             throw new Exception( sprintf( esc_html__( 'Error %d: Unable to create order. Please try again.', 'woocommerce' ), 521 ) );
  31.         } else {
  32.             $order_id = $order->get_id();
  33.             do_action( 'woocommerce_new_order', $order_id );
  34.         }
  35.  
  36.         $billing_addres = array(
  37.             'first_name' => get_user_meta( $user_id, 'billing_first_name', true ),
  38.             'last_name'  => get_user_meta( $user_id, 'billing_last_name', true ),
  39.             'company'    => get_user_meta( $user_id, 'billing_company', true ),
  40.             'address_1'  => get_user_meta( $user_id, 'billing_address_1', true ),
  41.             'address_2'  => get_user_meta( $user_id, 'billing_address_2', true ),
  42.             'city'       => get_user_meta( $user_id, 'billing_city', true ),
  43.             'state'      => get_user_meta( $user_id, 'billing_state', true ),
  44.             'postcode'   => get_user_meta( $user_id, 'billing_postcode', true ),
  45.             'country'    => get_user_meta( $user_id, 'billing_country', true ),
  46.             'email'      => get_user_meta( $user_id, 'billing_email', true ),
  47.             'phone'      => get_user_meta( $user_id, 'billing_phone', true ),
  48.         );
  49.  
  50.         $shipping_addres = array(
  51.             'first_name'     => get_user_meta( $user_id, 'shipping_first_name', true ),
  52.             'last_name'      => get_user_meta( $user_id, 'shipping_last_name', true ),
  53.             'company'        => get_user_meta( $user_id, 'shipping_company', true ),
  54.             'address_1'      => get_user_meta( $user_id, 'shipping_address_1', true ),
  55.             'address_2'      => get_user_meta( $user_id, 'shipping_address_2', true ),
  56.             'city'           => get_user_meta( $user_id, 'shipping_city', true ),
  57.             'state'          => get_user_meta( $user_id, 'shipping_state', true ),
  58.             'postcode'       => get_user_meta( $user_id, 'shipping_postcode', true ),
  59.             'country'        => get_user_meta( $user_id, 'shipping_country', true ),
  60.             'email'          => get_user_meta( $user_id, 'shipping_email', true ),
  61.             'shipping_phone' => get_user_meta( $user_id, 'shipping_phone', true ),
  62.         );
  63.         $order->add_product( $product, 1 );
  64.         $order->set_address( $billing_addres, 'billing' );
  65.         if ( ! array_filter( $shipping_addres ) ) {
  66.             $shipping_addres = $billing_addres;
  67.         }
  68.         $order->set_address( $shipping_addres, 'shipping' );
  69.         $order->calculate_totals();
  70.         $order->update_status( 'processing' );
  71.  
  72.         wc_update_product_stock( $product_id, '0' );
  73.  
  74.     }
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement