Woocommerce là một plugin miễn phí và đơn giản nhất để xây dựng một website bán hàng cho bất kì ai cũng có thể sử dụng, do vậy nó sẽ có rất nhiều tính năng thừa cũng như những thiết kế không vừa ý với nhiều người. Để tùy biến Woocommerce theo mong muốn bạn không cần biết quá nhiều về code. Bên dưới là 5 đoạn code Thằng Mò tin rằng giúp cho các chức năng của Woocommerce trên website của bạn trở nên hữu ích hơn
Cách tùy chỉnh file function.php WordPress
Tùy chỉnh file function.php trong WordPress khá đơn giản, bạn chỉ việc tìm file function.php của theme đang sử dụng và thêm code vào đó. Thằng Mò khuyến cáo: Nên sử dụng Child theme để tinh chỉnh.
Bạn vào quyền Admin > Giao diện > Sửa (Giao diện) và tìm file function.php. Tùy WP theme sẽ có các vị trí file function khác nhau. Thêm code tùy chỉnh vào sau thẻ <?php, nên để dưới cuối cùng của file.
Top 5 đoạn code hay và hữu ích dành cho Woocommerce
Để tùy biến Woocommerce cần thêm code vào file function.php như hướng dẫn trên, những đoạn code dưới Thằng Mò đã test và hoạt động khá ổn không có lỗi phát sinh.
Ẩn chức năng download trên Woocommerce
//*Ẩn chức năng download*// function custom_my_account_menu_items( $items ) { unset($items['downloads']); return $items; } add_filter( 'woocommerce_account_menu_items', 'custom_my_account_menu_items' );
Thay đổi đơn vị đ thành VNĐ trên Woocommerce
//*Thay đổi đơn vị đ thành VNĐ*// add_filter('woocommerce_currency_symbol', 'change_existing_currency_symbol', 10, 2); function change_existing_currency_symbol( $currency_symbol, $currency ) { switch( $currency ) { case 'VND': $currency_symbol = 'VND'; break; } return $currency_symbol; }
Thay “Add to cart” thành “Thêm vào giỏ” trên Woocommerce
//*Thay "Add to cart" thành "Thêm vào giỏ" trang sản phẩm*// function woo_custom_cart_button_text() { return __('Thêm vào giỏ', 'woocommerce'); } add_filter('single_add_to_cart_text', 'woo_custom_cart_button_text'); //*Thay "Add to cart" thành "Thêm vào giỏ" trang kho lưu trữ*// function woo_archive_custom_cart_button_text() { return __( 'Thêm vào giỏ', 'woocommerce' ); } add_filter( 'add_to_cart_text', 'woo_archive_custom_cart_button_text' );
Thay thông báo sau khi thêm sản phẩm vào giỏ hàng trên Woocommerce
//*Thay đổi đoạn thông báo sau khi thêm sản phẩm vào giỏ hàng*// add_filter( ‘woocommerce_add_to_cart_message’, ‘custom_add_to_cart_message’ ); function custom_add_to_cart_message() { global $woocommerce; // Thông báo thành công if (get_option(‘woocommerce_cart_redirect_after_add’)==’yes’) : $return_to = get_permalink(woocommerce_get_page_id(‘shop’)); $message = sprintf(‘<a href=”%s” class=”button”>%s</a> %s’, $return_to, __(‘Continue Shopping →’, ‘woocommerce’), __(‘Nội dung bạn muốn thay đổi.’, ‘woocommerce’) ); else : $message = sprintf(‘<a href=”%s” class=”button”>%s</a> %s’, get_permalink(woocommerce_get_page_id(‘cart’)), __(‘View Cart →’, ‘woocommerce’), __(‘Nội dung bạn muốn thay đổi.’, ‘woocommerce’) ); endif; return $message;
Chuyển giá 0đ thành Liên hệ trên Woocommerce
//*Chuyển giá 0đ thành Liên hệ*// function tvc_wc_custom_get_price_html( $price, $product ) { if ( $product->get_price() == 0 ) { if ( $product->is_on_sale() && $product->get_regular_price() ) { $regular_price = wc_get_price_to_display( $product, array( 'qty' => 1, 'price' => $product->get_regular_price() ) ); $price = wc_format_price_range( $regular_price, __( 'Free!', 'woocommerce' ) ); } else { $price = '<span class="amount">' . __( 'Liên hệ', 'woocommerce' ) . '</span>'; } } return $price; } add_filter( 'woocommerce_get_price_html', 'tvc_wc_custom_get_price_html', 10, 2 );