Học WordPress + AI / WordPress cho người VIỆT !
WPShare247

Học wordpress cho người mới bắt đầu

  • Bắt đầu
    • Học WordPress
    • Elementor
    • WooCommerce
    • Tạo WordPress
    • Học WordPress kiểu mới 2025
    • Học PHP cơ bản
    • Tạo Website bằng WordPress
    • Quản trị WordPress
    • Sửa lỗi WordPress
    • Theme wordpress
    • Plugin WordPress
    • Hàm PHP
    • Seo WordPress
    • Bảo mật WordPress
    • Html & Css
    • Javascript và jQuery
    • Hướng dẫn lập trình
  • Video
    • Video TikTok
    • Video Shorts
  • Kho plugin
  • Kho Prompt
  • Tải Web Miễn Phí
  • Công cụ
    • Kiểm tra web WordPress
    • WordPress theme gì?
    • Check IP Website
    • Kiểm tra Tên Miền
    • Kiểm Tra SSL
    • Tạo File Disavow Google
    • Tạo QR Code
  • Giới thiệu
    • Khuyến Mãi
  • Đăng nhập
X
☰

Trang chủ » Cách áp dụng các tính năng mới của WooCommerce 2025 vào cửa hàng WordPress của bạn

Cách áp dụng các tính năng mới của WooCommerce 2025 vào cửa hàng WordPress của bạn

Ngày đăng: Xem: 1429

Hướng dẫn sử dụng Plugin WooCommerce WordPress từ A đến Z

Hướng dẫn tạo Product Data Tabs trong WooCommerce – WordPress Tutorial

WooCommerce 2025 mang đến hàng loạt thay đổi quan trọng trong kiến trúc và cách hoạt động của hệ thống bán hàng trên WordPress — từ HPOS (High-Performance Order Storage), Product Blocks thế hệ mới, cho tới REST API mở rộng và Template Registration API.

Nếu bạn là developer hoặc quản trị viên đang muốn cập nhật cửa hàng của mình lên phiên bản mới nhất, thì những đoạn code dưới đây sẽ giúp bạn thích nghi nhanh, mở rộng đúng hướng, và tận dụng toàn bộ tính năng của WooCommerce 2025.

💡 Bạn có thể sử dụng ngay các đoạn code này bằng cách chèn trực tiếp vào file
functions.php của theme đang dùng hoặc tạo plugin nhỏ riêng cho dự án của mình (khuyến khích cách thứ hai để dễ bảo trì).

1. Phát hiện WYCO (HPOS) và xử lý khi bật / tắt

WooCommerce 2025 khuyến khích sử dụng HPOS (High-Performance Order Storage). Bạn có thể cần kiểm tra xem site đang dùng HPOS để xử lý tương thích.

/**
 * Kiểm tra nếu HPOS đang được bật
 */
function wpshare247_is_hpos_active() {
    if ( defined( 'WC_USE_HPOS' ) ) {
        return (bool) WC_USE_HPOS;
    }
    // fallback: kiểm tra option nếu có
    return ( 'yes' === get_option( 'woocommerce_hpos_enabled', 'no' ) );
}

// Ví dụ sử dụng
if ( wpshare247_is_hpos_active() ) {
    // logic xử lý cho HPOS (vd. truy vấn bảng mới, tránh truy vấn postmeta cũ)
}

Mô tả:

  • Đây là đoạn code giúp bạn kiểm tra xem WooCommerce đang sử dụng hệ thống lưu đơn hàng HPOS hay không.
  • Từ đó bạn có thể chuyển logic lấy dữ liệu order / meta từ bảng mới thay vì postmeta truyền thống.

2. Hook / filter di chuyển (migrated legacy hooks)

WooCommerce 10+ “di chuyển” nhiều hook legacy vào hệ thống block / Blocks API để thích nghi với block-based rendering. (The WooCommerce Developer Blog)

Ví dụ: woocommerce_add_to_cart, woocommerce_before_shop_loop, woocommerce_after_shop_loop, woocommerce_loop_add_to_cart_link, … được liệt kê trong danh sách legacy hooks đã được “migrated”. (The WooCommerce Developer Blog)

Bạn có thể viết wrapper để đảm bảo code custom bạn dùng các hook legacy vẫn tương thích:

/**
 * Wrapper để đảm bảo callback tương thích cả block + legacy
 */
function wpshare247_add_to_cart_wrapper( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
    // code bạn muốn thực thi
}

// nếu legacy hook tồn tại, dùng nó
add_action( 'woocommerce_add_to_cart', 'wpshare247_add_to_cart_wrapper', 10, 6 );

// nếu cần, bạn có thể dùng các hook block / store API tương ứng (tùy plugin / block)

Mô tả:

  • WooCommerce 2025 chuyển khá nhiều hook “cũ” vào hệ thống block / data-store.
  • Việc dùng wrapper này giúp bạn “bắt” callback ở cả môi trường sử dụng block mới hoặc môi trường cũ.

3. Block — mẫu Product Collection Carousel & Cross-Sells block mới

Trong WooCommerce 10.2, họ giới thiệu Product Collection block dạng carousel và block Cross-Sells dùng Product Collection block (không dùng block cũ Cart Cross-Sells) (The WooCommerce Developer Blog)

Bạn có thể mở rộng hoặc đè block này như:

/**
 * Đăng ký block variation hoặc transform block mới
 */
add_action( 'init', 'wpshare247_register_custom_collection_block' );
function wpshare247_register_custom_collection_block() {
    if ( ! function_exists( 'register_block_variation' ) ) {
        return;
    }
    register_block_variation(
        'woocommerce/product-collection',
        array(
            'name'        => 'wpshare247_carousel',
            'title'       => 'Carousel Layout',
            'variations'  => array(
                'layout' => 'carousel',
            ),
        )
    );
}

Mô tả:

  • Cho phép bạn mở rộng block “Product Collection” để có layout carousel riêng (ví dụ style “mặc định” + carousel).
  • Hoặc bạn có thể cung cấp transform từ block cũ Cross-Sells → block mới để di chuyển nội dung.

4. Skeleton loading / partial rendering Checkout & Cart

WooCommerce 10.2 có cải thiện trải nghiệm load cho Cart / Checkout block: sử dụng skeleton placeholders khi dữ liệu thực tải sau, giúp UI mượt hơn (The WooCommerce Developer Blog)

Bạn có thể chèn script / filter để tuỳ chỉnh placeholder hoặc trạng thái loading:

/**
 * Add custom skeleton placeholder class
 */
function wpshare247_modify_cart_skeleton_placeholders( $attributes ) {
    // ví dụ: thêm class CSS riêng
    if ( isset( $attributes['className'] ) ) {
        $attributes['className'] .= ' wpshare247-cart-skeleton';
    } else {
        $attributes['className'] = 'wpshare247-cart-skeleton';
    }
    return $attributes;
}
// filter tên giả định, bạn phải tìm tên đúng trong block attributes hook
add_filter( 'woocommerce_cart_block_skeleton_attributes', 'wpshare247_modify_cart_skeleton_placeholders' );

Mô tả:

  • Khi block Cart / Checkout đang load dữ liệu (AJAX), nó hiển thị placeholder tạm.
  • Bạn có thể chỉnh CSS / class để style riêng — như màu, hiệu ứng — để đồng bộ giao diện.

5. Đăng ký REST / Store API mở rộng cho filters / bộ lọc taxonomy mới

WooCommerce 10+ thêm block Taxonomy Filter blocks cho Category, Brand, Tag (The WooCommerce Developer Blog)
Bạn có thể mở rộng API để trả dữ liệu bổ sung (ví dụ: số sản phẩm, metadata taxonomy) qua REST:

/**
 * Thêm field metadata cho taxonomy category/brand trong REST
 */
add_action( 'rest_api_init', 'wpshare247_register_taxonomy_meta' );
function wpshare247_register_taxonomy_meta() {
    register_rest_field(
        'product_cat',
        'wpshare247_extra_info',
        array(
            'get_callback' => 'wpshare247_get_tax_meta',
            'schema'       => array(
                'description' => 'Thông tin bổ sung cho taxonomy product category',
                'type'        => 'string',
                'context'     => array( 'view', 'edit' ),
            ),
        )
    );
}

function wpshare247_get_tax_meta( $object ) {
    $term_id = $object['id'];
    return get_term_meta( $term_id, 'wpshare247_extra_info', true );
}

Mô tả:

  • Bạn mở rộng REST API (và theo đó Store API hoặc block data) để taxonomy như category / brand có thêm metadata do bạn định nghĩa.
  • Khi người dùng lọc theo taxonomy trong block Filter, bạn có thể cung cấp dữ liệu bổ sung.

6. Tối ưu template registration API — block template registration

WooCommerce 2025 áp dụng Template Registration API của WordPress để block templates (Single Product, Products by Category, Tag, Brand) có logic rõ ràng hơn, dễ override / customize khi chuyển giữa theme / revert template. (The WooCommerce Developer Blog)

Một ví dụ mẫu bạn có thể dùng để đăng ký template override riêng:

/**
 * Đăng ký template override cho Single Product template custom
 */
function wpshare247_register_product_template() {
    if ( function_exists( 'register_block_template' ) ) {
        register_block_template(
            'woocommerce/single-product',
            array(
                'path' => plugin_dir_path( __FILE__ ) . 'templates/wpshare247-single-product.html',
                'type' => 'full',
            )
        );
    }
}
add_action( 'init', 'wpshare247_register_product_template' );

Mô tả:

  • Với block template registration API, bạn có đăng ký template HTML / block cho các loại page WooCommerce.
  • Khi theme bị đổi, template vẫn tồn tại, và theme có thể override dễ hơn.

 

Shares
ChatGPT ChatGPT
Share
Share
Pin
Về WPShare247

Học wordpress cho người mới bắt đầu.

WPShare247 là blog chia sẻ các kiến thức về WordPress, sửa chữa lỗi wordpress, giới thiệu theme và plugin wordpress mới nhất. Đào tạo khóa học thiết kế Website bằng WordPress.

Menu
  • Trang chủ
  • Giới thiệu
  • Blog
  • Plugin WordPress
  • Liên hệ
Chủ đề wordpress
  • Sửa lỗi WordPress
  • Plugin WordPress
  • Theme wordpress
  • Seo WordPress
  • WooCommerce

Copyright © 2026 WPSHARE247. All Rights Reserved.
Một sản phẩm của WEB 366

  • Thiết kế web bởi Website366.com
  • Thiết kế website chuyên nghiệp TBAY.VN
  • Thiết kế WEB AI