是否有在Woocommerce产品说明一个简码

问题描述 投票:1回答:2

有没有简码调用(标题下的文本字段)产品说明?

现在我在使用其他自定义字段做这个工作,但它会更好,如果我使用woocomerce领域。

php wordpress woocommerce product shortcode
2个回答
1
投票

我写了一个非常类似的解决方案,以@ LoicTheAztec的回答以上,但稍微防守(他的溶液打破Elementor对我来说,由于不存在作为一个产品的背景下,当它执行上保存的简码编辑)。

作为内容在返回之前格式化(基本上与<p>标记替换换行符),这也解决了你的款/换行的问题。

function custom_product_description($atts){
    global $product;

    try {
        if( is_a($product, 'WC_Product') ) {
            return wc_format_content( $product->get_description("shortcode") );
        }

        return "Product description shortcode run outside of product context";
    } catch (Exception $e) {
        return "Product description shortcode encountered an exception";
    }
}
add_shortcode( 'custom_product_description', 'custom_product_description' );

1
投票

你可以建立自己的短代码是这样的:

add_shortcode( 'product_description', 'display_product_description' );
function display_product_description( $atts ){
    $atts = shortcode_atts( array(
        'id' => get_the_id(),
    ), $atts, 'product_description' );

    global $product;

    if ( ! is_a( $product, 'WC_Product') )
        $product = wc_get_product($atts['id']);

    return $product->get_description();
}

代码放在您的活动子主题(活动主题)的function.php文件。测试和工程。

用法[product_description]的例子

1)在目前的产品页面pH值:

echo do_shortcode( "[product_description]" );

2)在任何PHP代码提供所述相关的产品ID

echo do_shortcode( "[product_description id='37']" );
© www.soinside.com 2019 - 2024. All rights reserved.