Phần mô tả mặc định của danh mục sản phẩm khá là đơn giản và thô sơ, cũng không có sẵn trình soạn thảo WYSIWYG để dễ dàng thêm các nội dung vào. Vì vậy trong bài viết này mình sẽ hướng dẫn bạn thêm 1 trường mô tả vào phần danh mục sản phẩm với đầy đủ tool nhé.
Thêm code vào file functions.php
Đầu tiền chỉ cần copy code sau và thêm vào file functions.php của child theme bạn đang xài là được.
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
function replace_product_cat_desc_editor( $term ) { ?> <script> jQuery(document).ready(function($) { $('#tag-description').closest('.form-field').hide(); }); </script> <?php // Lấy nội dung mô tả hiện tại $description = ( isset( $term->description ) ) ? $term->description : ''; ?> <tr class="form-field term-description-wrap"> <th scope="row"><label for="description">Mô tả</label></th> <td> <?php // Hiển thị trình soạn thảo WYSIWYG wp_editor( htmlspecialchars_decode( $description ), 'tag_description', array( 'textarea_name' => 'description', 'media_buttons' => true, 'textarea_rows' => 10, 'teeny' => false, 'quicktags' => true, 'editor_class' => 'full-text-editor', ) ); ?> <p class="description">Phần mô tả chi tiết hiển thị trên trang danh mục sản phẩm.</p> </td> </tr> <?php } // Dùng cho trang chỉnh sửa chuyên mục sản phẩm add_action( 'product_cat_edit_form_fields', 'replace_product_cat_desc_editor', 10, 1 ); remove_filter( 'pre_term_description', 'wp_filter_kses' ); remove_filter( 'term_description', 'wp_kses_data' ); function save_product_cat_desc_editor( $term_id ) { if ( isset( $_POST['description'] ) ) { $data = array( 'description' => wp_kses_post( $_POST['description'] ) ); wp_update_term( $term_id, 'product_cat', $data ); } } add_action( 'edited_product_cat', 'save_product_cat_desc_editor' ); add_action( 'create_product_cat', 'save_product_cat_desc_editor' ); |
Sau khi thêm vào là xong nhé, bình thường nội dung mô tả danh mục sản phẩm sẽ nằm bên trên danh sách sản phẩm trong trang danh mục nhé.