// Include your custom code here.
//add_action('acf/save_post', 'my_acf_save_post', 30);
function my_acf_save_post($post_id) {
// get value from a field
$value = get_field('customer_salerep', $post_id);
// update another field to this value
update_field('order_salerep', $value, $post_id);
}
// Custom
// END ENQUEUE PARENT ACTION
add_action( 'rest_api_init', 'create_api_order_meta_field' );
function create_api_order_meta_field() {
register_rest_field( 'shop_order', 'data_salerep', array(
'get_callback' => 'non_salerep_order',
'schema' => null,
)
);
}
function non_salerep_order( $object ) {
//$salerep_id = get_field( "order_salerep" );
$order_id = $object['id'];
//return get_post_meta( $salerep_id );
return get_post_meta( $order_id, 'order_salerep', true );
}
// For user
add_action( 'rest_api_init', 'create_api_user_meta_field' );
function create_api_user_meta_field() {
register_rest_field( 'user', 'data_salerep', array(
'get_callback' => 'non_salerep_user',
'schema' => null,
)
);
}
function non_salerep_user( $object ) {
//$salerep_id = get_field( "order_salerep" );
$user_id = $object['id'];
//return get_post_meta( $salerep_id );
return get_user_meta( $user_id, 'customer_salerep', true );
}
// Customer code
add_action( 'rest_api_init', 'create_api_user_meta_field_code' );
function create_api_user_meta_field_code() {
register_rest_field( 'user', 'my_customer_code', array(
'get_callback' => 'acf_customer_code',
'schema' => null,
)
);
}
function acf_customer_code( $object ) {
$user_id = $object['id'];
return get_user_meta( $user_id, 'customer_code', true );
}
// Whosale Role
// Thêm role wholesale_customer khi người dùng đăng ký thành công
add_action('user_register', 'vl_add_wholesale_customer_role_on_registration', 10, 1);
function vl_add_wholesale_customer_role_on_registration($user_id) {
// Lấy đối tượng user
$user = get_user_by('id', $user_id);
// Chỉ xử lý nếu user có role mặc định là customer (tức là đăng ký từ form WooCommerce/WordPress)
$user_roles = (array) $user->roles;
// Nếu đã có role customer thì mới thêm wholesale_customer
if (in_array('customer', $user_roles)) {
// Thêm role wholesale_customer (không xóa role customer)
$user->add_role('wholesale_customer');
// Hoặc nếu bạn muốn set nhiều role cùng lúc (gọn hơn):
// $user->set_role(''); // xóa hết role cũ trước (không khuyến khích)
// $user->add_role('customer');
// $user->add_role('wholesale_customer');
}
}
// Auto clear rest cache
/**
* Add or modify States
*/
add_filter( 'woocommerce_states', 'vn_woocommerce_states', 199 );
function vn_woocommerce_states( $states ) {
$states['VN'] = [
'44' => 'An Giang',
'43' => 'Bà Rịa–Vũng Tàu',
'57' => 'Bình Dương',
'58' => 'Bình Phước',
'40' => 'Bình Thuận',
'31' => 'Bình Định',
'55' => 'Bạc Liêu',
'54' => 'Bắc Giang',
'53' => 'Bắc Kạn',
'56' => 'Bắc Ninh',
'50' => 'Bến Tre',
'04' => 'Cao Bằng',
'59' => 'Cà Mau',
'CT' => 'Cần Thơ',
'30' => 'Gia Lai',
'03' => 'Hà Giang',
'63' => 'Hà Nam',
'HN' => 'Hà Nội',
'15' => 'Hà Tây',
'23' => 'Hà Tĩnh',
'14' => 'Hòa Bình',
'66' => 'Hưng Yên',
'61' => 'Hải Dương',
'HP' => 'Hải Phòng',
'73' => 'Hậu Giang',
'SG' => 'Hồ Chí Minh',
'34' => 'Khánh Hòa',
'47' => 'Kiên Giang',
'28' => 'Kon Tum',
'01' => 'Lai Châu',
'41' => 'Long An',
'02' => 'Lào Cai',
'35' => 'Lâm Đồng',
'09' => 'Lạng Sơn',
'67' => 'Nam Định',
'22' => 'Nghệ An',
'18' => 'Ninh Bình',
'36' => 'Ninh Thuận',
'68' => 'Phú Thọ',
'32' => 'Phú Yên',
'24' => 'Quảng Bình',
'27' => 'Quảng Nam',
'29' => 'Quảng Ngãi',
'13' => 'Quảng Ninh',
'25' => 'Quảng Trị',
'52' => 'Sóc Trăng',
'05' => 'Sơn La',
'21' => 'Thanh Hóa',
'20' => 'Thái Bình',
'69' => 'Thái Nguyên',
'26' => 'Thừa Thiên–Huế',
'46' => 'Tiền Giang',
'51' => 'Trà Vinh',
'07' => 'Tuyên Quang',
'37' => 'Tây Ninh',
'49' => 'Vĩnh Long',
'70' => 'Vĩnh Phúc',
'06' => 'Yên Bái',
'71' => 'Điện Biên',
'DN' => 'Đà Nẵng',
'33' => 'Đắk Lắk',
'72' => 'Đắk Nông',
'39' => 'Đồng Nai',
'45' => 'Đồng Tháp'
];
//error_log('woocommerce_states works');
return $states;
}
// Ajax Search
function asp_custom_rest_handler( $data ) {
$id = -2;
$defaults = $args = array(
's' => ''
);
foreach ( $defaults as $k => $v ) {
$param = $data->get_param($k);
if ( $param !== null ) {
$args[$k] = $param;
}
}
// Fetch the search ID, which is probably the WooCommerce search
foreach ( wd_asp()->instances->get() as $instance ) {
if ( in_array('product', $instance['data']['customtypes'] ) ) {
$id = $instance['id'];
break;
}
}
// No search was found with products enabled, set it explicitly
if ( $id == -2 ) {
$args['post_type'] = array('product');
}
$asp_query = new ASP_Query($args, $id);
return $asp_query->posts;
}
// POST to: http://example.com/wp-json/ajax-search-pro/v1/woo_search
add_action( 'rest_api_init', function () {
register_rest_route('ajax-search-pro/v0', '/woo_search', array(
'methods' => 'POST',
'callback' => 'asp_custom_rest_handler',
));
});
// Remove password metter
function iconic_remove_password_strength() {
wp_dequeue_script( 'wc-password-strength-meter' );
}
add_action( 'wp_print_scripts', 'iconic_remove_password_strength', 10 );
Warning: Cannot modify header information - headers already sent by (output started at /code/wp-content/plugins/vietlac-woocustom/custom-woocommerce.php:1) in /code/wp-includes/pluggable.php on line 1531
Warning: Cannot modify header information - headers already sent by (output started at /code/wp-content/plugins/vietlac-woocustom/custom-woocommerce.php:1) in /code/wp-includes/pluggable.php on line 1534