snippet: working WooCommerce order generator
-
I scoured the interwebz looking for this and none of the plethora of examples worked for me.
Perhaps this snippet will be of some use to others wanting to build orders via their CF7 forms and don’t want to use the
/cart?add-to-cart=
URL method.
In my use case, that method won’t work, since it takes the user immediately to checkout, and my client wants the user to wait for their application to be manually approved before being allowed to go to checkout.So, I coded this backend order builder for a combination dance school class enrollment & website account registration form. This snippet obviously goes inside a larger function.
Doing it this way avoids using global variables, and besides, I couldn’t get
$id = $product->get_id();
to work anyway (nor didwc_get_product_id_by_sku
, nor any of the MANY variations and schemes I tried. The ONLY thing that works in my case is to associate known product IDs with CF7 dropdown options. I presume it’s due to this being in a function (CF7wpcf7_before_send_mail
) that doesn’t hook into WC. <shrugh>
Thus, this snippet doesn’t get product IDs. But, for those who know the product IDs (anyone with WC backend capabilities), it works just fine.Minor loop nesting is involved. I had considered looping the $_POST values, but it would require about the same number of lines. If you have a lot of dropdowns, looping may be preferable.
This does not take multi-select into account, since this school only allows the student to enroll in a single class per season.
// Create WC order from CF7 dropdowns $class_pick = array( // 5 student enrollment slots available per form $_POST['Dancer_1_Class'], // use $_POST instead of $posted_data to get raw dropdown data $_POST['Dancer_2_Class'], $_POST['Dancer_3_Class'], $_POST['Dancer_4_Class'], $_POST['Dancer_5_Class'] ); $classes = array( // 4 classes available to each dancer, 1 choice per dancer 'Class 1: $100' => 1001, // associate product id (value) with dropdown option (key) 'Class 2: $200' => 1002, 'Class 3: $300' => 1003, 'Class 4: $400' => 1004 ); $order = wc_create_order(); foreach ( $class_pick as $selection ) { // match class with dropdown selection foreach ( $classes as $class => $id ) { // get product id from $classes array if ( $selection == $class ) { $order->add_product( wc_get_product( $id ), 1 ); } } } $order->set_address( $address, 'billing' ); // insert billing array data which also dumps into new customer account meta (not shown) $order->calculate_totals(); $order->update_status("Completed", 'New User Created', true);
Some readers may be wondering why I didn’t nest a third loop to account for repeats (more than 1 student enrolling in the same class). Not doing so will make it easier for my subadmins to look at the order and know which line belongs with which dancer – e.g., if dancer 2 and dancer 4 are both enrolling in class 3, the order would show x2 for product 1003, but would not specify which of the 5 dancers that product belongs to, only that it wasn’t dancer 1. The subadmins will have these specifics via CF7 email and separate database to look at, but I may as well make it less confusing for them while they’re in the WC orders.
- The topic ‘snippet: working WooCommerce order generator’ is closed to new replies.