Để tuỳ chỉnh những chức năng trong Woocommerce thì có nhiều theme bạn có thể thay đổi các option đó, tuy nhiên cũng có một số thay đổi bạn phải chỉnh sửa code ngay trong file functions.php, với việc bạn chỉ cần copy những dòng code dưới đây thì có thể thay đổi được woocommerce, mình thấy rằng, chỉ với những đoạn code này bạn hoàn toàn tuỳ chỉnh Woocommerce ưng ý. Dưới đây là tổng hợp các đoạn code chỉnh sửa tuỳ theo mong muốn của bạn
1. Thay chữ “Add to Cart” hoặc “Thêm vào giỏ”
2. Xóa chữ “Product” trên thanh điều hướng
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 |
/* * Hide “Products” in WooCommerce breadcrumb */ function woo_custom_filter_breadcrumbs_trail ( $trail ) { foreach ( $trail as $k => $v ) { if ( strtolower( strip_tags( $v ) ) == ‘products’ ) { unset( $trail[$k] ); break; } }
return $trail; }
add_filter( ‘woo_breadcrumbs_trail’, ‘woo_custom_filter_breadcrumbs_trail’, 10 ); |
3. Ẩn các phương thức vận chuyển khác khi phương thức miễn phí kích hoạt
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
// Hide ALL shipping options when free shipping is available add_filter( ‘woocommerce_available_shipping_methods’, ‘hide_all_shipping_when_free_is_available’ , 10, 1 );
/** * Hide ALL Shipping option when free shipping is available * * @param array $available_methods */ function hide_all_shipping_when_free_is_available( $available_methods ) {
if( isset( $available_methods[‘free_shipping’] ) ) :
// Get Free Shipping array into a new array $freeshipping = array(); $freeshipping = $available_methods[‘free_shipping’];
// Empty the $available_methods array unset( $available_methods );
// Add Free Shipping back into $avaialble_methods $available_methods = array(); $available_methods[] = $freeshipping;
endif;
return $available_methods; } |
4. Kéo dài field nhập địa chỉ
01 02 03 04 05 06 07 08 09 10 |
add_filter(‘woocommerce_billing_fields’, ‘custom_woocommerce_billing_fields’);
function custom_woocommerce_billing_fields( $fields ) {
$fields[‘billing_address_1’][‘class’] = array( ‘form-row-wide’ );
$fields[‘billing_address_2’][‘class’] = array( ‘form-row-wide’ );
return $fields; } |
5. Tự thêm sản phẩm vào giỏ mỗi khi khách truy cập vào
Thay số 64 trong đoạn code thành ID của sản phẩm mà bạn cần tự động thêm vào giỏ.
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
/* * Add item to cart on visit */ function add_product_to_cart() { if ( ! is_admin() ) { global $woocommerce; $product_id = 64; $found = false; //check if product already in cart if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) { foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) { $_product = $values[‘data’]; if ( $_product->id == $product_id ) $found = true; } // if product not found, add it if ( ! $found ) $woocommerce->cart->add_to_cart( $product_id ); } else { // if no products in cart, add it $woocommerce->cart->add_to_cart( $product_id ); } } } add_action( ‘init’, ‘add_product_to_cart’ ); |
6. Sửa đường link nút “Add to Cart”
01 02 03 04 05 06 07 08 09 |
add_filter(‘add_to_cart_redirect’, ‘custom_add_to_cart_redirect’);
function custom_add_to_cart_redirect() { /** * Replace with the url of your choosing * e.g. return ‘http://www.yourshop.com/’ */ return get_permalink( get_option(‘woocommerce_checkout_page_id’) ); } |
7. Thêm ký hiệu tiền tệ theo ý mình
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 |
add_filter( ‘woocommerce_currencies’, ‘add_my_currency’ );
function add_my_currency( $currencies ) { $currencies[‘VNĐ’] = __( ‘Vietnam Dong’, ‘woocommerce’ ); return $currencies; }
add_filter(‘woocommerce_currency_symbol’, ‘add_my_currency_symbol’, 10, 2);
function add_my_currency_symbol( $currency_symbol, $currency ) { switch( $currency ) { case ‘VNĐ’: $currency_symbol = ‘$’; break; } return $currency_symbol; } |
8. Sửa số lượng sản phẩm hiển thị ở mỗi trang
01 02 |
// Display 24 products per page. Goes in functions.php add_filter( ‘loop_shop_per_page’, create_function( ‘$cols’, ‘return 24;’ ), 20 ); |
9. Tự chuyển tới trang thanh toán sau khi thêm vào giỏ
Hiện tại tính năng này không cần sử dụng code nữa mà bạn có thể bật trong Woocommerce -> Settings -> Products -> Display và chọn Redirect to the cart page after successful addition.
10. Gửi email sau khi thanh toán bằng coupon
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
/** * WooCommerce Extra Feature * ————————– * * Send an email each time an order with coupon(s) is completed * The email contains coupon(s) used during checkout process * */ function woo_email_order_coupons( $order_id ) { $order = new WC_Order( $order_id );
if( $order->get_used_coupons() ) {
$to = ‘youremail@yourcompany.com’; $subject = ‘New Order Completed’; $headers = ‘From: My Name ‘ . “\r\n”;
$message = ‘A new order has been completed.\n’; $message .= ‘Order ID: ‘.$order_id.‘\n’; $message .= ‘Coupons used:\n’;
foreach( $order->get_used_coupons() as $coupon) { $message .= $coupon.‘\n’; } @wp_mail( $to, $subject, $message, $headers ); } } add_action( ‘woocommerce_thankyou’, ‘woo_email_order_coupons’ ); |
11. Thay đổi số lượng sản phẩm liên quan
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
/** * WooCommerce Extra Feature * ————————– * * Change number of related products on product page * Set your own value for ‘posts_per_page’ * */ function woo_related_products_limit() { global $product;
$args = array( ‘post_type’ => ‘product’, ‘no_found_rows’ => 1, ‘posts_per_page’ => 6, ‘ignore_sticky_posts’ => 1, ‘orderby’ => $orderby, ‘post__in’ => $related, ‘post__not_in’ => array($product->id) ); return $args; } add_filter( ‘woocommerce_related_products_args’, ‘woo_related_products_limit’ ); |
12. Không cho hiển thị sản phẩm trong category nào đó ở trang Shop
Thay chữ shoes thành slug của category sản phẩm mà bạn cần loại bỏ, có thể thêm nhiều bằng cách điền như array( ‘shoes’, ‘computer’ ).
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
/** * Remove products from shop page by category * */ function woo_custom_pre_get_posts_query( $q ) {
if ( ! $q->is_main_query() ) return; if ( ! $q->is_post_type_archive() ) return;
if ( ! is_admin() && is_shop() ) {
$q->set( ‘tax_query’, array(array( ‘taxonomy’ => ‘product_cat’, ‘field’ => ‘slug’, ‘terms’ => array( ‘shoes’ ), // Don’t display products in the shoes category on the shop page ‘operator’ => ‘NOT IN’ )));
}
remove_action( ‘pre_get_posts’, ‘custom_pre_get_posts_query’ );
} add_action( ‘pre_get_posts’, ‘woo_custom_pre_get_posts_query’ ); |
13. Tắt các tab như Review, Description trong sản phẩm
01 02 03 04 05 06 07 08 09 10 11 12 13 14 |
/** * Remove product tabs * */ function woo_remove_product_tab($tabs) {
unset( $tabs[‘description’] ); // Remove the description tab unset( $tabs[‘reviews’] ); // Remove the reviews tab unset( $tabs[‘additional_information’] ); // Remove the additional information tab
return $tabs;
} add_filter( ‘woocommerce_product_tabs’, ‘woo_remove_product_tab’, 98); |
14. Thay chữ “Free” thành một chữ bất kỳ
01 02 03 04 05 06 07 08 09 10 11 12 |
/** * WooCommerce Extra Feature * ————————– * * Replace “Free!” by a custom string * */ function woo_my_custom_free_message() { return “Liên hệ để lấy giá”; }
add_filter(‘woocommerce_free_price_html’, ‘woo_my_custom_free_message’); |
15. Sửa thông báo sau khi thêm vào giỏ
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
/** * Custom Add To Cart Messages * Add this to your theme functions.php file **/ add_filter( ‘woocommerce_add_to_cart_message’, ‘custom_add_to_cart_message’ ); function custom_add_to_cart_message() { global $woocommerce;
// Output success messages 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’), __(‘Product successfully added to your cart.’, ‘woocommerce’) );
else :
$message = sprintf(‘<a href=”%s” class=”button”>%s</a> %s’, get_permalink(woocommerce_get_page_id(‘cart’)), __(‘View Cart →’, ‘woocommerce’), __(‘Product successfully added to your cart.’, ‘woocommerce’) );
endif;
return $message; } |
16. Tự thêm tỉnh/thành (States)
01 02 03 04 05 06 07 08 09 10 11 12 13 14 |
/** * Code goes in functions.php or a custom plugin. Replace XX with the country code your changing. */ add_filter( ‘woocommerce_states’, ‘custom_woocommerce_states’ );
function custom_woocommerce_states( $states ) {
$states[‘XX’] = array( ‘XX1’ => ‘State 1’, ‘XX2’ => ‘State 2’ );
return $states; } |
17. Thay số lượng ảnh thumbnail trong sản phẩm
01 02 03 04 |
add_filter ( ‘woocommerce_product_thumbnails_columns’, ‘xx_thumb_cols’ ); function xx_thumb_cols() { return 4; // .last class applied to every 4th thumbnail } |
18. Hiển thị ảnh đại diện cho category sản phẩm ở trang category
01 02 03 04 05 06 07 08 09 10 11 12 |
add_action( ‘woocommerce_archive_description’, ‘woocommerce_category_image’, 2 ); function woocommerce_category_image() { if ( is_product_category() ){ global $wp_query; $cat = $wp_query->get_queried_object(); $thumbnail_id = get_woocommerce_term_meta( $cat->term_id, ‘thumbnail_id’, true ); $image = wp_get_attachment_url( $thumbnail_id ); if ( $image ) { echo ‘<img src=”‘ . $image . ‘” alt=”” />’; } } } |
19. Ví dụ của vòng lặp hiển thị sản phẩm
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 |
<ul class=“products”> <?php $args = array( ‘post_type’ => ‘product’, ‘posts_per_page’ => 12 ); $loop = new WP_Query( $args ); if ( $loop->have_posts() ) { while ( $loop->have_posts() ) : $loop->the_post(); woocommerce_get_template_part( ‘content’, ‘product’ ); endwhile; } else { echo __( ‘No products found’ ); } wp_reset_postdata(); ?> </ul>
<!–/.products–> |
20. Ẩn sản phẩm liên quan
01 02 03 04 05 06 07 08 09 10 |
/* * wc_remove_related_products * * Clear the query arguments for related products so none show. * Add this code to your theme functions.php file. */ function wc_remove_related_products( $args ) { return array(); } add_filter(‘woocommerce_related_products_args’,‘wc_remove_related_products’, 10); |
21. Xóa menu tùy chỉnh kiểu sắp xếp sản phẩm
01 |
remove_action( ‘woocommerce_before_shop_loop’, ‘woocommerce_catalog_ordering’, 30 ); |
22. Ảnh sản phẩm bo tròn
Cái này thì thêm vào file style.css trong theme:
01 |
.woocommerce .woocommerce-tabs {border: 1px solid #e6e6e6} |
23. Đưa mô tả sản phẩm vào bên dưới ảnh sản phẩm
01 |
add_action(‘woocommerce_after_shop_loop_item_title’,‘woocommerce_template_single_excerpt’, 5); |
24. Tắt chức năng mã SKU
01 |
add_filter( ‘wc_product_sku_enabled’, ‘__return_false’ ); |
25. Dịch Breadcrumb trong Giỏ hàng
SHOPPING CART -> CHECKOUT DETAILS -> ORDER COMPLETE
1 2 3 4 5 6 7 8 9 10 11 |
// Translate Shopping Cart Breadcrumb add_filter( ‘gettext’, function ( $strings ) {
$text = array( ‘SHOPPING CART’ => ‘Giỏ hàng’, ‘CHECKOUT DETAILS’ => ‘Thanh toán’, ‘ORDER COMPLETE’ => ‘Hoàn tất’, ); $strings = str_ireplace( array_keys( $text ), $text, $strings ); return $strings; }, 20 ); |
26. Sửa ký hiệu tiền tệ Đ sang VND
// Change currency symbols
add_filter( ‘woocommerce_currencies’, ‘add_my_currency’ );
function add_my_currency( $currencies )
{
$currencies[‘VND’] = __( ‘Vietnam Dong’, ‘woocommerce’ );
return $currencies;
}
add_filter(‘woocommerce_currency_symbol’, ‘add_my_currency_symbol’, 10, 2);
function add_my_currency_symbol( $currency_symbol, $currency ) {
switch( $currency ) {
case ‘VND’: $currency_symbol = ‘
Còn nhiều đoạn code khác liên quan đến Woocommerce sẽ giúp bạn có thể chỉnh sửa được gian hàng của mình phù hợp, giúp việc bán hàng tốt hơn.