使用产品ID WooCommerce手动创建订单

时间:2020-02-22 作者:Jessica Gronow

我正在编写一个代码,从重力表单提交中手动创建woocommerce订单。

我有“自定义注释”,我已经设法将其添加到订单注释中,但理想情况下,它将位于产品/项目行下方的元框中。

如果您有任何建议,我们将不胜感激(我是自学成才,不是专家)。

这是我的代码:

add_action( \'gform_after_submission_56\', \'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
    $user_id =rgar( $entry, \'97\' );
    $note = rgar( $entry, \'53\' );

    $product_id = rgar( $entry, \'71\' );
    $quantity = rgar( $entry, \'73\' );


    $address = array(

         \'first_name\' => rgar( $entry, \'98\' ),
         \'last_name\'  => rgar( $entry, \'99\' ),
         \'company\'    => rgar( $entry, \'\' ),
         \'email\'      => rgar( $entry, \'83\' ),
         \'phone\'      => rgar( $entry, \'84\' ),
         \'address_1\'  => rgar( $entry, \'88.1\' ),
         \'address_2\'  => rgar( $entry, \'88.2\' ),
         \'city\'       => rgar( $entry, \'88.3\' ),
         \'state\'      => rgar( $entry, \'88.4\' ),
         \'postcode\'   => rgar( $entry, \'88.5\' ),
         \'country\'    => rgar( $entry, \'88.6\' ),

    );



    $order = wc_create_order();
    $order->set_customer_id( $user_id );

    $order->add_product( wc_get_product($product_id), $quantity, $prices); 
    $order->set_address( $address, \'billing\' );
    $order->calculate_totals();
    $order->update_status("on hold", \'pending\', TRUE); 

    $order->add_order_note( $note );
}

1 个回复
SO网友:Abdul Awal Uzzal

我认为您可以使用以下选项:

$order->update_meta_data( \'YOUR_META_KEY_HERE\', $note );
记住更换YOUR_META_KEY_HERE 使用首选密钥,完成代码:

add_action( \'gform_after_submission_56\', \'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
    $user_id =rgar( $entry, \'97\' );
    $note = rgar( $entry, \'53\' );

    $product_id = rgar( $entry, \'71\' );
    $quantity = rgar( $entry, \'73\' );


    $address = array(

         \'first_name\' => rgar( $entry, \'98\' ),
         \'last_name\'  => rgar( $entry, \'99\' ),
         \'company\'    => rgar( $entry, \'\' ),
         \'email\'      => rgar( $entry, \'83\' ),
         \'phone\'      => rgar( $entry, \'84\' ),
         \'address_1\'  => rgar( $entry, \'88.1\' ),
         \'address_2\'  => rgar( $entry, \'88.2\' ),
         \'city\'       => rgar( $entry, \'88.3\' ),
         \'state\'      => rgar( $entry, \'88.4\' ),
         \'postcode\'   => rgar( $entry, \'88.5\' ),
         \'country\'    => rgar( $entry, \'88.6\' ),

    );



    $order = wc_create_order();
    $order->set_customer_id( $user_id );

    $order->add_product( wc_get_product($product_id), $quantity, $prices); 
    $order->set_address( $address, \'billing\' );
    $order->calculate_totals();
    $order->update_status("on hold", \'pending\', TRUE);

    $order->update_meta_data( \'YOUR_META_KEY_HERE\', $note );

}
谢谢你

相关推荐