$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>woocommerce offtopic - How do I use a combination of switch_to_blog() and wc_create_order()?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

woocommerce offtopic - How do I use a combination of switch_to_blog() and wc_create_order()?

matteradmin7PV0评论

I'm looking for a way to create a WC order programmatically for a subsite in my multisite environment.

When I use the following, the order is created on the main site and not the subsite with ID 7:

switch_to_blog( 7 );

// Now we create the order
$bestelling = wc_create_order();

// Get orderid from order-to-be-made
$neworderid = trim( str_replace( '#', '', $bestelling->get_order_number() ) );

// Get product data
$occurences = $_POST['productids'];

foreach($occurences as $key => $value) {
  // Add products
  $product = wc_get_product($key);
  $bestelling->add_product($product, $value);
}

// Set addresses
$bestelling->set_address( $billingaddress, 'billing' );
$bestelling->set_address( $shippingaddress, 'shipping' );

// Set payment gateway
$payment_gateways = WC()->payment_gateways->payment_gateways();
$bestelling->set_payment_method( $payment_gateways['bacs'] );

$bestelling->set_customer_note( $_POST['customer_message'] );

// Calculate totals
$bestelling->calculate_totals();

// Give Revamp status
$bestelling->update_status('revamped', 'This order has been made with the revamp tool.', TRUE);

do_action( 'woocommerce_checkout_order_processed', $neworderid );

restore_current_blog();

Can it be done this way or should I look at this from another angle? Thanks in advance :).

Post a comment

comment list (0)

  1. No comments so far