Tôi Thích Kiếm Tiền

24 đoạn code hay cho Woocommerce



Mục lục nội dung

  1. 1. Xóa chữ “Product” trên thanh điều hướng
  2. 2. Tự thêm sản phẩm vào giỏ mỗi khi khách truy cập vào
  3. 3. Thêm ký hiệu tiền tệ theo ý mình
  4. 4. Thay chữ “Add to Cart” hoặc “Thêm vào giỏ”
  5. 5. Tự chuyển tới trang thanh toán sau khi thêm vào giỏ
  6. 6. Gửi email sau khi thanh toán bằng coupon
  7. 7. Thay đổi số lượng sản phẩm liên quan
  8. 8. Không cho hiển thị sản phẩm trong category nào đó ở trang Shop
  9. 9. Tắt các tab như Review, Description trong sản phẩm
  10. 10. Thay chữ “Free” thành một chữ bất kỳ
  11. 11. Ẩ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
  12. 12. Sửa thông báo sau khi thêm vào giỏ
  13. 13. Tự thêm tỉnh/thành (States)
  14. 14. Thay số lượng ảnh thumbnail trong sản phẩm
  15. 15. Hiển thị ảnh đại diện cho category sản phẩm ở trang category
  16. 16. Sửa đường link nút “Add to Cart”
  17. 17. Kéo dài field nhập địa chỉ
  18. 18. Ví dụ của vòng lặp hiển thị sản phẩm
  19. 19. Sửa số lượng sản phẩm hiển thị ở mỗi trang
  20. 20. Ẩn sản phẩm liên quan
  21. 21. Xóa menu tùy chỉnh kiểu sắp xếp sản phẩm
  22. 22. Ảnh sản phẩm bo tròn
  23. 23. Đưa mô tả sản phẩm vào bên dưới ảnh sản phẩm
  24. 24. Tắt chức năng mã SKU
Bài này chưa được cập nhật cho WooCommerce ở bản mới nhất. Mình sẽ cập nhật lại sau.

Ngoài những themes đẹp dành cho Woocommerce và các plugin để mở rộng chức năng của nó ra, thì chúng ta có thể tận dụng những cái hook có sẵn của nó để tùy biến lại trang bán hàng theo ý của mình. Nếu như bạn đang dùng Woocommerce, sẽ thật có ích nếu bạn xem qua danh sách những đoạn code dưới đây xem có cái nào phù hợp với nhu cầu của bạn hay không, cách sử dụng chỉ là copy và paste vào file functions.php trong theme bạn đang sử dụng.

1. Xóa chữ “Product” trên thanh điều hướng

/* * 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 );

2. 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ỏ.

/* * 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 cartif ( 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 itif ( ! $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' );

3. Thêm ký hiệu tiền tệ theo ý mình

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;}

4. Thay chữ “Add to Cart” hoặc “Thêm vào giỏ”

/** * Change the add to cart text on single product pages */function woo_custom_cart_button_text() {return __('My Button Text', 'woocommerce');}add_filter('single_add_to_cart_text', 'woo_custom_cart_button_text');/** * Change the add to cart text on product archives */function woo_archive_custom_cart_button_text() {return __( 'My Button Text', 'woocommerce' );}add_filter( 'add_to_cart_text', 'woo_archive_custom_cart_button_text' );

5. 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.

6. Gửi email sau khi thanh toán bằng coupon

/** * 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 = '[email protected]'; $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' );

7. Thay đổi số lượng sản phẩm liên quan

/** * 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' );

8. 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' ).

 /** * 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' );

9. Tắt các tab như Review, Description trong sản phẩm

 /** * 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);

10. Thay chữ “Free” thành một chữ bất kỳ

/** * 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');

11. Ẩ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

// Hide ALL shipping options when free shipping is availableadd_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 arrayunset( $available_methods );// Add Free Shipping back into $avaialble_methods$available_methods = array();$available_methods[] = $freeshipping;endif;return $available_methods;}

12. Sửa thông báo sau khi thêm vào giỏ

/** * 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 messagesif (get_option('woocommerce_cart_redirect_after_add')=='yes') :$return_to = get_permalink(woocommerce_get_page_id('shop'));$message = sprintf('<a href="%s" >%s</a> %s', $return_to, __('Continue Shopping &rarr;', 'woocommerce'), __('Product successfully added to your cart.', 'woocommerce') );else :$message = sprintf('<a href="%s" >%s</a> %s', get_permalink(woocommerce_get_page_id('cart')), __('View Cart &rarr;', 'woocommerce'), __('Product successfully added to your cart.', 'woocommerce') );endif;return $message;}

13. Tự thêm tỉnh/thành (States)

/** * 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;}

14. Thay số lượng ảnh thumbnail trong sản phẩm

add_filter ( 'woocommerce_product_thumbnails_columns', 'xx_thumb_cols' ); function xx_thumb_cols() { return 4; // .last class applied to every 4th thumbnail }

15. Hiển thị ảnh đại diện cho category sản phẩm ở trang category

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="" />';}}}

16. Sửa đường link nút “Add to Cart”

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') );}

17. Kéo dài field nhập địa chỉ

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;}

18. Ví dụ của vòng lặp hiển thị sản phẩm

<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-->

19. Sửa số lượng sản phẩm hiển thị ở mỗi trang

// Display 24 products per page. Goes in functions.phpadd_filter( 'loop_shop_per_page', create_function( '$cols', 'return 24;' ), 20 );

20. Ẩn sản phẩm liên quan

/* * 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

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:

.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

add_action('woocommerce_after_shop_loop_item_title','woocommerce_template_single_excerpt', 5);

24. Tắt chức năng mã SKU

add_filter( 'wc_product_sku_enabled', '__return_false' );

Còn khá nhiều chức năng hay ho khác nhưng các chức năng phức tạp thì nên dùng plugin sẽ hay hơn. Mình sẽ có một bài tổng hợp plugin có ích cho Woocommerce sau nhé.

24 đoạn code hay cho Woocommerce was last modified: Tháng Mười Một 20th, 2015 by Thạch Phạm
woocommercewordpress snippet

Categories