• Resolved lucaspalencia

    (@lucaspalencia)


    Hi!
    In my checkout, the user can select the date of shipping and the plan (example: 3 months, 6, 9). For simplify the admin, im thinking to create custorm orders and only change the date based on plan that the user selected.

    In one of my searchs, i saw about wc_create_order(), but im using woocommerce 2.1 and this function not exists.
    Some suggestions / example? The best way is update woocommerccec and use wc_create_order ?

    thanks!

Viewing 1 replies (of 1 total)
  • Thread Starter lucaspalencia

    (@lucaspalencia)

    Update to woocommerce 2.2 and:

    <?php
    /**
     * Create custom order
     **/
    function create_custom_order($orderid) {
    
    	$origin_order = new WC_Order($orderid);
    
    	//create order args
    	$args =  array(
            'status'        => 'on-hold',
        );
    
    	//order address
    	$address = array(
            'first_name' => $origin_order->billing_first_name,
            'email'      => $origin_order->billing_email,
            'phone'      => $origin_order->billing_phone,
            'address_1'  => $origin_order->billing_address_1,
            'city'       => $origin_order->billing_city,
            'state'      => $origin_order->billing_state,
            'postcode'   => $origin_order->billing_postcode,
            'country'    => 'BR'
        );
    
    	//plan by user
        $plan = $origin_order->billing_plano;
    
        //order date
        $origin_date = $origin_order->post->post_date;
    
        //create order repeater
        for ($i=0; $i < $plan; $i++) { 
    
        	//create new order
        	$order = wc_create_order($args);
    
        	//new date
        	$new_date = date('Y-m-d H:i:s',strtotime($origin_date . '+'. $i .' months'));
    		$custom_date = array(
    	      'ID'       => $order->id,
    	      'post_date' => $new_date
    	  	);
    
    	    //get items origin order
    	    $orderitems = $origin_order->get_items();
    
    		//new order add products
    		foreach ($orderitems as $item ) {
    			$productitem = apply_filters( 'woocommerce_order_item_product', $origin_order->get_product_from_item( $item ), $item );
    			$idproduct = $productitem->parent->id;
    			$order->add_product( get_product( ''.$idproduct.'' ), $item['qty'] );
    		}
    
    	    //update new order address
    	    $order->set_address( $address, 'billing' );
    
    	    //update new order shipping
    	    $order->calculate_shipping();
    
    	    //update new order totals
    	    $order->calculate_totals();
    
    	    //update new order date
    	    wp_update_post($custom_date);
        }
    }
    
    ?>
Viewing 1 replies (of 1 total)
  • The topic ‘[Plugin: Woocommerce] Custom order’ is closed to new replies.