[英]Add custom html content in WooCommerce My Account Dashboard only
I am using WordPress with WooCommerce and WooCommerce Subscription plugins and its working fine as per my requirements.我正在将 WordPress 与 WooCommerce 和 WooCommerce 订阅插件一起使用,并且根据我的要求可以正常工作。
Now I want to add my custom div
in /my-account/
page ie ( http://www.example.com/my-account ) after we login to system.现在我想在登录系统后在
/my-account/
页面中添加我的自定义div
即( http://www.example.com/my-account )。
So I researched and found out this page /wp-content/plugins/woocommerce/templates/myaccount/my-account.php
and that page has below code.所以我研究并发现了这个页面
/wp-content/plugins/woocommerce/templates/myaccount/my-account.php
并且该页面具有以下代码。
<?php
defined( 'ABSPATH' ) || exit;
do_action( 'woocommerce_account_navigation' ); ?>
<div class="woocommerce-MyAccount-content">
<?php
do_action( 'woocommerce_account_content' );
?>
</div>
Hence I have added my code code something like this因此,我添加了类似这样的代码代码
<div>Test</div>
Below do_action( 'woocommerce_account_content' );
下面
do_action( 'woocommerce_account_content' );
but then its appearing in all the pages like my-account/orders/
, /my-account/downloads/
etc.. I do not want it.但随后它出现在
my-account/orders/
、 /my-account/downloads/
等所有页面中。我不想要它。
How can I show this only in /my-account/
page?如何仅在
/my-account/
页面中显示此内容? Additionally, how can I just overwrite this page by copying in one of my active themes so that I do not need to make changes in core files of the plugins?此外,我怎样才能通过复制我的一个活动主题来覆盖此页面,以便我不需要更改插件的核心文件?
To make that displayed only in the My account Dashboard without overriding any template file , you can use the following hooked function instead:要使其仅显示在我的帐户仪表板中而不覆盖任何模板文件,您可以使用以下挂钩 function 代替:
add_action( 'woocommerce_account_dashboard', 'custom_account_dashboard_content' );
function custom_account_dashboard_content(){
echo '<div>' . __("Test", "woocommerce") . '</div>';
}
Code goes in functions.php file of the active child theme (or active theme).代码进入活动子主题(或活动主题)的functions.php文件。 Tested and works.
测试和工作。
The custom html content is just added only to My Account Dashboard as expected (without overriding any template file) :自定义 html 内容仅按预期添加到我的帐户仪表板(不覆盖任何模板文件) :
Note: This requires to replace back WooCommerce original related templates files.注意:这需要将WooCommerce原来的相关模板文件替换回来。
You're on the right track with the templates, but you haven't quite gone deep enough.您在模板的正确轨道上,但还不够深入。 We can see by another
do_action()
that it's still pulling in another template in the code you're working on - in this case, the code specifically for that dashboard page.我们可以通过另一个
do_action()
看到它仍在您正在处理的代码中拉入另一个模板 - 在这种情况下,是专门用于该仪表板页面的代码。
Instead of /wp-content/plugins/woocommerce/templates/myaccount/my-account.php
, you want to go to: /wp-content/plugins/woocommerce/templates/myaccount/dashboard.php
.而不是
/wp-content/plugins/woocommerce/templates/myaccount/my-account.php
,你想 go 到: /wp-content/plugins/woocommerce/templates/myaccount/dashboard.php
There, you'll find this code (which displays the default message) and you can add your custom div there, which would only display on the dashboard.在那里,您会找到此代码(显示默认消息),您可以在那里添加您的自定义 div,它只会显示在仪表板上。 Around line 50 (as of writing) you'll see
大约第 50 行(截至写作时)你会看到
printf(
wp_kses( $dashboard_desc, $allowed_html ),
esc_url( wc_get_endpoint_url( 'orders' ) ),
esc_url( wc_get_endpoint_url( 'edit-address' ) ),
esc_url( wc_get_endpoint_url( 'edit-account' ) )
);
?>
</p>
You can add any custom code under the closing </p>
to stay under the paragraph.您可以在结束
</p>
下添加任何自定义代码以留在段落下方。 If you want it above the paragraph, you'd start writing around Line 41, again after a closing </p>
如果你想要它在段落上方,你可以在第 41 行左右开始写,在结束之后再次写
</p>
You would want to override that template in your theme, so in your theme, create a folder/file structure like this:您可能希望在您的主题中覆盖该模板,因此在您的主题中,创建一个文件夹/文件结构,如下所示:
[yourtheme]/woocommerce/myaccount/dashboard.php
. [yourtheme]/woocommerce/myaccount/dashboard.php
。 You intentionally do not include templates
in your theme override.您有意不在主题覆盖中包含
templates
。 You would copy dashboard.php
into this folder and edit that version.您可以将
dashboard.php
复制到此文件夹并编辑该版本。
There's some developer documentation about template structure and overrides that might be good to check out here: https://docs.woocommerce.com/document/template-structure/有一些关于模板结构和覆盖的开发人员文档可能会在这里查看: https://docs.woocommerce.com/document/template-structure/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.