Bạn cần thêm mã vào tệp chủ đề con của mình hoặc thông qua một plugin cho phép thêm các chức năng tùy chỉnh, chẳng hạn như plugin Đoạn mã. Vui lòng không thêm mã tùy chỉnh trực tiếp vào tệp chủ đề mẹ của bạn vì mã này sẽ bị xóa hoàn toàn khi bạn cập nhật chủ đề.functions.php
functions.php
Xóa các tab
Sử dụng đoạn mã sau để xóa các tab cụ thể
/**
* Remove product data tabs
*/
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;
}
Đổi tên các tab
Sử dụng đoạn mã sau để đổi tên các tab.
/**
* Rename product data tabs
*/
add_filter( 'woocommerce_product_tabs', 'woo_rename_tabs', 98 );
function woo_rename_tabs( $tabs ) {
$tabs['description']['title'] = __( 'More Information' ); // Rename the description tab
$tabs['reviews']['title'] = __( 'Ratings' ); // Rename the reviews tab
$tabs['additional_information']['title'] = __( 'Product Data' ); // Rename the additional information tab
return $tabs;
}
Sắp xếp lại các tab
Sử dụng đoạn mã sau để thay đổi thứ tự tab
/**
* Reorder product data tabs
*/
add_filter( 'woocommerce_product_tabs', 'woo_reorder_tabs', 98 );
function woo_reorder_tabs( $tabs ) {
$tabs['reviews']['priority'] = 5; // Reviews first
$tabs['description']['priority'] = 10; // Description second
$tabs['additional_information']['priority'] = 15; // Additional information third
return $tabs;
}
Tùy chỉnh một tab
Đoạn mã sau sẽ thay thế tab mô tả bằng một chức năng tùy chỉnh
/**
* Customize product data tabs
*/
add_filter( 'woocommerce_product_tabs', 'woo_custom_description_tab', 98 );
function woo_custom_description_tab( $tabs ) {
$tabs['description']['callback'] = 'woo_custom_description_tab_content'; // Custom description callback
return $tabs;
}
function woo_custom_description_tab_content() {
echo '<h2>Custom Description</h2>';
echo '<p>Here\'s a custom description</p>';
}
Thêm một tab tùy chỉnh
Sử dụng đoạn mã sau để thêm tab sản phẩm toàn cầu tùy chỉnh
/**
* Add a custom product data tab
*/
add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
function woo_new_product_tab( $tabs ) {
// Adds the new tab
$tabs['test_tab'] = array(
'title' => __( 'New Product Tab', 'woocommerce' ),
'priority' => 50,
'callback' => 'woo_new_product_tab_content'
);
return $tabs;
}
function woo_new_product_tab_content() {
// The new tab content
echo '<h2>New Product Tab</h2>';
echo '<p>Here\'s your new product tab.</p>';
}
Tab Thông tin bổ sung
Xin lưu ý rằng tab " Thông tin bổ sung " sẽ chỉ hiển thị nếu sản phẩm có trọng lượng, kích thước hoặc thuộc tính (với “Hiển thị trên trang sản phẩm” được chọn). Nếu bạn cố gắng áp dụng thay đổi cho tab đó và nếu sản phẩm không có trọng lượng, kích thước hoặc thuộc tính, bạn sẽ nhận được thông báo lỗi tương tự như:
Cảnh báo : call_user_func () yêu cầu tham số 1 là một lệnh gọi lại hợp lệ, không có mảng hoặc chuỗi nào được cung cấp trong /mysite/wp-content/plugins/woocommerce/templates/single-product/tabs/tabs.php trên dòng 35
Trong trường hợp đó, bạn phải sử dụng các thẻ điều kiện WooCommerce:
has_attributes()
has_dimensions()
has_weight()
/**
* Check if product has attributes, dimensions or weight to override the call_user_func() expects parameter 1 to be a valid callback error when changing the additional tab
*/
add_filter( 'woocommerce_product_tabs', 'woo_rename_tabs', 98 );
function woo_rename_tabs( $tabs ) {
global $product;
if( $product->has_attributes() || $product->has_dimensions() || $product->has_weight() ) { // Check if product has attributes, dimensions or weight
$tabs['additional_information']['title'] = __( 'Product Data' ); // Rename the additional information tab
}
return $tabs;
}