Woocommerce là plugin quản lý bán hàng trên wordpress được dùng phổ biến nhất hiện nay. Để phù hợp với thị trường Việt Nam thường chúng ta thường phải tùy chỉnh rất nhiều thứ mới dùng được. Bài viết này mình tổng hợp tất cả các đoạn code hay dùng cho những nhiệm vụ cụ thể để các bạn tham khảo.
Bỏ các trường không cần thiết trong trang check out (thanh toán)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/** codfe.com bỏ các trường ko cần trong trang checkout------------ **/ function cs_woocommerce_remote_billing_fields( $fields ) { unset( $fields['billing_last_name'] ); //unset( $fields['billing_phone'] ); //unset( $fields['billing_address_1'] ); unset( $fields['billing_address_2'] ); unset( $fields['billing_city'] ); unset( $fields['billing_postcode'] ); unset( $fields['billing_state'] ); //unset( $fields['billing_country'] ); //unset( $fields['billing_company'] ); $fields['billing_email']['class'] = array( 'form-row-wide' ); return $fields; } add_filter( 'woocommerce_billing_fields', 'cs_woocommerce_remote_billing_fields' ); |
Bỏ bắt buộc nhập và xóa trường billing_address_2 và shipping_address_2
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 28 29 30 |
// Do NOT include the opening php tag. // Place in your theme's functions.php file // Set address 2 to not required add_filter('woocommerce_checkout_fields', 'unrequire_address_2_checkout_fields' ); function unrequire_address_2_checkout_fields( $fields ) { $fields['billing']['billing_address_2']['required'] = false; $fields['shipping']['shipping_address_2']['required'] = false; return $fields; } // Remove billing address 2 from Checkout for WooCommerce add_filter('cfw_get_billing_checkout_fields', 'remove_billing_address_2_checkout_fields', 100, 3); function remove_billing_address_2_checkout_fields( $fields ) { unset($fields['billing_address_2']); return $fields; } // Remove shipping address 2 from Checkout for WooCommerce add_filter('cfw_get_shipping_checkout_fields', 'remove_shipping_address_2_checkout_fields', 100, 3); function remove_shipping_address_2_checkout_fields( $fields ) { unset($fields['shipping_address_2']); return $fields; } |
Ẩn các title field và thêm placeholder cho các trường trong trang Checkout
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 |
/** codfe.com Gỡ bỏ các title field trong trang checkout------------------ **/ // WooCommerce Checkout Fields Hook add_filter('woocommerce_checkout_fields','custom_wc_checkout_fields_no_label'); // Our hooked in function - $fields is passed via the filter! // Action: remove label from $fields function custom_wc_checkout_fields_no_label($fields) { // loop by category foreach ($fields as $category => $value) { // loop by fields foreach ($fields[$category] as $field => $property) { // remove label property unset($fields[$category][$field]['label']); } } return $fields; } /** codfe.com Thêm Placeholder (gợi ý) cho checkout field ------------------------- **/ add_filter( 'woocommerce_checkout_fields' , 'override_billing_checkout_fields', 20, 1 ); function override_billing_checkout_fields( $fields ) { $fields['billing']['billing_first_name']['placeholder'] = 'Họ và tên'; $fields['billing']['billing_phone']['placeholder'] = 'Điện thoại'; $fields['billing']['billing_address_1']['placeholder'] = 'Địa chỉ'; $fields['billing']['billing_email']['placeholder'] = 'Địa chỉ Email'; return $fields; } |
Sắp xếp thứ tự các trường trong trang Checkout
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
/** Codfe.com Sắp xếp các trường ------------------------------------------------------------- **/ add_filter("woocommerce_checkout_fields", "new_order_fields"); function new_order_fields($fields) { $order = array( "billing_first_name", "billing_phone", "billing_address_1", "billing_email" ); foreach( $order as $field ) { $ordered_fields[$field] = $fields["billing"][$field]; } $fields["billing"] = $ordered_fields; return $fields; } |
Liệt kê các sản phẩm bán chéo Cross Sale

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 28 29 30 31 32 |
// lây danh sach cac san pham cross sale function show_cross_sell_in_single_product(){ $crosssells = get_post_meta( get_the_ID(), '_crosssell_ids',true); if(empty($crosssells)){ return; } $args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'post__in' => $crosssells ); $products = new WP_Query( $args ); ob_start(); if( $products->have_posts() ) : echo '<div class="cross-sells"><h2>Sản phẩm khác</h2>'; woocommerce_product_loop_start(); while ( $products->have_posts() ) : $products->the_post(); wc_get_template_part( 'content', 'product' ); endwhile; // end of the loop. woocommerce_product_loop_end(); echo '</div>'; endif; wp_reset_postdata(); $content = ob_get_contents(); ob_end_clean(); return $content; } add_shortcode( 'tp_cross_sale', 'show_cross_sell_in_single_product' ); |
Liệt kê các sản phẩm bán kèm Up Sale
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 28 29 30 31 32 33 34 |
// lây danh sach cac san pham up sale function show_up_sell_in_single_product(){ $upsells = get_post_meta( get_the_ID(), '_upsell_ids',true); if(empty($upsells)){ return; } $args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'post__in' => $upsells ); $products = new WP_Query( $args ); ob_start(); if( $products->have_posts() ) : echo '<div class="cross-sells"><h2>Sản phẩm liên quan</h2>'; woocommerce_product_loop_start(); while ( $products->have_posts() ) : $products->the_post(); wc_get_template_part( 'content', 'product' ); endwhile; // end of the loop. woocommerce_product_loop_end(); echo '</div>'; endif; wp_reset_postdata(); $content = ob_get_contents(); ob_end_clean(); return $content; } // lây danh sach cac san pham cross sale add_shortcode( 'hwp_up_sale', 'show_up_sell_in_single_product' ); |
Di chuyển sản phẩm hết hàng xuống cuối danh mục
1 2 3 4 5 6 7 8 9 10 11 |
add_filter('posts_clauses', 'hwp_sort_order_by_stock_status', 1000); function hwp_sort_order_by_stock_status($posts_clauses) { global $wpdb; if (is_woocommerce() && (is_shop() || is_product_category() || is_product_tag())) { $posts_clauses['join'] .= " INNER JOIN $wpdb->postmeta istockstatus ON ($wpdb->posts.ID = istockstatus.post_id) "; $posts_clauses['orderby'] = " istockstatus.meta_value ASC, " . $posts_clauses['orderby']; $posts_clauses['where'] = " AND istockstatus.meta_key = '_stock_status' AND istockstatus.meta_value <> '' " . $posts_clauses['where']; } return $posts_clauses; } |
Tự thêm sản phẩm vào giỏ mỗi khi khách truy cập vào
Bạn đang muốn giới thiệu sản phẩm mới hoặc khuyến mãi thêm sản phẩm cho người mua hàng.
Bằng cách tự động thêm sản phẩm vào giỏ hàng khi người dùng truy cập vào website.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
add_action( 'init', 'add_product_to_cart' ); 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 ); } } } |
Trường hợp thêm sản phẩm khuyến mãi, khi người mua hàng có giá trị đơn hàng lớn hoặc theo quy định của chủ shop với giá trị đó sẽ được khuyến mãi thêm sản phẩm chẳng hạn.
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 |
add_action( 'init', 'add_product_to_cart' ); function add_product_to_cart() { if ( ! is_admin() ) { global $woocommerce; $product_id = 2831; $found = false; $cart_total = 30; if( $woocommerce->cart->total >= $cart_total ) { //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 ); } } } } |
Thay đổi ký tự tiền tệ Đồng Việt Nam
1 2 3 4 5 6 7 8 |
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 = 'VNĐ'; break; } return $currency_symbol; } |
Thay chữ “Add to Cart” hoặc “Thêm vào giỏ”
1 2 3 4 5 6 7 8 9 10 |
// To change add to cart text on single product page add_filter( 'woocommerce_product_single_add_to_cart_text', 'woocommerce_custom_single_add_to_cart_text' ); function woocommerce_custom_single_add_to_cart_text() { return __( 'Buy Now', 'woocommerce' ); } // To change add to cart text on product archives(Collection) page add_filter( 'woocommerce_product_add_to_cart_text', 'woocommerce_custom_product_add_to_cart_text' ); function woocommerce_custom_product_add_to_cart_text() { return __( 'Buy Now', 'woocommerce' ); } |
Thêm sản phẩm không có giá vào giỏ hàng
1 2 3 4 5 6 7 |
function hwp_theme_custom_woocommerce_is_purchasable_filter( $can, $product ) { if ( '' == $product->get_price() ) { $can = $product->exists() && ( 'publish' === $product->get_status() || current_user_can( 'edit_post', $product->get_id() ) ); } return $can; } add_filter( 'woocommerce_is_purchasable', 'hwp_theme_custom_woocommerce_is_purchasable_filter', 10, 2 ); |
Sau khi áp dụng bộ lọc woocommerce_is_purchasable
như trên thì bạn đã có thể cho sản phẩm không có giá vào giỏ hàng. Tuy nhiên hệ thống sẽ báo lỗi khi tính thành tiền vì hàm lấy số lượng nhân đơn giá để ra tổng.
1 2 3 4 5 6 7 |
function hwp_theme_wc_product_data_filter( $value, $data ) { if ( empty( $value ) ) { $value = 0; } return $value; } add_filter( 'woocommerce_product_get_price', 'hwp_theme_wc_product_data_filter', 10, 2 ); |
Thay đổi số lượng sản phẩm liên quan
1 2 3 4 5 6 7 8 9 10 11 12 13 |
function woo_related_products_limit() { global $product; $args['posts_per_page'] = 6; return $args; } add_filter( 'woocommerce_output_related_products_args', 'hwp_related_products_args' ); function hwp_related_products_args( $args ) { $args['posts_per_page'] = 4; // 4 related products $args['columns'] = 2; // arranged in 2 columns return $args; } |
Xóa các Tabs trong single-product
1 2 3 4 5 6 7 |
add_filter( 'woocommerce_product_tabs', 'woo_remove_product_tabs', 98 ); function woo_remove_product_tabs( $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; } |
Sửa thông báo sau khi thêm vào giỏ
1 2 3 4 5 6 7 8 9 10 |
// Custom Cart Message add_filter( 'woocommerce_add_to_cart_message', 'custom_add_to_cart_message' ); function custom_add_to_cart_message ($message) { global $woocommerce; // Output success messages $custom_message = sprintf('%s %s', __('Product successfully added to your ', 'woocommerce'), get_permalink(woocommerce_get_page_id('cart')), __('Briefcase', 'woocommerce')); global $is_cart_added; $is_cart_added = 1; return $custom_message; } |
Hiển thị ảnh đại diện cho chuyên mục sản phẩm
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//display category image on category page 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 style="margin: 0px 10px 10px 0px;" class="category-image" src="' . $image . '" alt="" />'; } } } |
Hiển thị (sale flash) % chiết khấu cho sản phẩm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
add_filter( 'woocommerce_get_price_html', 'change_displayed_sale_price_html', 10, 2 ); function change_displayed_sale_price_html( $price, $product ) { // Only on sale products on frontend and excluding min/max price on variable products if( $product->is_on_sale() && ! is_admin() && ! $product->is_type('variable')){ // Get product prices $regular_price = (float) $product->get_regular_price(); // Regular price $sale_price = (float) $product->get_price(); // Active price (the "Sale price" when on-sale) // "Saving price" calculation and formatting $saving_price = wc_price( $regular_price - $sale_price ); // "Saving Percentage" calculation and formatting $precision = 1; // Max number of decimals $saving_percentage = round( 100 - ( $sale_price / $regular_price * 100 ), 1 ) . '%'; // Append to the formated html price $price .= sprintf( __('<p class="saved-sale">Save: %s <em>(%s)</em></p>', 'woocommerce' ), $saving_price, $saving_percentage ); } return $price; } |
***********Còn nhiều lắm, cập nhật dần dần dài quá rồi !