我们如何删除、更改或添加新标签?
是的,一种选择是使用现有的插件,另一种选择是编写几行 PHP。我将在下面提供一些 PHP 片段,以便您可以将它们添加到您的 functions.php 文件并进行任何需要的自定义。
移除标签
/**
* Remove the pre orders link from the My Account page.
* @param $menu_links
* @return array|string[]
*/
function my_site_remove_my_account_links( $menu_links ) {
// Remove the pre-orders tab from the my account page.
// This tab is created by the plugin https://woocommerce.com/products/woocommerce-pre-orders/
unset( $menu_links['pre-orders'] );
// You can find below the default tabs.
//unset( $menu_links['dashboard'] ); // Remove Dashboard tab.
//unset( $menu_links['orders'] ); // Remove Orders tab.
//unset( $menu_links['edit-address'] ); // Addresses tab.
//unset( $menu_links['payment-methods'] ); // Remove Payment Methods tab.
//unset( $menu_links['edit-account'] ); // Remove Account details tab.
//unset( $menu_links['customer-logout'] ); // Remove Logout link tab.
return $menu_links;
}
add_filter ( 'woocommerce_account_menu_items', 'my_site_remove_my_account_links' );
更新标签的标题
/**
* Update the title of the tabs in the My Account page.
* @param $menu_links
* @return array|string[]
*/
function my_site_update_my_account_links( $menu_links ) {
// Update the text for the orders tab.
$menu_links['orders'] = __('My Past Orders');
// You can find below the default tabs.
// $menu_links['dashboard'] = __('Your Custom Dashboard Text'); // Update the Dashboard tab title.
// $menu_links['edit-address'] = __('Your Custom Addresses Text'); // Update the Addresses tab title.
// $menu_links['payment-methods'] =__('Your Custom Payment Methods Text'); // Update the Addresses tab title.
// $menu_links['edit-account'] = __('Your Custom Account Text'); // Update the Addresses tab title.
// $menu_links['customer-logout'] = __('Your Custom Logout Text'); // Update the logout tab title.
return $menu_links;
}
add_filter ( 'woocommerce_account_menu_items', 'my_site_update_my_account_links' );
添加自定义选项卡,链接到内部/外部 URL
/**
* Add a new custom link to the My Account page.
* @param $menu_links
* @return array|string[]
*/
function my_site_add_my_account_links( $menu_links ) {
// Uncomment these lines if you want to display the new tab only to certain role.
// Get the current user.
// $user = wp_get_current_user();
// if ( !in_array( 'MY_ROLE', (array) $user->roles ) ) {
// return $menu_links;
// }
// customendpoint will be hooked from the woocommerce_get_endpoint_url filter.
$new = [ 'customendpoint' => __('My new tab') ];
return array_slice( $menu_links, 0, 1, true )
+ $new
+ array_slice( $menu_links, 1, NULL, true );
}
add_filter ( 'woocommerce_account_menu_items', 'my_site_add_my_account_links' );
/**
* Set the URL for the endpoint customendpoint created in woocommerce_account_menu_items
* @param $url
* @param $endpoint
* @param $value
* @param $permalink
* @return mixed|string
*/
function my_site_custom_endpoint( $url, $endpoint, $value, $permalink ) {
if ( $endpoint === 'customendpoint' ) {
$url = '/internal-url/';
// You can have an external URL too.
// $url = 'https://davidloor.com';
}
return $url;
}
add_filter( 'woocommerce_get_endpoint_url', 'my_site_custom_endpoint', 10, 4 );
自定义排序
/**
* @snippet Reorder tabs @ My Account
* @how-to Get CustomizeWoo.com FREE
* @author Rodolfo Melogli
* @compatible WooCommerce 6
* @community https://businessbloomer.com/club/
*/
add_filter( 'woocommerce_account_menu_items', 'bbloomer_add_link_my_account' );
function bbloomer_add_link_my_account( $items ) {
$newitems = array(
'dashboard' => __( 'Dashboard', 'woocommerce' ),
'customer-logout' => __( 'Logout', 'woocommerce' ),
'edit-address' => _n( 'Addresses', 'Address', (int) wc_shipping_enabled(), 'woocommerce' ),
'edit-account' => __( 'Account details', 'woocommerce' ),
'orders' => __( 'Orders', 'woocommerce' ),
'downloads' => __( 'Downloads', 'woocommerce' ),
'payment-methods' => __( 'Payment methods', 'woocommerce' ),
);
return $newitems;
}
通过CSS分配图标
// Add custom product setting tab
function filter_woocommerce_product_data_tabs( $default_tabs ) {
$default_tabs['custom_tab'] = array(
'label' => __( 'Custom Tab', 'woocommerce' ),
'target' => 'my_custom_tab_data',
'priority' => 80,
'class' => array()
);
return $default_tabs;
}
add_filter( 'woocommerce_product_data_tabs', 'filter_woocommerce_product_data_tabs', 10, 1 );
// Contents custom product setting tab
function action_woocommerce_product_data_panels() {
// Note the 'id' attribute needs to match the 'target' parameter set above
echo '<div id="my_custom_tab_data" class="panel woocommerce_options_panel">';
// Add field
woocommerce_wp_text_input(array(
'id' => '_my_custom_data',
'label' => __( 'Product Support', 'woocommerce' ),
));
echo '</div>';
}
add_action( 'woocommerce_product_data_panels', 'action_woocommerce_product_data_panels', 10, 0 );
// Add CSS - icon
function action_admin_head() {
echo '<style>
#woocommerce-product-data ul.wc-tabs li.custom_tab_options a::before {
content: "\f101";
}
</style>';
}
add_action( 'admin_head', 'action_admin_head' );

原文链接:https://addprofans.com/woocommerce-my-account/,转载请注明出处。

