【问题标题】:Quick buy button on products and popup quick buy form in woocommerce?产品上的快速购买按钮和 woocommerce 中的弹出快速购买表单?
【发布时间】:2017-05-11 13:21:13
【问题描述】:

您好,我不是一个优秀的开发人员,我刚刚从我自己学到了我一直在搜索链接(http://www.ourstore.com.pk/)中的快速购买 woocommerce,但如果有办法创建它,找不到购买或制作它的方法?请帮帮我!

【问题讨论】:

  • 如果您理解我所说的,请以正确的提问方式回复我!

标签: wordpress plugins jquery-plugins woocommerce


【解决方案1】:

我只用电话来创建订单。但是您可以添加任何字段。 页面上的表格

<form action="" method="post" id="buy_click_form">
    <input type="hidden" value="buy_click" name="action">
    <input type="hidden" value="<?php the_ID(); ?>" name="product_id">
    <input type="tel" value="" name="phone" class="one_click_tel" placeholder="+18 0__ ___ __ __" onFocus="if(this.value=='') this.value='+180';" >
    <button type="submit" class="buy_click" >Buy</button>
</form>

JS代码

$('#buy_click_form').submit(function () {
            $('.loader').fadeIn(200);
        var id = $(this).data('id');
        var ob = $('.one_click_tel');
            ob.removeClass('error');
        var phone = ob.val().replace(/[^\d]+/g, '');

        if( phone.match(/^180([\d]{9})$/g) ){
            $.ajax({
                url:ajaxurl,
                data: $('#buy_click_form input, #buy_click_form select, .variation_id'),
                dataType: 'json',
                type: "POST",
                success:function(data){
                    console.log(data);
                    if( data['redirect'] ){
                        window.location = data['redirect'];
                    }
                    $('.buy_click').after(data.message);
                    $('.loader').fadeOut(200);
                }
            });
        } else {
            ob.addClass('error');
        }
    return false;
});

通过 ajax 提交表单并在函数中添加钩子,如下所示:

add_action("wp_ajax_buy_click", "purchace_one_click");
add_action("wp_ajax_nopriv_buy_click", "purchace_one_click");

function purchace_one_click(){

    $address = array(
            'phone'      => $_POST['phone'],
        );

    // $order = wc_get_order(385);
    $order = wc_create_order(array('customer_id'=>get_current_user_id()));
    // print_r($order);
    update_post_meta( $order->get_id(), '_billing_phone', $_POST['phone']);
    $order->set_customer_user_agent( wc_get_user_agent() );
    $order->set_customer_note('Order by one click');
    $order->set_address( $address, 'billing' );

    if( isset($_POST['product_id']) && $_POST['product_id'] ){

        $id = ($_POST['variation_id']) ? $_POST['variation_id'] : $_POST['product_id'];
        $order->add_product( get_product( $id ), 1 ); //(get_product with id and next is for quantity)
        $order->calculate_totals();

    } else {
        $cart = WC()->cart;
        //print_r($cart);
        $cart_hash = md5( json_encode( wc_clean( $cart->get_cart_for_session() ) ) . $cart->total );
        $order->set_cart_hash( $cart_hash );
        WC_Checkout::create_order_line_items( $order, $cart );
        $order->set_total( $cart->total );

        $order->set_shipping_total( $cart->shipping_total );
        $order->set_discount_total( $cart->get_cart_discount_total() );
        $order->set_discount_tax( $cart->get_cart_discount_tax_total() );
        $order->set_cart_tax( $cart->tax_total );
        $order->set_shipping_tax( $cart->shipping_tax_total );
        WC_Checkout::create_order_fee_lines( $order, $cart );
        WC_Checkout::create_order_shipping_lines( $order, WC()->session->get( 'chosen_shipping_methods' ), WC()->shipping->get_packages() );
        WC_Checkout::create_order_tax_lines( $order, $cart );
        WC_Checkout::create_order_coupon_lines( $order, $cart );
        $cart->empty_cart();
        $return_url = wc_get_endpoint_url( 'order-received', $order->get_id(), wc_get_page_permalink( 'checkout' ) );
        // $return_url = $order->get_checkout_order_received_url();
        $return_url = add_query_arg( 'key', $order->get_order_key(), $return_url );
        $json['redirect'] = $return_url ;
    }

    $order->save();

    WC()->payment_gateways();
    WC()->shipping();

    // Load mailer.
    $mailer = WC()->mailer();
    $email_to_send = 'new_order';
    $mails = $mailer->get_emails();

    if ( ! empty( $mails ) ) {
        foreach ( $mails as $mail ) {
            if ( $mail->id == $email_to_send ) {
                $mail->trigger( $order->get_id(), $order );
                /* translators: %s: email title */
                $order->add_order_note( sprintf( __( '%s email notification manually sent.', 'woocommerce' ), $mail->title ), false, true );
            }
        }
    }
    $json['message'] = __('<p>Thank you fot order! We call you soon.</p>', 'oneplsone');
    wp_send_json($json);
}

【讨论】:

  • 你不会相信这对我 Galina 完全不相关的问题有多大帮助,非常感谢。然而,WC_Checkout 方法应该被访问为:WC()-&gt;checkout-&gt;create_order_fee_lines(例如)。 ??
猜你喜欢
  • 2011-11-20
  • 1970-01-01
  • 2013-12-02
  • 1970-01-01
  • 2015-01-15
  • 1970-01-01
  • 1970-01-01
  • 2019-02-20
  • 1970-01-01
相关资源
最近更新 更多