如何解决“在Wordpress中处理帖子并获取请求”(未填写表单输入)

时间:2019-06-05 07:04:15

标签: wordpress woocommerce custom-wordpress-pages

您好,stackoverflow程序员,我创建了一个woocommerce函数,该函数创建了一个下标。变量$ address首先具有固定数据,我现在想接收它。客户必须填写表格,并且一旦单击提交按钮,就必须提供这些数据(输入)。我创建了一个表单,并调用该操作的函数。重新加载页面后,它将立即执行该功能,而无需单击提交按钮或输入任何数据。有人可以根据我编写的代码向我解释我做错了什么吗?

FORM:

      <div class="col-md-10">
                    <div class="head">
                        <h2 style="font-size: 20px; color: #FFFFFF; padding: 0; margin: 0; line-height: 1;">Je gegevens</h2>
                        <h2 style="font-size: 55px; font-weight: 700; letter-spacing: -1px; color: #FFFFFF;">Vertel ons wie je bent</h2>
                    </div>
                    <div class="content">
                        <form action="<?php create_new_order() ?>" method="POST"">
                            <div class="row form-subscription">
                                <div class="form-group col-sm-6">
                                    <div class="col-12" style="padding-left: 0px;">
                                        <label for="voornaam" class="control-label">Voornaam</label>
                                        <div>
                                            <input class="form-control" id="voornaam" />
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group col-sm-6">
                                    <div class="col-12" style="padding-left: 0px;">
                                        <label for="achternaam" class="control-label">Achternaam</label>
                                        <div>
                                            <input class="form-control" id="achternaam" />
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group col-sm-6">
                                    <div class="col-12" style="padding-left: 0px;">
                                        <label for="email" class="control-label">E-Mail</label>
                                        <div>
                                            <input class="form-control" id="email" />
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group col-sm-6">
                                    <div class="col-12" style="padding-left: 0px;">
                                        <label for="telefoonnummer" class="control-label">Telefoonnummer</label>
                                        <div>
                                            <input class="form-control" id="telefoonnummer" />
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group col-sm-6">
                                    <div class="col-12" style="padding-left: 0px;">
                                        <label for="adres_1" class="control-label">Straat, huisnummer (toevoeging)</label>
                                        <div>
                                            <input class="form-control" id="adres_1" />
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group col-sm-6">
                                    <div class="col-12" style="padding-left: 0px;">
                                        <label for="plaats" class="control-label">Plaats</label>
                                        <div>
                                            <input class="form-control" id="plaats" />
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group col-sm-6">
                                    <div class="col-12" style="padding-left: 0px;">
                                        <label for="postcode" class="control-label">Postcode</label>
                                        <div>
                                            <input class="form-control" id="postcode" />
                                        </div>
                                    </div>
                                </div>
                                <input type="submit" value="Submit">
                            </div>
                        </form>
                    </div>
                </div>

我实现的Wordpress功能:

add_action('woocommerce_checkout_process', 'create_new_order');

function create_new_order() {
    $product_sku = 'wpoh-prof-webshop';

    $address = array(
        'first_name' => $_REQUEST['voornaam'],
        'last_name'  => $_REQUEST['achternaam'],
        'email'      => $_REQUEST['email'],
        'phone'      => $_REQUEST['telefoonnummer'],
        'address_1'  => $_REQUEST['adres_1'],
        'address_2'  => '',
        'city'       => $_REQUEST['plaats'],
        'postcode'   => $_REQUEST['postcode'],
    );

    // Create the user from the input variables // Generate a automatic password sended to email address.
    $email            = $address['email'];
    $default_password = wp_generate_password();

    $user = get_user_by( 'login', $email );
    if ( ! $user ) {
        wp_create_user( $email, $default_password, $email );
        $user = get_user_by( 'login', $email );
    }

    // Create a WC_Order object and save it.
    $order = wc_create_order();

    // Set customer billing adress
    $order->set_address( $address, 'billing' );
    $product = wc_get_product( wc_get_product_id_by_sku( $product_sku ) );

    // Check if the product exists
    if ( $product === 0 ) {
        // CANCEL ORDER HERE: PRODUCT NOT FOUND
    }
    // Add an order line item
    $order->add_product( $product, 1 );

    // Set payment gateway
    $payment_gateways = WC()->payment_gateways->payment_gateways();
    $order->set_payment_method( $payment_gateways['mollie_wc_gateway_ideal'] );


    // Update order taxes and totals
    $order->calculate_totals();

    // Set order status and save
    $order->update_status( 'completed', 'In Store ', true );
    $data         = [
        'order_id'         => $order->get_id(),
        'billing_period'   => 'month',
        'billing_interval' => 1,
        'customer_id'      => $user->ID
    ];
    $subscription = wcs_create_subscription( $data );
    $subscription->add_product( $product, 1 );
    $subscription->calculate_totals();

    // Changing manual to auto renewall subscription
    update_post_meta( $subscription->get_id(), '_requires_manual_renewal', 'false' );
    update_post_meta( $subscription->get_id(), '_payment_method', 'mollie_wc_gateway_ideal' );
    update_post_meta( $subscription->get_id(), '_payment_method_title', 'iDEAL' );

    WC_Subscriptions_Manager::activate_subscriptions_for_order( $order );

}

0 个答案:

没有答案