• Bỏ qua primary navigation
  • Skip to main content
  • Bỏ qua primary sidebar

Thủ thuật thiết kế website

Chia sẻ kinh nghiệm thiết kế website

  • Trang chủ
  • Joomla
    • Thủ thuật Joomla
    • Joomla Extensions
    • Joomla Themes
  • WordPress
    • Thủ thuật WordPress
    • WordPress Plugins
    • WordPress Themes
  • PHP Framework
    • Codeigniter
    • Laravel
    • Laminas
  • App Developer
    • React Native
    • Flutter
  • SEO
  • Chia sẻ
  • Phần mềm
Bạn đang ở:Trang chủ / Wordpress / Thủ thuật WordPress / [Woocommerce] Chọn cửa hàng gần nhất trong mục thanh toán

[Woocommerce] Chọn cửa hàng gần nhất trong mục thanh toán

20/04/2021 - Administrator Để lại bình luận

Đoạn code này cho phép chúng ta chọn cửa hàng gần nhất trong mục thanh toán mà không cần cài thêm plugin nào cho website.

Đoạn code này được chia sẻ trên Group WPVN Tám mình chỉ tổng hợp và chia sẻ lại cho mọi người. Bạn thêm đoạn code sau và file function.php của theme :

function tao_custom_post_type()
{
    /*
     * Biến $label để chứa các text liên quan đến tên hiển thị của Post Type trong Admin
     */
    $label = array(
        'name' => 'Các cửa hàng', //Tên post type dạng số nhiều
        'singular_name' => 'Cửa hàng' //Tên post type dạng số ít
    );
 
    /*
     * Biến $args là những tham số quan trọng trong Post Type
     */
    $args = array(
        'labels' => $label, //Gọi các label trong biến $label ở trên
        'description' => 'Post type đăng cửa hàng', //Mô tả của post type
        'supports' => array(
            'title',
            'editor',
            'excerpt',
            'author',
            'thumbnail',
            'comments',
            'trackbacks',
            'revisions',
            'custom-fields'
        ), //Các tính năng được hỗ trợ trong post type
        'taxonomies' => array( 'category', 'post_tag' ), //Các taxonomy được phép sử dụng để phân loại nội dung
        'hierarchical' => false, //Cho phép phân cấp, nếu là false thì post type này giống như Post, true thì giống như Page
        'public' => true, //Kích hoạt post type
        'show_ui' => true, //Hiển thị khung quản trị như Post/Page
        'show_in_menu' => true, //Hiển thị trên Admin Menu (tay trái)
        'show_in_nav_menus' => true, //Hiển thị trong Appearance -> Menus
        'show_in_admin_bar' => true, //Hiển thị trên thanh Admin bar màu đen.
        'menu_position' => 5, //Thứ tự vị trí hiển thị trong menu (tay trái)
        'menu_icon' => '', //Đường dẫn tới icon sẽ hiển thị
        'can_export' => true, //Có thể export nội dung bằng Tools -> Export
        'has_archive' => true, //Cho phép lưu trữ (month, date, year)
        'exclude_from_search' => false, //Loại bỏ khỏi kết quả tìm kiếm
        'publicly_queryable' => true, //Hiển thị các tham số trong query, phải đặt true
        'capability_type' => 'post' //
    );
 
    register_post_type('cua-hang', $args); //Tạo post type với slug tên là sanpham và các tham số trong biến $args ở trên
 
}
/* Kích hoạt hàm tạo custom post type */
add_action('init', 'tao_custom_post_type');

add_action( 'woocommerce_before_order_notes', 'field_store',10 );

function field_store() {
   $args = array( 
	  'post_type' => 'cua-hang',
	 'post_status' => 'publish',  
);
	$the_query = new WP_Query( $args );
echo '<div class="chon-cua-hang"><ul>';
	  echo '<li><input type="radio" onclick="click_show()" class="input-radio " value="1" name="chon_cua_hang" id="chon-ch">';
			echo '<label for="chon_cua_hang">Vui lòng chọn cửa hàng gân nhất</label></li>';
	 echo '<li><input type="radio" class="input-radio "onclick="click_hide()"  value="Giao hàng tận nơi" name="address_store" id="giao_hang_tannoi">';
			echo '<label for="giao_hang_tannoi">Giao hàng tận nơi</label></li>';
	echo '</ul></div>';
	// The Loop
	if ( $the_query->have_posts() ) :
	
	echo '<ul id="ds-cua-hang">';
	while ( $the_query->have_posts() ) : $the_query->the_post();
		
		  echo '<li><input type="radio" class="input-radio " value="'.get_the_title().'" name="address_store" id="'.get_the_ID().'">';
			echo '<label for="'.get_the_ID().'">'.get_the_title().'</label></li>';
	endwhile;
	echo '</ul>';
	endif;

	// Reset Post Data
	wp_reset_postdata();
	?>
	<script>
		function click_show(){
				document.getElementById("ds-cua-hang").style.display = "block";
				document.getElementById("giao_hang_tannoi").checked = false;
		}
		function click_hide(){
				document.getElementById("ds-cua-hang").style.display = "none";
				document.getElementById("chon-ch").checked = false;
		}
		
	</script>
<?php
}

//* Update the order meta with field value
add_action('woocommerce_checkout_update_order_meta', 'lv_select_checkout_field_update_order_meta');
function lv_select_checkout_field_update_order_meta( $order_id ) {
   if ($_POST['address_store']) update_post_meta( $order_id, 'address_store', esc_attr($_POST['address_store']));
}

//* Display field value on the order edition page
add_action( 'woocommerce_admin_order_data_after_billing_address', 'lv_select_checkout_field_display_admin_order_meta', 10, 1 );
function lv_select_checkout_field_display_admin_order_meta($order){
    echo '<p><strong>'.__('Cửa hàng').':</strong> ' . get_post_meta( $order->id, 'address_store', true ) . '</p>';
}
//* Add selection field value to emails
add_filter('woocommerce_email_order_meta_keys', 'lv_select_order_meta_keys');
function lv_select_order_meta_keys( $keys ) {
    $keys['address_store'] = 'address_store';
    return $keys;
     
}

add_action( 'woocommerce_thankyou', 'view_order_and_thankyou_page', 20 );
add_action( 'woocommerce_view_order', 'view_order_and_thankyou_page', 20 );
function view_order_and_thankyou_page( $order_id ){  ?>
    <table class="woocommerce-table shop_table gift_info">
        <tfoot>
            
            <tr>
                <th scope="row">Cửa hàng</th>
                <td><?php echo get_post_meta( $order_id, 'address_store', true ); ?></td>
            </tr>
          
        </tfoot>
    </table>
<?php }
?>

Và chúng ta thêm đoạn code CSS này vào file style.css của theme là xong.

.chon-cua-hang ul{
 display:flex;
}
#ds-cua-hang{
 display:none;
}

Chúc các bạn thành công !

Nguồn : Group WPVN Tám

0 0 đánh giá
Article Rating
Theo dõi
Đăng nhập
Thông báo của
guest
guest
0 Comments
Phản hồi nội tuyến
Xem tất cả bình luận

Sidebar chính

LỜI NGỎ

Đây là blog cá nhân, cung cấp các thông tin, kiến thức và kinh nghiệm về lập trình và cuộc sống. Những bài viết được mình sưu tập từ nhiều nguồn, mọi chi tiết liên quan đến bản quyền xin vui lòng liên hệ qua email kairu2607@gmail.com ! Cám ơn rất nhiều.

Tìm kiếm

Thủ thuật Wordpress

Hướng dẫn tạo liên hệ All in one phiên bản có yêu cầu gọi lại

Cách Tạo Demo Website Mockup – giới thiệu trang tạo Demo Website Mockup

Hướng Dẫn Cấu Hình SMTP Để Gửi Mail Trên WordPress

Cách tự tạo thêm Sidebar cho theme wordpress

Vì Sao Rank Math Vượt Trội – SEO WordPress 2023

Thêm lượt xem bài viết vào WordPress Post Views Counter

Website Bị Hack Virus Index Tiếng Nhật và Toàn Tập Các Cách Xử Lý

Laravel

Câu hỏi phỏng vấn Laravel cơ bản đến nâng cao

Câu lệnh Php Artisan Laravel thường dùng nên biết

Tổng hợp câu lệnh trong Laravel PHP Framework

[Laravel Tutorials] Tổ chức theo dạng packages/modules – Phần 4: tạo local package với Composer

[Laravel Tutorials] Tổ chức theo dạng packages/modules – Phần 3: Load migrations, models, translations

[Laravel Tutorials] Tổ chức theo dạng packages/modules – Phần 2: Load views và controllers

Codeigniter Framework

[CodeIgniter 4] Codeigniter 4 Remove Public and Index.php From URL

[CodeIgniter 4] How to upload Codeigniter 4 website on share hosting?

Sửa lỗi website Codeigniter 2.x không chạy được với PHP 7.x

[CodeIgniter 4] Sử dụng cURL trong CodeIgniter 4

[CodeIgniter 4] Sử dụng cache để tăng tốc website trong CodeIgniter 4

[CodeIgniter 4] Xử lý hình ảnh chuyên nghiệp trong CodeIgniter 4

[CodeIgniter 4] Hướng dẫn gửi mail trong CodeIgniter 4

Dịch vụ Thiết Kế Website

Phần mềm hay

Hướng dẫn sử dụng phần mềm putty trên Windows

Switching between multiple PHP versions on macOS

Byebye Edge Chromium/Microsoft Edge

Hướng dẫn tắt Gatekeeper (Bật tùy chọn Anywhere) sửa lỗi “damaged and can’t be opened. You should move it to the Trash”

Cài lại macOS trên Mac Silicon (M1) “sạch trơn” bằng macOS Recovery

Copyright © 2023 · Metro Pro on Genesis Framework · WordPress · Đăng nhập

wpDiscuz