PHP
Woocommerce create order programmatically

Woocommerce create order programmatically

Recently I have a side project that can be quickly completed with the Woocommerce plugin, I started to do some research on it. I feel the checkout process is too long and unnecessary, so I customized it on the theme, hide the cart page, checkout page, user just click the buy button, then pay with the payment gateway and redirect to invoice page automatically. Here is my sample code of functions.php.

add_action('woocommerce_add_to_cart_redirect', 'my_checkout');


function my_checkout() {

  	
 global $woocommerce;

  	
 $items = $woocommerce->cart->get_cart();

    // check the category of product
  	
 foreach($items as $item => $values) {
  		
   $terms = get_the_terms( $values['product_id'], 'product_cat' );
  		
      foreach ($terms as $term) {
            
        $_categoryid = $term->term_id;
            
        if ($_categoryid === 51 ) {
                
          wp_safe_redirect(wc_get_cart_url());
                
          exit;
            
        }
        
      }
  	
 }

    
 $address = array(
      	
   'first_name' => $woocommerce->customer->get_billing_first_name(),
      	
   'last_name'  => $woocommerce->customer->get_billing_last_name(),
      	
   'company'    => $woocommerce->customer->get_billing_company(),
      	
   'email'      => $woocommerce->customer->get_billing_email(),
      	
   'phone'      => $woocommerce->customer->get_billing_phone(),
      	
   'address_1'  => $woocommerce->customer->get_billing_address_1(),
      	
   'address_2'  => $woocommerce->customer->get_billing_address_2(),
      	
   'city'       => $woocommerce->customer->get_billing_city(),
      	
   'state'      => $woocommerce->customer->get_billing_state(),
      	
   'postcode'   => $woocommerce->customer->get_billing_postcode(),
      	
   'country'    => $woocommerce->customer->get_billing_country()
  	
 );

    
 // assign the user id to the order 
    
 $userid = get_current_user_id();
  	
 $order = wc_create_order(array('customer_id' => $userid));

  	
 foreach($items as $item => $values) {
  		
   $order->add_product( wc_get_product($values['product_id']), 1);
  	
 }

  	
 $order->set_address($address, 'billing');
  	
 $order->calculate_totals();
  	
 $order_id = $order->get_order_number();
  	
 update_post_meta( $order_id, '_payment_method', 'mycred' );
  	
 update_post_meta( $order_id, '_payment_method_title', 'Pay with mycred' );

  	
 WC()->session->order_awaiting_payment = $order_id;

  	
 $available_gateways = WC()->payment_gateways->get_available_payment_gateways();
  	
 $result = $available_gateways['mycred']->process_payment( $order_id );

   	
 if ( $result['result'] == 'success' ) {
        
   $result = apply_filters( 'woocommerce_payment_successful_result', $result, $order_id );
        
   // $order->update_status("Completed", 'Imported order', TRUE);
        
   wp_safe_redirect( $result['redirect'] );
        
   exit;
    
 }

}
Digiprove sealCopyright secured by Digiprove © 2018-2019

發表迴響