I sell packages online in my store. When a customer orders 2 packages (1 package consists of 4 products), I want to create 2 orders with 1 package, instead of 1 order with 2 packages.
Example:
I have a package (ID: 212), which can consist of up to 4 bundled products (all ID: 730). The problem right now: When I try to split the order, only the product with ID 212 is put into a new order. The 4 products, which belong to ID 212 still stay in the same order.
Following a screenshot, which makes the hierarchy more clear:
What happens, when I try the solution from #Shir Gans:
The package is put into a new order, but the products, which should also be in the new order are still in the old order.
What is the easiest way to do this, and which hook do I have to use?
You can hook to woocommerce_payment_complete which accepts $order_id, then you can loop through the items and decide if to create another order. Then you can split the items into a new order. here is an (untested) example:
define('PACKAGE_PRODUCT_ID', 1010);
add_action('woocommerce_payment_complete', 'order_splitter', 100, 1);
function order_splitter($order_id){
$completed_order = new WC_Order($order_id);
$item_splitted = false;
$address = array(
'first_name' => $completed_order->get_billing_first_name(),
'last_name' => $completed_order->get_billing_last_name(),
'company' => '',
'email' => $completed_order->get_billing_email(),
'phone' => $completed_order->get_billing_phone(),
'address_1' => $completed_order->get_billing_address_1(),
'address_2' => $completed_order->get_billing_address_2(),
'city' => $completed_order->get_billing_city(),
'state' => $completed_order->get_billing_state(),
'postcode' => $completed_order->get_billing_postcode(),
'country' => $completed_order->get_billing_country()
);
foreach($completed_order->get_items() as $item){
if (!$item_splitted && $item->get_product_id() === PACKAGE_PRODUCT_ID) {
//create new order
$new_order_args = array(
'customer_id' => $completed_order->get_customer_id(),
'status' => 'wc-pending',
);
$new_order = wc_create_order($new_order_args);
$product_to_add = wc_get_product(PACKAGE_PRODUCT_ID);
$new_order->add_product($product_to_add, 1, array());
$new_order->set_address($address, 'billing');
$new_order->set_address($address, 'shipping');
$new_order->update_status('wc-processing');
$new_order->add_order_note('This order created automatically');
$new_order->save();
$completed_order->remove_item($item->get_id());
$item_splitted = true;
} else if ($item_splitted && $item['product_id'] === PACKAGE_PRODUCT_ID){
# This will ensure every 2 products are splitted (skipping the 2nd one)
$item_splitted = false;
continue;
}
}
}
Related
I have tried modifying the answer provided in //stackoverflow.com/a/59544491/19984414. I am not getting any errors when testing it, but also nothing really happens to my test order.
How can I test, why nothing happens when I make an order?
add_action( 'woocommerce_checkout_order_processed', 'action_woocommerce_checkout_order_processed', 10, 3 );
function action_woocommerce_checkout_order_processed( $order_id, $posted_data, $order ) {
// Initialize
$check_for_stock = false;
$taxonomy = 'product_tag';
// Loop through order items
foreach ( $order->get_items() as $item_key => $item ) {
// Get product
$product = $item->get_product();
// Product has tag 'stock'
if ( has_term( 'stock', $taxonomy, $product ) ) {
// Will only be executed once if the order contains line items with tag "stock"
if ( $check_for_stock == false ) {
$check_for_stock = true;
// Create new order with stock items
$stock_order = wc_create_order();
}
// Add product to 'backorder' order
$stock_order->add_product( $product, $item['quantity'] );
$stock_order->save();
// Delete item from original order
$order->remove_item( $item->get_id() );
}
}
// If current order contains line items with product tag "stock", retrieve the necessary data from the existing order and apply it in the new order
if ( $check_for_stock ) {
// Recalculate and save original order
$order->calculate_totals();
$order->save();
// Obtain necessary information
// Get address
$address = array(
'first_name' => $order->get_billing_first_name(),
'last_name' => $order->get_billing_last_name(),
'email' => $order->get_billing_email(),
'phone' => $order->get_billing_phone(),
'address_1' => $order->get_billing_address_1(),
'address_2' => $order->get_billing_address_2(),
'city' => $order->get_billing_city(),
'state' => $order->get_billing_state(),
'postcode' => $order->get_billing_postcode(),
'country' => $order->get_billing_country()
);
// Get shipping
$shipping = array(
'first_name' => $order->get_shipping_first_name(),
'last_name' => $order->get_shipping_last_name(),
'address_1' => $order->get_shipping_address_1(),
'address_2' => $order->get_shipping_address_2(),
'city' => $order->get_shipping_city(),
'state' => $order->get_shipping_state(),
'postcode' => $order->get_shipping_postcode(),
'country' => $order->get_shipping_country()
);
// Get order currency
$currency = $order->get_currency();
// Get order payment method
$payment_gateway = $order->get_payment_method();
// Required information has been obtained, assign it to the 'backorder' order
// Set address
$stock_order->set_address( $address, 'billing' );
$stock_order->set_address( $shipping, 'shipping' );
// Set the correct currency and payment gateway
$stock_order->set_currency( $currency );
$stock_order->set_payment_method( $payment_gateway );
// Calculate totals
$stock_order->calculate_totals();
// Set order note with original ID
$stock_order->add_order_note( 'Automated "stock" order. Created from the original order ID: ' . $order_id );
// Optional: give the new 'backorder' order the correct status
$stock_order->update_status( 'on-hold' );
$stock_order->save();
}
}
I am running the snippet from WPCodeBox with the following (default) settings:
How to run the snippet: Always (On Page Load)
Hook/Priority: Root (Default)
Snippet Order: 10
Where to run the snippet: Everywhere
So I've been going through a restructure build of an entire site, and part of that involved switching from Formidable Forms to Gravity Forms. We did this because we wanted to use the Nested Form feature, so that we could automate multiple attendees without having to create a new form for each.
Here's the problem - on our old site that did have a separate form per attendee via Formidable, we had a code using the Canvas API to send name + email info to Canvas and automatically register users for the online courses this company offers. In trying to convert sections of this code to work with my nested forms, I'm running into a snag:
The main issue is that the value is being spit out as all of the information from the nested form entry, not by name/ email/ etc.
The info is being spit out twice, perhaps because of the way the forms are structured? There are a couple calculations happening in the forms/ nested forms so I'm chalking it up to that.
[1] => WC_Meta_Data Object
(
[current_data:protected] => Array
(
[id] => 212
[key] => Attendee Registration
[value] =>
Name
Test Name
Email Address
courses#email.com
Cell Phone
(333) 333-3333
Would you like to receive text message reminders for this registration?
No
Post-class notification is required for the following states, please identify if you will be using this class to fulfill any state license requirements:
N/A
You'll receive a hard copy and digital certificate upon course completion. Additional options are available here:
All live classes include a hard copy manual and regulations. To join our effort to save paper, please also add any of the following options to take your books home:
)
[data:protected] => Array
(
[id] => 212
[key] => Attendee Registration
[value] =>
Name
Test Name
Email Address
courses#email.com
Cell Phone
(333) 333-3333
Would you like to receive text message reminders for this registration?
No
Post-class notification is required for the following states, please identify if you will be using this class to fulfill any state license requirements:
N/A
You'll receive a hard copy and digital certificate upon course completion. Additional options are available here:
All live classes include a hard copy manual and regulations. To join our effort to save paper, please also add any of the following options to take your books home:
)
)
Also: I was playing around with grabbing the ID of the main entry via [_gravity_form_linked_entry_id], and grabbing the nested info from that via [_gravity_form_lead].
The best I was able to get from that was this... so yeah kind of lost on how to progress here if anyone has any pointers! Thanks so much!
[data:protected] => Array
(
[id] => 211
[key] => _gravity_forms_history
[value] => Array
(
[_gravity_form_cart_item_key] => 72201a9586fb30895b8fb5cac2a796b9
[_gravity_form_linked_entry_id] => 125
[_gravity_form_lead] => Array
(
[form_id] => 1
[source_url] => https://chcv2.flywheelstaging.com/product/air-monitoring-specialist-live/
[ip] => 75.151.95.41
[42.1] => Course Price
[42.2] => $580.00
[42.3] => 1
[21] => 122
[40.1] => Add-On Fees
[40.2] => $0.00
[40.3] => 1
)
[_gravity_form_data] => Array
(
[id] => 1
[bulk_id] => 0
[display_title] =>
[display_description] =>
[disable_woocommerce_price] => no
[price_before] =>
[price_after] =>
[disable_calculations] => no
[disable_label_subtotal] => yes
[disable_label_options] => yes
[disable_label_total] => no
[disable_anchor] => no
[label_subtotal] => Course Fee
[label_options] => Additional Attendees + Selected Options
[label_total] => Attendee Registration + Add-Ons:
[use_ajax] => no
[enable_cart_edit] => no
[enable_cart_edit_remove] => no
[keep_cart_entries] => no
[send_notifications] => no
[enable_cart_quantity_management] => stock
[cart_quantity_field] =>
[update_payment_details] => yes
[display_totals_location] => after
[structured_data_override] => no
[structured_data_low_price] =>
[structured_data_high_price] =>
[structured_data_override_type] => overwrite
)
)
)
Update: Here's how I've incorporated the code from Rochelle's comment below, where I'm getting an error
add_action( 'woocommerce_thankyou', 'canvas_enroll', 20, 2 );
function canvas_enroll($orders) {
$query = new WC_Order_Query( array(
'orderby' => 'date',
'order' => 'DESC',
'return' => 'ids',
) );
$orders = $query->get_orders();
foreach($orders as $order){
foreach ($order->get_items() as $item_id => $item_data) {
if(isset($item_data->get_meta( '_gravity_forms_history')["_gravity_form_lead"]["form_id"]) && !empty($item_data->get_meta( '_gravity_forms_history')["_gravity_form_lead"]["form_id"])){
$linked_entry=$item_data->get_meta( '_gravity_forms_history')["_gravity_form_linked_entry_id"];
$entry_id = $linked_entry;
$entry = GFAPI::get_entry( $entry_id );//id of Parent Gravity Form
if(isset($item_data->get_meta( '_gravity_forms_history')["_gravity_form_lead"]['2']) && !empty($item_data->get_meta( '_gravity_forms_history')["_gravity_form_lead"]['2'])){
$linked_nested_value=$item_data->get_meta( '_gravity_forms_history')["_gravity_form_lead"]['2'];
$nested_value_array = preg_split ("/\,/", $linked_nested_value); //array of child entries
$child_entry_amt = substr_count($linked_nested_value, ",") + 1;
if ($child_entry_amt > 0){
for ($n = 0; $n < $child_entry_amt; $n++) {
$entry_id_nest[$n]=$nested_value_array[$n];
$entry_nest[$n] = GFAPI::get_entry( $entry_id_nest[$n] ); //nested form entry
$name[$n] = $entry_nest[$n]['12.3'].''.$entry_nest[$n]['12.6'];//replace 1.3 and 1.6 with nested field id of name
$email[$n] = $entry_nest[$n]['11']; //2 is the GF nested field id of email
}
}
}
}
}
}
}
Finally got this figured out! Something that was super helpful was to echo the item meta data ($value, in my case) for that to display all the ids and such, that's how I was able to figure out that I needed 21 in that ID for the child entries.
I'm not really sure why I had to switch to wc_get_order instead of wc_order_query, but it solved the errors I was getting.
function canvas_enroll($order_id) {
$order = wc_get_order( $order_id);
$order_id = array(
'orderby' => 'date',
'order' => 'DESC',
'return' => 'ids',
);
if(!empty($order) && isset($order)){
// Loop through order line items
foreach( $order->get_items() as $key => $value ){
// get order item data (in an unprotected array)
if(isset($value->get_meta( '_gravity_forms_history')["_gravity_form_lead"]["form_id"]) && !empty($value->get_meta( '_gravity_forms_history')["_gravity_form_lead"]["form_id"])){
$linked_entry=$value->get_meta( '_gravity_forms_history')["_gravity_form_linked_entry_id"];
$entry_id = $linked_entry;
$entry = GFAPI::get_entry( $entry_id );//id of Parent Gravity Form
if(isset($value->get_meta( '_gravity_forms_history')["_gravity_form_lead"]['21']) && !empty($value->get_meta( '_gravity_forms_history')["_gravity_form_lead"]['21'])) { //21 was the id for my child form
$linked_nested_value = $value->get_meta( '_gravity_forms_history')["_gravity_form_lead"]['21'];
$nested_value_array = preg_split ("/\,/", $linked_nested_value); //array of child entries
$child_entry_amt = substr_count($linked_nested_value, ",") + 1;
if ($child_entry_amt > 0){
for ($n = 0; $n < $child_entry_amt; $n++) {
$entry_id_nest[$n]=$nested_value_array[$n];
$entry_nest[$n] = GFAPI::get_entry( $entry_id_nest[$n] ); //nested form entry
$firstname[$n] = $entry_nest[$n]['12.3'];//replace 12.3 with nested field id of first name
$lastname[$n] = $entry_nest[$n]['12.6'];//replace 12.6 with nested field id of last name
$email[$n] = $entry_nest[$n]['11']; //replace 11 with nested field id of email
}
}
}
}
}
}
}
I'm just going to paste the code I used for another project that I needed to pull the same type of data in case it puts you on the right track. You'd have to replace the numbers with the ids from your forms:
$query = new WC_Order_Query( array(
'orderby' => 'date',
'order' => 'DESC',
'return' => 'ids',
) );
$orders = $query->get_orders();
foreach($orders as $order){
foreach ($order->get_items() as $item_id => $item_data) {
if(isset($item_data->get_meta( '_gravity_forms_history')["_gravity_form_lead"]["form_id"]) && !empty($item_data->get_meta( '_gravity_forms_history')["_gravity_form_lead"]["form_id"])){
$linked_entry=$item_data->get_meta( '_gravity_forms_history')["_gravity_form_linked_entry_id"];
$entry_id = $linked_entry;
$entry = GFAPI::get_entry( $entry_id );//id of Parent Gravity Form
if(isset($item_data->get_meta( '_gravity_forms_history')["_gravity_form_lead"]['2']) && !empty($item_data->get_meta( '_gravity_forms_history')["_gravity_form_lead"]['2'])){
$linked_nested_value=$item_data->get_meta( '_gravity_forms_history')["_gravity_form_lead"]['2'];
$nested_value_array = preg_split ("/\,/", $linked_nested_value); //array of child entries
$child_entry_amt = substr_count($linked_nested_value, ",") + 1;
if ($child_entry_amt > 0){
for ($n = 0; $n < $child_entry_amt; $n++) {
$entry_id_nest[$n]=$nested_value_array[$n];
$entry_nest[$n] = GFAPI::get_entry( $entry_id_nest[$n] ); //nested form entry
$name[$n] = $entry_nest[$n]['1.3'].''.$entry_nest[$n]['1.6'];//replace 1.3 and 1.6 with nested field id of name
$email[$n] = $entry_nest[$n]['2']; //2 is the GF nested field id of email
}
}
}
}
}
}
Ok. Try this first to see if it changes anything:
$query = new WC_Order_Query( array(
'orderby' => 'date',
'order' => 'DESC',
'return' => 'ids',
) );
$orders = $query->get_orders();
foreach($orders as $order){
if(!empty($order) && isset($order)){
foreach ($order->get_items() as $item_id => $item_data) {
if(isset($item_data->get_meta( '_gravity_forms_history')["_gravity_form_lead"]["form_id"]) && !empty($item_data->get_meta( '_gravity_forms_history')["_gravity_form_lead"]["form_id"])){
$linked_entry=$item_data->get_meta( '_gravity_forms_history')["_gravity_form_linked_entry_id"];
$entry_id = $linked_entry;
$entry = GFAPI::get_entry( $entry_id );//id of Parent Gravity Form
if(isset($item_data->get_meta( '_gravity_forms_history')["_gravity_form_lead"]['2']) && !empty($item_data->get_meta( '_gravity_forms_history')["_gravity_form_lead"]['2'])){
$linked_nested_value=$item_data->get_meta( '_gravity_forms_history')["_gravity_form_lead"]['2'];
$nested_value_array = preg_split ("/\,/", $linked_nested_value); //array of child entries
$child_entry_amt = substr_count($linked_nested_value, ",") + 1;
if ($child_entry_amt > 0){
for ($n = 0; $n < $child_entry_amt; $n++) {
$entry_id_nest[$n]=$nested_value_array[$n];
$entry_nest[$n] = GFAPI::get_entry( $entry_id_nest[$n] ); //nested form entry
$name[$n] = $entry_nest[$n]['1.3'].''.$entry_nest[$n]['1.6'];//replace 1.3 and 1.6 with nested field id of name
$email[$n] = $entry_nest[$n]['2']; //2 is the GF nested field id of email
}
}
}
}
}
}
}
I'm getting a bunch of paginated variable products using wc_get_products(). And I would like to only get products in a given price-range. The problem is that there are a lot of products, so only 20 products are fetched at a time.
According to the documentation, there are no meta_query()-option when using wc_get_products().
So how do I get this?
Here is my current query:
$products_query = [
'category' => [ get_queried_object()->slug ],
'limit' => 20,
'paginate' => true,
'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1,
];
$products = wc_get_products( $products_query );
Now remember, that it's something that needs to be implemented before the products are queried from the database.
Let's say that I have 100 products total. Since the products are paginated, then if I have ordered the products after _price (ASC), then page 1 should return the 20 cheapest products. And page 3 should return the products 40-60 (after they have been sorted).
Solution attempts
Attempt 1 - Try using a meta_query anyways
I figured that wc_get_products was inspired and built on top of WP_Query, so I tried this:
$products_query = [
'category' => [ get_queried_object()->slug ],
'limit' => 20,
'paginate' => true,
'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1,
'meta_query' => [
'relation' => 'AND',
[
'key' => '_price',
'value' => 100,
'compare' => '<=',
'type' => 'NUMERIC'
]
]
];
$products = wc_get_products( $products_query );
It just ignores the meta_query-part and returns a result as if it wasn't there.
Attempt 2 - Using WP_Query
Everywhere I look, there are arrows pointing towards wc_get_products and away from wp_query.
So I haven't pursued this.
Attempt 3 - Raw SQL
One solution would be to make a raw SQL-query. But since the products are variable products, then the SQL-query would be quite a few joins, since it should first find all products - and then find all variations. And then sort the products after the lowest variation-price (if the price-sort is ascending) - and after the highest variation-price (if the price-sort is descending). It's obviously possible, but I was hoping to find a more WordPress-like solution. And I'm not good at SQL.
Attempt 4 - Using a plugin
I look around a bit - and sort'n'filter-plugins for WooCommerce are monsters, since they slab scripts and styles left, right and center. So this seems like a poor solution.
Attempt 5 - Using WooCommerce API
I can see that min_price and max_price are mentioned under List all products. So this might actually work.
Variable products are something complicated to filter by priceā¦ So the following is not perfect, but show you a way to enable a price range in WC_Product_Query.
So the following function will enable a custom price range query on WC_Product_Query:
add_filter( 'woocommerce_product_data_store_cpt_get_products_query', 'handle_price_range_query_var', 10, 2 );
function handle_price_range_query_var( $query, $query_vars ) {
if ( ! empty( $query_vars['price_range'] ) ) {
$price_range = explode( '|', esc_attr($query_vars['price_range']) );
if ( is_array($price_range) && count($price_range) == 2 ) {
$query['meta_query']['relation'] = 'AND';
$query['meta_query'][] = array(
'key' => '_price',
'value' => reset($price_range), // From price value
'compare' => '>=',
'type' => 'NUMERIC'
);
$query['meta_query'][] = array(
'key' => '_price',
'value' => end($price_range), // To price value
'compare' => '<=',
'type' => 'NUMERIC'
);
$query['orderby'] = 'meta_value_num'; // sort by price
$query['order'] = 'ASC'; // In ascending order
}
}
return $query;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
EXAMPLE USAGE
Handle float numbers.
The 1st price is separated from the 2nd one by a pipe |
Here we query products from $10.25 to $50 (price range):
$products = wc_get_products( [
'limit' => 20,
'status' => 'publish',
'price_range' => '10.25|50', // The first price is separated from the 2nd one with a pipe
] );
echo '<ul>';
foreach( $products as $product ) {
echo '<li>'.$product->get_name().' '.$product->get_price_html().'</li>';
}
echo '</ul>';
To filter only variable products, you can add the following line to the query:
'type' => 'variable',
Documentation: wc_get_products and WC_Product_Query
function handle_custom_query($query, $query_vars)
{
if (!empty($query_vars['meta_query'])) {
foreach ($query_vars['meta_query'] as $q) {
$query['meta_query'][] = $q;
}
}
return $query;
}
add_filter('woocommerce_product_data_store_cpt_get_products_query', 'handle_custom_query', 10, 3);
usage:
$query = new \WC_Product_Query(
array(
// ...standard queries
// https://github.com/woocommerce/woocommerce/wiki/wc_get_products-and-WC_Product_Query
'meta_query' => array(
// ...array of meta queries
),
)
);
$products = $query->get_products();
I am doing migration from very old custom made ecommerce site that is working as subscription model. I have 3 different products that contains custom data to is available to customers that have purchased to product.
So i am importing customers by their email address and i need to add products to their purchase / order history so the can get their hands on into custom data.
So how to link a product to customer?
I'm not so sure about those custom fields but it seem like what to need is to create orders programmatically, where you "link" existing users and existing products.
Luckily, WooCommerce allow us to do that :)
take a look at this code:
function create_order() {
// Create product
$product = WC_Helper_Product::create_simple_product();
WC_Helper_Shipping::create_simple_flat_rate();
$order_data = array('status' => 'pending', 'customer_id' => 1, 'customer_note' => '', 'total' => '');
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
// Required, else wc_create_order throws an exception
$order = wc_create_order($order_data);
// Add order products
$item_id = $order->add_product($product, 4);
// Set billing address
$billing_address = array('country' => 'US', 'first_name' => 'Jeroen', 'last_name' => 'Sormani', 'company' => 'WooCompany', 'address_1' => 'WooAddress', 'address_2' => '', 'postcode' => '123456', 'city' => 'WooCity', 'state' => 'NY', 'email' => 'admin#example.org', 'phone' => '555-32123');
$order->set_address($billing_address, 'billing');
// Add shipping costs
$shipping_taxes = WC_Tax::calc_shipping_tax('10', WC_Tax::get_shipping_tax_rates());
$order->add_shipping(new WC_Shipping_Rate('flat_rate_shipping', 'Flat rate shipping', '10', $shipping_taxes, 'flat_rate'));
// Set payment gateway
$payment_gateways = WC()->payment_gateways->payment_gateways();
$order->set_payment_method($payment_gateways['bacs']);
// Set totals
$order->set_total(10, 'shipping');
$order->set_total(0, 'cart_discount');
$order->set_total(0, 'cart_discount_tax');
$order->set_total(0, 'tax');
$order->set_total(0, 'shipping_tax');
$order->set_total(40, 'total');
// 4 x $10 simple helper product
return wc_get_order($order->id);
}
at the 5th line, you set the customer ID
and, at the 12th line, you assign the product to the order.
caveat: I'm not sure how much of this snippet is required by wc_create_order(), as the official documentation is very poor.
I'd try to run this function as clean as possible, just using the minimum information (or the information that you have available).
Something like this:
function create_order() {
// Create product
$product = WC_Helper_Product::create_simple_product();
$order_data = array('status' => 'pending', 'customer_id' => 1, 'customer_note' => '', 'total' => '');
// Required, else wc_create_order throws an exception
$order = wc_create_order($order_data);
// Add order products
$item_id = $order->add_product($product, 4);
// Set totals
//$order->set_total(40, 'total');
return wc_get_order($order->id);
}
good luck!
I am working on a Drupal site that needs to have a 'careers' page. I have a list of twenty or so jobs, and 30 or so locations where these jobs may be available.
What I am looking to do is make it so, when a job becomes available, all that needs to be done is the user selects the job title and the location where it is available and it will create the posting with the job description and other info I have along with the info for that location.
Another hurtle I am running into is making it so I can have multiple instances... ex. If the same job is available at two or more locations.
I have been trying to wrap my mind around how I am going to make this work and am coming up blank. If anyone has an idea to point me in the right direction, it would be appreciated.
Sounds like a pretty common use case; if it was me I'd approach it like this:
Create a 'Job' content type
Add a new 'Location' Vocabulary
Add a term reference field on the 'Job' content type to the 'Location' vocabulary, with unlimited values (or the maximum no. of locations you want to allow per job).
Create a custom form for your admins, something like:
function MYMODULE_add_job_form($form, &$form_state) {
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('Title'),
'#maxlength' => 255,
'#required' => TRUE
);
// Load the vocabulary (the machine name might be different).
$vocabulary = taxonomy_vocabulary_machine_name_load('location');
// Get the terms
$terms = taxonomy_get_tree($vocabulary->vid);
// Extract the top level terms for the select options
$options = array();
foreach ($terms as $term) {
$options[$term->tid] = $term->name;
}
$form['locations'] = array(
'#type' => 'select',
'#title' => t('Locations'),
'#options' => $options,
'#multiple' => TRUE,
'#required' => TRUE
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Add job')
);
return $form;
}
Create a custom submit handler for the form to add the new node programatically:
function MYMODULE_add_job_form_submit($form, &$form_state) {
$location_tids = array_filter($form_state['values']['locations']);
$node = new stdClass;
$node->type = 'job';
$node->language = LANGUAGE_NONE;
node_object_prepare($node);
$node->title = $form_state['values']['title'];
$node->field_location_term_ref[LANGUAGE_NONE] = array();
foreach ($location_tids as $tid) {
$node->field_location_term_ref[LANGUAGE_NONE][] = array(
'tid' => $tid
);
}
node_save($node);
$form_state['redirect'] = "node/$node->nid";
}
You'll need to add a page callback for that form obviously, and there'll likely be some small changes you'll need to make (names of fields etc), but it should give you a good starting point. You'll also need to load the location taxonomy terms at some point to extract the description info you mentioned...you can use taxonomy_term_load() to do that.