Woocommerce là một plugin nổi tiếng hỗ trợ xây dựng một trang web thương mại điện tử một cách nhanh chóng và hoàn hảo. Dưới đây là tổng hợp các đoạn code có thể giúp tùy biến theo nhu cầu sử dụng của bạn. Mình sẽ cập nhật thường xuyên tips code hay liên quan đến Woocommerce, bạn nhớ theo dõi nha.
Okay, let’s go!!!
Di chuyển phần mô tả danh mục sản phẩm
Nếu bạn muốn di chuyển phần mô tả danh mục sản phẩm xuống bên dưới danh sách sản phẩm thì sử dụng đoạn code dưới đây:
function tu_move_wc_archive_description()
{
if (is_archive()) {
remove_action('woocommerce_archive_description', 'woocommerce_taxonomy_archive_description', 10);
remove_action('woocommerce_archive_description', 'woocommerce_product_archive_description', 10);
add_action('woocommerce_after_main_content', 'woocommerce_taxonomy_archive_description', 10);
add_action('woocommerce_after_main_content', 'woocommerce_product_archive_description', 10);
}
}
add_action('wp', 'tu_move_wc_archive_description');
Bỏ các trường không cần thiết tại trang thanh toán
add_filter( 'woocommerce_checkout_fields' , 'custom_remove_woo_checkout_fields' );
function custom_remove_woo_checkout_fields( $fields ) {
// remove billing fields
// unset($fields['billing']['billing_first_name']);
// unset($fields['billing']['billing_last_name']);
unset($fields['billing']['billing_company']);
unset($fields['billing']['billing_address_1']);
unset($fields['billing']['billing_address_2']);
unset($fields['billing']['billing_city']);
unset($fields['billing']['billing_postcode']);
unset($fields['billing']['billing_country']);
unset($fields['billing']['billing_state']);
// unset($fields['billing']['billing_phone']);
// unset($fields['billing']['billing_email']);
// remove shipping fields
unset($fields['shipping']['shipping_first_name']);
unset($fields['shipping']['shipping_last_name']);
unset($fields['shipping']['shipping_company']);
unset($fields['shipping']['shipping_address_1']);
unset($fields['shipping']['shipping_address_2']);
unset($fields['shipping']['shipping_city']);
unset($fields['shipping']['shipping_postcode']);
unset($fields['shipping']['shipping_country']);
unset($fields['shipping']['shipping_state']);
// remove order comment fields
unset($fields['order']['order_comments']);
return $fields;
Xóa Badge giảm giá
Nếu bạn muốn xóa Badge giảm giá mặc định để thay thế badge tự tùy biến.
add_filter( 'woocommerce_sale_flash', '__return_null' );
Thay đổi nhãn ‘thêm vào giỏ’
Bạn có thể thay đổi chữ ‘Mua ngay’ theo ý thích của bạn.
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_single_add_to_cart_text' );
function woo_custom_single_add_to_cart_text() {
return __( 'Mua ngay', 'woocommerce' );
}
Bỏ các menu không cần thiết tại trang My Account
add_filter('woocommerce_account_menu_items', 'tk99_remove_my_account_links');
function tk99_remove_my_account_links($menu_links)
{
// unset($menu_links['edit-address']); // Addresses
unset( $menu_links['dashboard'] ); // Remove Dashboard
unset( $menu_links['payment-methods'] ); // Remove Payment Methods
unset( $menu_links['orders'] ); // Remove Orders
// unset( $menu_links['downloads'] ); // Remove Downloads
unset( $menu_links['edit-account'] ); // Remove Account details tab
unset( $menu_links['customer-logout'] ); // Remove Logout link
return $menu_links;
}
Tạo trường xác nhận mật khẩu khi tạo tài khoản tại trang thanh toán

add_action('woocommerce_checkout_init', 'wc_add_confirm_password_checkout', 10, 1);
function wc_add_confirm_password_checkout($checkout)
{
if (get_option('woocommerce_registration_generate_password') == 'no') {
$fields = $checkout->get_checkout_fields();
$fields['account']['account_password2'] = array(
'type' => 'password',
'label' => __('Xác nhận mật khẩu', 'woocommerce'),
'placeholder' => _x('Nhập lại mật khẩu của bạn', 'placeholder', 'devvn'),
'required' => true,
'class' => array('form-row-first')
);
$checkout->__set('checkout_fields', $fields);
}
}
// Check the password and confirm password fields match before allow checkout to proceed.
add_action('woocommerce_after_checkout_validation', 'wc_check_confirm_password_matches_checkout', 10, 2);
function wc_check_confirm_password_matches_checkout($posted)
{
$checkout = WC()->checkout;
if (!is_user_logged_in() && ($checkout->must_create_account || !empty($posted['createaccount']))) {
if (strcmp($posted['account_password'], $posted['account_password2']) !== 0) {
wc_add_notice(__('Mật khẩu không giống nhau', 'woocommerce'), 'error');
}
}
}
Xóa chọn số lượng (quantity input) trong sản phẩm
function wc_remove_all_quantity_fields( $return, $product ) {
return true;
}
add_filter( 'woocommerce_is_sold_individually', 'wc_remove_all_quantity_fields', 10, 2 );
Không cho phép thay đổi Email tại trang My Account
Đoạn code này không cho phép người dùng thay đổi email tại trang quản lý tài khoản
add_action( 'woocommerce_after_edit_account_form', 'tk99_disable_edit_email_address' );
function tk99_disable_edit_email_address( ) {
$script = '<script type="text/javascript">'.
'var account_email = document.getElementById("account_email");'.
'if(account_email) { '.
' account_email.readOnly = true; '.
' account_email.className += " disable-input";'.
'}'.
'</script>';
echo $script;
}
Xóa WooCommerce Marketing Hub, Analytics
/* Remove WooCommerce Marketing Hub, Analytics Menu Woo v4.3+
*===============================================================*/
add_filter( 'woocommerce_admin_features', function( $features ) {
return array_values(
array_filter( $features, function($feature) {
return ! in_array( $feature, [ 'marketing' ] );
} )
);
} );
/* Disable WooCommerce Admin (Analytics)
*===============================================================*/
add_filter( 'woocommerce_admin_disabled', '__return_true' );
Hiện thị cột ngày giờ đặt hàng tại danh sách đơn hàng

add_filter('manage_edit-shop_order_columns', 'tk99_add_new_order_admin_column');
function tk99_add_new_order_admin_column($columns)
{
unset($columns['order_date']); // remove the default column
$edit_columns = array_splice($columns, 0, 2);
$edit_columns['order_custom_date'] = 'Ngày Giờ';
return array_merge($edit_columns, $columns);
}
add_action('manage_shop_order_posts_custom_column', 'so61375632_add_new_order_admin_column_content', 20, 2);
function so61375632_add_new_order_admin_column_content($column, $order_id)
{
if ($column == 'order_custom_date') {
$order = wc_get_order($order_id);
if (!is_wp_error($order)) {
echo $order->get_date_created()->format('d/m/Y H:i');
}
}
}
Xóa và thay đổi tên nhãn ‘sắp xếp’ tại danh mục sản phẩm
// Remove some options from the “sort-by” list
add_filter('woocommerce_catalog_orderby', 'tk99_remove_sorting_option_woocommerce_shop');
function bbloomer_remove_sorting_option_woocommerce_shop($options)
{
unset($options['popularity']);
unset($options['date']);
return $options;
}
// Rename default sorting options
add_filter('woocommerce_catalog_orderby', 'tk99_rename_default_sorting_options');
function tk99_rename_default_sorting_options($options)
{
$options['price-desc'] = 'Giá cao đến thấp'; // rename
$options['price'] = 'Giá thấp đến cao'; // rename
$options['rating'] = 'Theo đánh giá'; // rename
return $options;
}
Còn update liên tục…
AE còn code tùy biến nào hay thì comment cho mọi người tham khảo nha.
Nguồn: Sưu tầm + Dev