如何让客户在woocommerce上编辑订单

时间:2018-03-24 16:54:28

标签: php wordpress woocommerce

我的客户经常需要更改订单的详细信息。 他们需要: - 修改日期/小时和交货地址 - 修改数量 - 修改那些卡的产品。 我需要让订单可编辑或让我的客户保存卡片(每个客户10到20张卡片)。 我找了一个允许我这样做的插头,但我没有找到任何东西。 我不是开发人员,我是PHP的新手。 谢谢你的帮助!

1 个答案:

答案 0 :(得分:-1)

您可以使用此解决方案-https://businessbloomer.com/woocommerce-edit-orders/。它对我有用!

/**
 * @snippet       Edit Order Functionality @ WooCommerce My Account Page
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=91893
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.5.3
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

// ----------------
// 1. Allow Order Again for Processing Status

add_filter( 'woocommerce_valid_order_statuses_for_order_again', 'bbloomer_order_again_statuses' );

function bbloomer_order_again_statuses( $statuses ) {
    $statuses[] = 'processing';
    return $statuses;
}

// ----------------
// 2. Add Order Actions @ My Account

add_filter( 'woocommerce_my_account_my_orders_actions', 'bbloomer_add_edit_order_my_account_orders_actions', 50, 2 );

function bbloomer_add_edit_order_my_account_orders_actions( $actions, $order ) {
    if ( $order->has_status( 'processing' ) ) {
        $actions['edit-order'] = array(
            'url'  => wp_nonce_url( add_query_arg( array( 'order_again' => $order->get_id(), 'edit_order' => $order->get_id() ) ), 'woocommerce-order_again' ),
            'name' => __( 'Edit Order', 'woocommerce' )
        );
    }
    return $actions;
}

// ----------------
// 3. Detect Edit Order Action and Store in Session

add_action( 'woocommerce_cart_loaded_from_session', 'bbloomer_detect_edit_order' );

function bbloomer_detect_edit_order( $cart ) {
    if ( isset( $_GET['edit_order'] ) ) WC()->session->set( 'edit_order', absint( $_GET['edit_order'] ) );
}

// ----------------
// 4. Display Cart Notice re: Edited Order

add_action( 'woocommerce_before_cart', 'bbloomer_show_me_session' );

function bbloomer_show_me_session() {
    if ( ! is_cart() ) return;
    $edited = WC()->session->get('edit_order');
    if ( ! empty( $edited ) ) {
        $order = new WC_Order( $edited );
        $credit = $order->get_total();
        wc_print_notice( 'A credit of ' . wc_price($credit) . ' has been applied to this new order. Feel free to add products to it or change other details such as delivery date.', 'notice' );
    }
}

// ----------------
// 5. Calculate New Total if Edited Order

add_action( 'woocommerce_cart_calculate_fees', 'bbloomer_use_edit_order_total', 20, 1 );

function bbloomer_use_edit_order_total( $cart ) {

  if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

  $edited = WC()->session->get('edit_order');
  if ( ! empty( $edited ) ) {
      $order = new WC_Order( $edited );
      $credit = -1 * $order->get_total();
      $cart->add_fee( 'Credit', $credit );
  }

}

// ----------------
// 6. Save Order Action if New Order is Placed

add_action( 'woocommerce_checkout_update_order_meta', 'bbloomer_save_edit_order' );

function bbloomer_save_edit_order( $order_id ) {
    $edited = WC()->session->get('edit_order');
    if ( ! empty( $edited ) ) {
        // update this new order
        update_post_meta( $order_id, '_edit_order', $edited );
        $neworder = new WC_Order( $order_id );
        $oldorder_edit = get_edit_post_link( $edited );
        $neworder->add_order_note( 'Order placed after editing. Old order number: <a href="' . $oldorder_edit . '">' . $edited . '</a>' );
        // cancel previous order
        $oldorder = new WC_Order( $edited );
        $neworder_edit = get_edit_post_link( $order_id );
        $oldorder->update_status( 'cancelled', 'Order cancelled after editing. New order number: <a href="' . $neworder_edit . '">' . $order_id . '</a> -' );
    }
}