Thứ Hai, 21 tháng 5, 2018

Một số hook thông dụng trong woocommerce nên biết

Bài này sẽ tổng hợp một số hood (action, filter) thông dụng trong woocommerce … sẽ rất hữu ít cho ai đang làm về website bán hàng về woocommerce 🙂
Các bạn cần cái nào thì thêm code đó vào file functions.php trong theme đang sử dụng nhé

1. Xóa bỏ woocommerce result count

remove-woocommerce_result_count
Các bạn thêm code sau vào file functions.php trong theme đang sử dụng nhé
1
2
3
function woocommerce_result_count() {
 return;
}

2. Xóa bỏ woocommerce default sorting

remove-woocommerce_ordering
Xóa bỏ ở trang shop và trang cat
1
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );
Remove “default sorting” dropdown in StoreFront theme
1
2
3
// remove default sorting dropdown in StoreFront Theme
remove_action( 'woocommerce_after_shop_loop', 'woocommerce_catalog_ordering', 10 );
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 10 );

3. Xóa bỏ tiêu đề trang shop trong woocommerce bằng hood

remove page title
1
2
3
4
function override_page_title() {
 return false;
}
add_filter('woocommerce_show_page_title', 'override_page_title');
Hoặc
1
2
3
add_filter( 'woocommerce_show_page_title', function(){
 return false;
});

4. Thay đổi số sản phẩm trên 1 hàng.

Mặc định woocommerce là 4 sản phẩm trên 1 hàng, bây giờ ta thay thành 3 sản phẩm nhé.
1
2
3
4
5
6
7
// Change number or products per row to 3
add_filter('loop_shop_columns', 'loop_columns');
if (!function_exists('loop_columns')) {
 function loop_columns() {
 return 3; // 3 products per row
 }
}

5. Thay đổi số sản phẩm trên 1 page trong woocommerce.

Thay đổi thành 24 sản phẩm trên 1 page ta có code như bên dưới
1
2
// Display 24 products per page. Goes in functions.php
add_filter( 'loop_shop_per_page', create_function( '$cols', 'return 24;' ), 20 );
Nếu muốn hiển thị toàn bộ sản phẩm trên 1 pages thì ta thay 24 thành -1 là được
1
2
// Display all products per page. Goes in functions.php
add_filter( 'loop_shop_per_page', create_function( '$cols', 'return -1;' ), 20 );

6. Xóa bỏ nút Add to Cart trong trang shop và category

remove add to cart
1
2
3
4
function remove_loop_button(){
 remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
}
add_action('init','remove_loop_button');

7. Xóa bỏ SKU, Category, tag trong single product

remove_meta_single_product
1
remove_action('woocommerce_single_product_summary','woocommerce_template_single_meta',40);

8. Xóa bỏ tabs, upsell và related trong trang single product

1
2
3
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_product_data_tabs', 10 );
 remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_upsell_display', 15 );
 remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );

9. Chỉnh lại số product hiển thị và số product trên 1 dòng của related products

Các bạn thêm code sau vào functions.php nhé
1
2
3
4
5
6
add_filter( 'woocommerce_output_related_products_args', 'jk_related_products_args' );
function jk_related_products_args( $args ) {
 $args['posts_per_page'] = 4; //Số product hiển thị
 $args['columns'] = 4; // Số product trên 1 dòng
 return $args;
}

10. Xóa tiêu đề của phí ship

Thêm đoạn code sau vào functions.php của theme là được
1
2
/*Add to functions.php - Author levantoan.com*/
add_filter('woocommerce_order_shipping_to_display_shipped_via', '__return_false');

11. Bắt buộc đơn hàng đạt giá trị tối thiểu để có thể tiến hành thanh toán

Đoạn code này giúp cho các bạn đặt giá trị tối thiểu của đơn hàng (ví dụ >100k) để có thể tiến hành thanh toán. Hãy dán code này vào file functions.php của theme đang sử dụng
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
/*
Min Amount to checkout
Author levantoan.com
*/
add_action( 'woocommerce_checkout_process', 'devvn_wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'devvn_wc_minimum_order_amount' );
function devvn_wc_minimum_order_amount() {
    $minimum = '100000';
    $checkout_minimum_price_mess = 'Bạn cần có hóa đơn >100k để tiếp tục đặt hàng';
    if ( WC()->cart->total < $minimum ) {
        if( is_cart() ) {
            wc_print_notice(
                sprintf( $checkout_minimum_price_mess ,
                    wc_price( $minimum ),
                    wc_price( WC()->cart->total )
                ), 'error'
            );
        } else {
            wc_add_notice(
                sprintf( $checkout_minimum_price_mess ,
                    wc_price( $minimum ),
                    wc_price( WC()->cart->total )
                ), 'error'
            );
        }
    }
}
Cập nhật thường xuyên!

1 nhận xét:
Write nhận xét
  1. Cho mình hỏi cái nhé! khi mình chọn các lựa chọn sản phẩm thì giá nó nằm ngay bên dưới lựa chọn, >> làm sao mình cho nó lên trên thay vô vị trí của giá sản phẩm được nhỉ: như sản phẩm này nè: https://www.hnammobile.com/dien.../iphone-x-plus.14334.html khi mình bấm chọn màu giá nó tự thay đổi ở trên luôn

    Trả lờiXóa