[PHP/Woocommerce] Thêm trường tùy chỉnh vào thông tin biến thể sản phẩm trong backend

0
89

Code thêm trường tùy chỉnh vào thông tin biến thể sản phẩm trong backend (How to Add a Custom Field to Product Variations in WooCommerce):

  • Đoạn 1: Thêm trường ngày ship vào biến thể trong backend
  • Đoạn 2: Lưu dự liệu trường thông tin mới
// 1. Thêm trường ngày ship vào biến thể trong backend
add_action( 'woocommerce_variation_options_pricing', 'custom_add_ship_date_to_variations', 10, 3); 
function custom_add_ship_date_to_variations ($loop, $variation_data, $variation) {
woocommerce_wp_text_input(array(
	'id' => 'ship_date['. $loop .']',
	'class' => 'short',	
	'style' => 'width: 100%;',	
	'placeholder'   => 'Nhập ngày dự kiến ship sản phẩm. Ví dụ: 22/12',
	'value' => get_post_meta($variation->ID, 'ship_date', true),
	));
}
// 2. Lưu dự liệu trường thông tin mới
add_action( 'woocommerce_save_product_variation', 'custom_save_ship_date_variations', 10, 2); 
function custom_save_ship_date_variations ($variation_id, $i) { 
	$ship_date= $_POST['da_ban'][$i];
	if (isset($ship_date)) {
	update_post_meta($variation_id, 'ship_date', esc_attr($da_ban));
	}
}

-youtube: @TechnicalRajni-

P/S: đoạn code show thông tin trường ra frontend:

echo get_post_meta( $variation_id, 'ship_date', true );
Link rút gọn của bài viết: https://longnh.com/noH14