【问题标题】:Display related products using a shortcode on WooCommerce My account Dashboard在 WooCommerce 我的帐户仪表板上使用简码显示相关产品
【发布时间】:2021-02-15 18:22:42
【问题描述】:

我正在尝试在我的帐户仪表板页面中显示相关产品。我关注了这个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” ]' );
    }  
}

请帮忙解决这个问题?

【问题讨论】:

    标签: php wordpress woocommerce hook-woocommerce shortcode


    【解决方案1】:

    请注意,相关产品短代码需要在单个产品页面上使用,因为相关产品始终链接到一个产品,这就是 [related_products limit="3"] 短代码在我的帐户仪表板上不起作用的原因。

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

    $post = get_post();
    
    if ( is_page() AND $post->post_parent ) {
    

    通过这个独特的代码行:

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

    好多了。见WooCommerce Conditional Tags

    【讨论】:

    • 感谢@loictheaztec 的回复。有没有其他方法在这里展示相关产品?没有简码。知道我可以从哪里开始吗?
    • 不,我已经告诉过你,这需要与产品“相关”。如果这个答案回答了你的问题,你可以请accept回答,谢谢。
    • 是否可以显示它与用户最后的订单相关?
    • @teqqq 你的意思是根据当前用户最后一个订单中购买的产品获取相关产品吗?是的,这应该是可能的......请提出一个新问题:““显示基于上次在 WooCommerce 我的帐户仪表板上购买的相关产品”。在您的问题中添加您的代码并更新解释。一旦发布,请在此处通过您的新链接通知我问题。
    猜你喜欢
    • 2020-10-30
    • 2019-10-05
    • 2017-05-24
    • 2021-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-25
    • 1970-01-01
    相关资源
    最近更新 更多