📌  相关文章
📜  自定义我的帐户页面 woocommerce - PHP 代码示例

📅  最后修改于: 2022-03-11 14:54:12.306000             🧑  作者: Mango

代码示例1
// Method 1:
// To create a whole new page using code, copy the following file: plugins/woocommerce/templates/myaccount/dashboard.php
// Paste the dashboard.php file in your child theme directory themes//woocommerce/templates/myaccount/dashboard.php
// Folder structure and file names must match to wordpress override the original content.
// Now you can mess with your own dashboard file or using hooks as shown below.

// Method 2:
// Edit WooCommerce My Account Page with hooks
// Rename Tabs
add_filter( 'woocommerce_account_menu_items', 'my_rename_acc_adress_tab', 9999 );
function my_rename_acc_adress_tab( $items ) {
  $items['edit-address'] = 'Your addresses';
  return $items;
}

// Remove Tabs
add_filter( 'woocommerce_account_menu_items', 'my_remove_acc_address’, 9999 );
function my_remove_acc_address( $items ) {
  unset( $items['downloads'] );
  return $items;
}

// All items that can be modified, just use the array keys 'dashboard', 'orders' etc...
$items = array(
  'dashboard'       => __( 'Dashboard', 'woocommerce' ),
  'orders'          => __( 'Orders', 'woocommerce' ),
  'downloads'       => __( 'Downloads', 'woocommerce' ),
  'edit-address'    => _n( 'Addresses', 'Address', (int) wc_shipping_enabled(), 'woocommerce' ),
  'payment-methods' => __( 'Payment methods', 'woocommerce' ),
  'edit-account'    => __( 'Account details', 'woocommerce' ),
  'customer-logout' => __( 'Logout', 'woocommerce' ),
);

// Merge Tabs and Content
// Remove the Addresses tab
add_filter( 'woocommerce_account_menu_items', 'my_remove_acc_tab', 999 );
function my_remove_acc_tab( $items ) {
  unset($items['edit-address']);
  return $items;
}

// Insert the content of the Addresses tab into an existing tab (edit-account in this case)
add_action( 'woocommerce_account_edit-account_endpoint', 'woocommerce_account_edit_address' );

// Add a new tab with custom content
// 1. Register new endpoint
// Note: Resave Permalinks or it will give 404 error  
function my_add_support_endpoint() {
    add_rewrite_endpoint( 'support', EP_ROOT | EP_PAGES );
}
add_action( 'init', 'my_add_support_endpoint' );

// 2. Add new query
function QuadLayers_support_query_vars( $vars ) {
    $vars[] = 'support';
    return $vars;
}  
add_filter( 'query_vars', 'my_support_query_vars', 0 );  

// 3. Insert the new endpoint 
function my_add_support_link_my_account( $items ) {
    $items['support'] = 'Support ™';
    return $items;
}  
add_filter( 'woocommerce_account_menu_items', 'my_add_support_link_my_account' );

// 4. Add content to the new endpoint 
// Note: The shortcodes on 'do_shortcodes' are just examples, use your own code.
function my_support_content() {
echo '

Support

Welcome to the support area. As a premium customer, manage your support tickets from here, you can submit a ticket if you have any issues with your website. We\'ll put our best to provide you with a fast and efficient solution

'; echo do_shortcode( '[tickets-shortcode]' ); echo do_shortcode( '[wpforms id="1082"]' ); } add_action( 'woocommerce_account_support_endpoint', 'my_support_content' );