在 WooCommerce 我的帐户仪表板上使用简码显示相关产品

Display related products using a shortcode on WooCommerce My account Dashboard

我正在尝试在“我的帐户仪表板”页面中显示相关产品。我关注了这个 WooCommerce Shortcodes documentation.

我尝试使用以下代码:

add_action( 'woocommerce_account_content', 'output_shortcode_on_child_pages');
function output_shortcode_on_child_pages() {
    $post = get_post();
    
    if ( is_page() AND $post->post_parent ) {
        echo do_shortcode( '[related_products limit=”3″]' );
    }
}

这里不显示相关产品。但是当我尝试显示特定类别的产品时,如下所示,它有效:

add_action( 'woocommerce_account_content', 'output_shortcode_on_child_pages');
function output_shortcode_on_child_pages() {
    $post = get_post();
    
    if ( is_page() AND $post->post_parent ) {
        echo do_shortcode( '[products limit="3" columns="3" orderby="rand" category=”presets-and-actions” ]' );
    }  
}

请帮忙解决这个问题?

Note that related products shortcode need to be used on single product pages, as related products are always linked to a product, that's why [related_products limit="3"] shortcode doesn't work on My account dashboard.

现在以我的帐户仪表板为目标,在您的函数中替换以下代码行:

$post = get_post();

if ( is_page() AND $post->post_parent ) {

通过这个独特的代码行:

if ( is_account_page() && ! is_wc_endpoint_url() ) {

好多了。参见 WooCommerce Conditional Tags