Category Archives: WooCommerce

How To Execute Woocommerce Payment Programmatically

How to trigger Woocommerce payment process like checkout but it should process in the background

To trigger Woocommerce payment process, need to call “process_payment” method. Here is an example to process Woocommerce payment programmatically.

global $woocommerce;
$address = array(
      'first_name' => 'Test',
      'last_name'  => 'Test',
      'company'    => 'Test',
      'email'      => 'test@softwarehtec.com',
      'phone'      => '00-00-0000',
      'address_1'  => '0 test st',
      'address_2'  => '',
      'city'       => 'Test',
      'state'      => 'NY',
      'postcode'   => '10010',
      'country'    => 'US'
);


$order = wc_create_order();
$order->add_product( get_product('1'), 1); 
$order->set_address( $address, 'billing' );
$order->set_address( $address, 'shipping' );
$order->calculate_totals();

update_post_meta( $order->id, '_payment_method', 'paypal' );
update_post_meta( $order->id, '_payment_method_title', 'PayPal' );

WC()->session->order_awaiting_payment = $order->id;

$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
$result = $available_gateways[ 'paypal' ]->process_payment( $order->id );

if ( $result['result'] == 'success' ) {
    $result = apply_filters( 'woocommerce_payment_successful_result', $result, $order->id );
    wp_redirect( $result['redirect'] );
    exit;
}


Create A WordPress Woocommerce Coupon Programatically

How can I create a WordPress Woocommerce coupon programmatically

The Woocommerce coupon is kind of post. Its similar to creating a post.

coupon_code = 'Test'; // Code
$amount = '1'; // Amount
$discount_type = 'fixed_cart'; // Type: fixed_cart, percent, fixed_product, percent_product
					
$coupon = array(
	'post_title' => $coupon_code,
	'post_content' => '',
	'post_status' => 'publish',
	'post_author' => 1,
	'post_type' => 'shop_coupon'
);
					
$new_coupon_id = wp_insert_post( $coupon );
					

update_post_meta( $new_coupon_id, 'discount_type', $discount_type );
update_post_meta( $new_coupon_id, 'coupon_amount', $amount );
update_post_meta( $new_coupon_id, 'individual_use', 'no' );
update_post_meta( $new_coupon_id, 'product_ids', '' );
update_post_meta( $new_coupon_id, 'exclude_product_ids', '' );
update_post_meta( $new_coupon_id, 'usage_limit', '' );
update_post_meta( $new_coupon_id, 'expiry_date', '' );
update_post_meta( $new_coupon_id, 'apply_before_tax', 'no' );
update_post_meta( $new_coupon_id, 'free_shipping', 'no' );

How Can I Create An Woocommerce Order Programmatically

How can I create an Woocommerce Order Programmatically

You can use wc_create_order() to setup an Woocommerce order object and assign data you would like.

global $woocommerce;
$address = array(
      'first_name' => 'Test',
      'last_name'  => 'Test',
      'company'    => 'Test',
      'email'      => 'test@softwarehtec.com',
      'phone'      => '00-00-0000',
      'address_1'  => '0 test st',
      'address_2'  => '',
      'city'       => 'Test',
      'state'      => 'NY',
      'postcode'   => '10010',
      'country'    => 'US'
);


$order = wc_create_order();
$order->add_product( get_product('1'), 1); 
$order->set_address( $address, 'billing' );

$order->calculate_totals();
$order->update_status("completed", 'Created order programmatically', TRUE);