我对重力形式和Woocommerce有一个有趣而复杂的任务。
在我们的网站上,我们为每位注册的新客户提供免费样品。我们尝试建立类似的东西,只要新用户填写他们的详细信息,如电子邮件,姓名和地址,以及他们想要获取和提交的免费样本,它应该在Woocommerce中创建一个订单。是否有可能使用重力形式或根本不可能?
我试过自己,但它并没有为我工作。
答案 0 :(得分:2)
It's possible, but there are a lot of variables that need to be taken into consideration, so it's really hard to help without you doing an attempt first.
This will only show you where to start, you need to figure out the rest:
add_action( 'gform_after_submission', 'post_to_third_party', 10, 2 );
function post_to_third_party( $entry, $form ) {
global $woocommerce;
// use this to find out $entry output
var_dump($entry);
// Make sure to add hidden field somewhere in the form with product id and define it here, If you have some other way of defining products in the form you need to make sure product id is returned in the variable below somehow
$product_id = rgar( $entry, '12' );
$address = array(
'first_name' => rgar( $entry, '5' ),
'last_name' => rgar( $entry, '2' ),
'company' => rgar( $entry, '3' ),
'email' => rgar( $entry, '4' ),
'phone' => rgar( $entry, '5' ),
'address_1' => rgar( $entry, '6' ),
'address_2' => rgar( $entry, '7' ),
'city' => rgar( $entry, '8' ),
'state' => rgar( $entry, '9' ),
'postcode' => rgar( $entry, '10' ),
'country' => rgar( $entry, '11' ),
);
$prices = array( 'totals' => array(
'subtotal' => 0,
'total' => 0,
) );
$order = wc_create_order();
$order->add_product( wc_get_product($product_id), 1, $prices);
$order->set_address( $address, 'billing' );
$order->calculate_totals();
$order->update_status("processing", 'Sample Order', TRUE);
}