【问题标题】:WooCommerce product additional information shortcodeWooCommerce 产品附加信息简码
【发布时间】:2018-10-17 22:50:46
【问题描述】:

我是 WooCommerce 的新手,正在寻找一种在帖子页面上显示 产品属性 的解决方案。 我进行了研究和一些测试,但似乎没有任何效果。

理想情况下,我想使用 简码 获取 产品 ID 并在我的帖子页面上显示他的所有产品属性。 类似[product_page id="99" display_only_attributes ]

【问题讨论】:

  • 我只希望它们像通常在产品页面上一样显示在我的帖子页面(博客)上。但只有附加信息部分(属性)。没有 html,只有 wordpress 后端。目标是我公司的内容团队能够使用在 woocommerce 中输入的任何产品,并在帖子页面中间显示其属性。这是一个简单的产品,也会显示变化。
  • 所以你想在产品列表页面上显示所有属性,产品方面。
  • 不是在产品页面上,而是在帖子页面上。我们目前正在建立一个汽车评论网站。每辆汽车都将进入 woocommerce,所有指定的属性都与每辆汽车相关联。评论写成普通的博客文章。当内容团队撰写关于汽车的评论时,我希望他们能够使用一个短代码来引用在 woocommerce 中输入的汽车 ID,这将在文章中列出其所有属性
  • 完美运行!谢谢美人 :)

标签: php wordpress woocommerce shortcode custom-taxonomy


【解决方案1】:

这是在自定义简码中获取产品属性的方法,您将在其中将产品 ID 定义为简码参数 id。

功能代码:

if ( ! function_exists( 'display_product_additional_information' ) ) {

    function display_product_additional_information($atts) {

        // Shortcode attribute (or argument)
        $atts = shortcode_atts( array(
            'id'    => ''
        ), $atts, 'product_additional_information' );

        // If the "id" argument is not defined, we try to get the post Id
        if ( ! ( ! empty($atts['id']) && $atts['id'] > 0 ) ) {
           $atts['id'] = get_the_id();
        }

        // We check that the "id" argument is a product id
        if ( get_post_type($atts['id']) === 'product' ) {
            $product = wc_get_product($atts['id']);
        }
        // If not we exit
        else {
            return;
        }

        ob_start(); // Start buffering

        do_action( 'woocommerce_product_additional_information', $product );

        return ob_get_clean(); // Return the buffered outpout
    }

    add_shortcode('product_additional_information', 'display_product_additional_information');

}

代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

短代码用法 (已更新)

  1. 具有定义的产品 ID:

    [product_additional_information id='37']
    

    或者在php中:

    echo do_shortcode("[product_additional_information id='37']");
    
  2. 在现有的产品页面中(例如,当“附加信息”产品标签被删除时):

    [product_additional_information]
    

    或者在php中:

    echo do_shortcode("[product_additional_information]");
    

你会得到这样的东西:

【讨论】:

    猜你喜欢
    • 2014-08-14
    • 1970-01-01
    • 2014-09-20
    • 1970-01-01
    • 2017-12-29
    • 2018-06-06
    • 2022-01-23
    • 2022-01-27
    • 1970-01-01
    相关资源
    最近更新 更多