【问题标题】:Woocommerce - Create an order manually via codeWoocommerce - 通过代码手动创建订单
【发布时间】:2020-06-06 16:29:19
【问题描述】:

我想知道是否有人可以帮助我编写代码?

我想在提交重力表单时以编程方式在 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);


    $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", 'On Hold', TRUE); 

    $order->add_order_note( $note ); 
}

【问题讨论】:

    标签: php wordpress woocommerce hook-woocommerce gravityforms


    【解决方案1】:

    注意:变量 $prices 未在您的原始代码示例中设置

    一个例子

    // set some variables
    $user_id = 1;
    $product_id = 30;
    $quantity = 1;
    $price = 10;
    $note = 'my custom note';
    
    $product = wc_get_product($product_id);
    
    // Create the order object
    $order = wc_create_order();
    $order->add_product( $product, $quantity, $price);
    
    foreach ($order->get_items() as $item_key => $item ) {
        $item->add_meta_data( 'Label', 'Value', true );
    }
    
    $order->set_customer_id( $user_id );
    $order->set_address( $address, 'billing' );
    $order->calculate_totals();
    $order->update_status('on-hold', 'pending', TRUE); 
    
    $order->add_order_note( $note );
    

    所以在你的代码中,这部分

    $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 );
    

    变成这个

    $order = wc_create_order();
    $order->set_customer_id( $user_id );
    
    $order->add_product( wc_get_product($product_id), $quantity, $prices);
    
    foreach ($order->get_items() as $item_key => $item ) {
        $item->add_meta_data( 'Label', 'Value', true );
    }
    
    $order->set_address( $address, 'billing' );
    $order->calculate_totals();
    $order->update_status( 'on-hold', 'pending', TRUE); 
    
    $order->add_order_note( $note );
    

    【讨论】:

      猜你喜欢
      • 2014-07-28
      • 2012-07-15
      • 2018-09-27
      • 1970-01-01
      • 2021-10-27
      • 2013-04-20
      • 2019-09-10
      • 2013-11-02
      • 1970-01-01
      相关资源
      最近更新 更多