Một số hook hay dùng trong woocommerce: nói chung là đi lòng vòng trên google rồi tìm code cũng tốn thời gian, thôi lập ra một post để lưu trữ vậy
Code dưới là dùng trong class nhé mấy bạn
1 2 3 4 |
add_filter ('woocommerce_add_to_cart_redirect', array( $this, 'redirect_to_checkout')); function redirect_to_checkout() { return wc_get_checkout_url(); } |
Lưu ý đây là code xử lý cho product không có variation (biến thể) với form_data là dữ liệu gửi đi từ form popup
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
<?php global $post; ?> <div class="quickorderwc-modalDialog" > <div> <a href="#" title="Đóng" class="close close-notice-order">X</a> <form class="quickorderwc_cusstom_info" id="quickorderwc_cusstom_info" method="post" enctype="multipart/form-data" > <input type="hidden" class="quickorderwc_product_id" name="quickorderwc_product_id" value="<?php echo $post->ID; ?>" /> <div class="popup-customer-info"> <div class="popup-customer-info-title">Thông tin người mua</div> <div class="popup-customer-info-group popup-customer-info-radio"> <label> <input type="radio" name="customer-gender" value="1" checked=""> <span>Anh</span> </label> <label> <input type="radio" name="customer-gender" value="2"> <span>Chị</span> </label> </div> <div class="popup-customer-info-group"> <div class="popup-customer-info-item-2 popup-customer-info-name"> <input type="text" class="customer-name" name="customer-name" placeholder="Họ và tên"> </div> <div class="popup-customer-info-item-2 popup-customer-info-phone"> <input type="text" class="customer-phone" name="customer-phone" placeholder="Số điện thoại"> </div> </div> <div class="popup-customer-info-group"> <div class="popup-customer-info-item-1"> <input type="text" class="customer-email" name="customer-email" placeholder="Địa chỉ email"> </div> </div> <div class="popup-customer-info-group"> <div class="popup-customer-info-item-1"> <textarea class="customer-address" name="customer-address" placeholder="<?php _e('Địa chỉ nhận hàng','quickorderwc'); ?>"></textarea> </div> </div> <div class="popup-customer-info-group"> <div class="popup-customer-info-item-1"> <label style="font-weight:400;"> Số lượng: <input type="text" class="quickorderwc_product_quantity" name="quickorderwc_product_quantity" type="number" value="1" > </label> </div> </div> <div class="popup-customer-info-group"> <div class="popup-customer-info-item-1"> <textarea class="order-note" name="order-note" placeholder="Ghi chú đơn hàng (Không bắt buộc)"></textarea> </div> </div> <div class="popup-customer-info-group"> <div class="popup-customer-info-item-1"> <button type="submit" class="button alt" id="quick_order_trigger">Đặt hàng ngay</button> </div> </div> <div class="popup-customer-info-group"> <div class="popup-customer-info-item-1"> <div class="quickorderwc_quickbuy_mess"></div> </div> </div> </div> </form> </div> </div> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
$(document).on('submit','form#quickorderwc_cusstom_info',function(e){ e.preventDefault(); var form = $(this), full_name=form.find('.customer-name').val(), phone = form.find('.customer-phone').val(), email = form.find('.customer-email').val(), note = form.find('textarea.order-note').val(), product_id = form.find('.quickorderwc_product_id').val(), product_quantity = form.find('.quickorderwc_product_quantity').val(), shipping_address = form.find('.customer-address').val(); if( full_name == ''){ alert(quick_order_for_wc_object.notice_full_name); form.find('.customer-name').focus(); return false } if( phone == ''){ alert(quick_order_for_wc_object.notice_phone); form.find('.customer-phone').focus(); return false } if(!is_phonenumber(phone)){ alert(quick_order_for_wc_object.notice_illegal_phone); form.find('.customer-phone').focus(); return false } if( shipping_address == ''){ alert(quick_order_for_wc_object.notice_shipping_address); form.find('.customer-address').focus(); return false } var form_data = $(this).serializeArray(); var submit_data = { action: 'quickorderwc_place_order', form_data: form_data }; $("button#quick_order_trigger").html('<i class="fa fa-spinner fa-spin fa-3x fa-fw"></i> Đang gửi đơn hàng...'); console.log(submit_data); $.ajax({ type: 'POST', url: quick_order_for_wc_object.ajaxurl, data: submit_data, success: function ( response ) { console.log(response); if (response.success ) { $("button#quick_order_trigger").html('Đặt hàng ngay'); $('.quickorderwc-modalDialog').hide(); alert(quick_order_for_wc_object.notice_thank_you); } } }); }) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
function quickorderwc_place_order(){ $results=array(); foreach($_POST['form_data'] as $item){ if( $item['name'] == 'quickorderwc_product_id'){ $product_id = $item['value']; }; if( $item['name'] == 'customer-gender'){ $gender = $item['value']; }; if( $item['name'] == 'customer-name'){ $full_name = $item['value']; }; if( $item['name'] == 'customer-name'){ $full_name = $item['value']; }; if( $item['name'] == 'customer-phone'){ $phone= $item['value']; }; if( $item['name'] == 'customer-email'){ $email= $item['value']; }; if( $item['name'] == 'customer-address'){ $shipping_address= $item['value']; }; if( $item['name'] == 'order-note'){ $note = $item['value']; }; if( $item['name'] == 'quickorderwc_product_quantity'){ $product_quantity = $item['value']; }; } $full_name = explode(" ",$full_name); $full_name_count=count($full_name); $first_name = $full_name[$full_name_count-1]; $last_name = $full_name[0]; if($full_name_count == 3){ $middle_name = $full_name[1]; }elseif($full_name_count == 4){ $middle_name = $full_name[1]." ".$full_name[2]; }if($full_name_count == 2){ $middle_name = ''; } $product_check = wc_get_product($product_id); if(!$product_check || is_wp_error($product_check)) wp_send_json_error(); $address = array( 'first_name' => $first_name, 'last_name' => $last_name, 'email' => $email, 'phone' => $phone, 'address_1' => $shipping_address ); // Now we create the order $order = wc_create_order(); if(!is_wp_error($order)) { $args = array( 'product_id' => $product_id ); $order->add_product($product_check, $product_quantity, $args); $order->set_address($address, 'billing'); $order->set_address($address, 'shipping'); $order->set_customer_note($note); $order->calculate_totals(); $order->update_status("processing", 'Đơn hàng nhanh', TRUE); do_action( 'woocommerce_checkout_order_processed', $order->get_id(), array(), $order ); /* $result['content'] = str_replace('%%order_id%%', $order->get_order_number(), $quickbuy_settings['popup_sucess']); $result['gotothankyou'] = ($quickbuy_settings['popup_gotothankyou'] == 1 )? true : false; $result['thankyou_link'] = $order->get_checkout_order_received_url();; */ } $results[]=$results; wp_send_json_success($results); } |
1 2 |
$get_cart_contents_count = (WC()->cart->get_cart_contents_count() > 0) ? "[".WC()->cart->get_cart_contents_count()."]" : ''; <a class="shopping-cart" href="'.wc_get_cart_url().'"><span class="get_cart_contents_count">'.$get_cart_contents_count.'</span><i class="fas fa-shopping-cart"></i></a> |
Xin lưu ý code dùng trong class
1 2 3 4 5 6 7 8 |
add_filter( 'woocommerce_add_to_cart_fragments', array(__CLASS__,'iconic_cart_count_fragments'), 10, 1 ); function iconic_cart_count_fragments( $fragments ) { $get_cart_contents_count = (WC()->cart->get_cart_contents_count() > 0) ? "[".WC()->cart->get_cart_contents_count()."]" : ''; $fragments['.get_cart_contents_count'] = '<span class="get_cart_contents_count">'.$get_cart_contents_count.'</span>'; return $fragments; } |
1 2 3 4 5 6 7 8 9 |
add_action( 'woocommerce_thankyou',array($this,"redirect_to_custom_thankyou_page") ); function redirect_to_custom_thankyou_page($order_id){ //id page cần chuyển đến $page_id=134; if(!empty(get_permalink($page_id))){ wp_safe_redirect(get_permalink($page_id)); exit; } } |
Mọi sự sao chép xin ghi rõ nguồn là fcwordpress.net
Chuyên trang về wordpress: hướng dẫn thiết kế theme, plugin, thủ thuật wordpress