【问题标题】:Create a Woocommerce order programmatically and apply payment method [duplicate]以编程方式创建 Woocommerce 订单并应用付款方式 [重复]
【发布时间】:2020-06-07 16:59:39
【问题描述】:

我有一个在重力表单提交后创建 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


    // set some variables
$user_id =rgar( $entry, '97' );
$product_id = rgar( $entry, '71' );
$quantity = rgar( $entry, '73' );
$note = rgar( $entry, '53' );

$product = wc_get_product($product_id);

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

$payment_gateways=rgar( $entry, '106' );
// Create the order object
$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( 'Booking Request', $note, true );
}

$order->set_address( $address, 'billing' );
$order->calculate_totals();
$order->update_status( 'on-hold', 'pending', TRUE); 

$order->add_order_note( $note );

    $coupon_code = rgar( $entry, '105' );
    $order->apply_coupon($coupon_code);
      // Set payment gateway
    $order->set_payment_method($payment_gateways);
}``` 

【问题讨论】:

标签: php wordpress woocommerce


【解决方案1】:

试试这个

$payment_gateways = WC()->payment_gateways->payment_gateways();
$order->set_payment_method($payment_gateways['stripe']);

这是我的工作代码 谢谢

【讨论】:

  • 我希望能够在表单字段中选择付款方式。您的解决方案将其设置为 Stripe。
  • @JessicaArmstrong WC()->payment_gateways->payment_gateways(); 此方法将返回 wordpress 中可用支付网关的数组。所以只需创建一个函数并选择您的付款方式并放入 $order->set_payment_method() 这个方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-21
  • 2018-08-06
  • 1970-01-01
相关资源
最近更新 更多