Woocommerce-以编程方式创建订单并应用优惠券

问题描述 投票:0回答:1

我有一个代码,在您的才华横溢的帮助下,我设法使代码工作。

根据重力形式提交,现在创建了新订单。

我需要做的最后一件事是将优惠券代码应用于订单。谁能告诉我我需要什么代码段?


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 ); 
woocommerce gravity
1个回答
0
投票

如何使用WC_Abstract_Order :: apply_coupon?

/**
 * Apply a coupon to the order and recalculate totals.
 *
 * @since 3.2.0
 * @param string|WC_Coupon $raw_coupon Coupon code or object.
 * @return true|WP_Error True if applied, error if not.
 */
public function apply_coupon( $raw_coupon )

您的代码

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

    $coupon_code = 'kortingsbon';
    $order->apply_coupon($coupon_code);
}
© www.soinside.com 2019 - 2024. All rights reserved.