显示WooCommerce产品页面上的产品名称和价格的短代码

huangapple go评论53阅读模式
英文:

Shortcode displaying the product name and price in WooCommerce product page

问题

我需要在产品页面上通过短代码显示产品标题和当前价格,而不使用产品ID。

我的意思是,我的代码将自动从我使用短代码的当前产品页面检测产品ID,并获取标题和价格。

例如,像这样,我必须在短代码内部使用特定的ID,如此 [iw_product_price id=''] 以显示产品价格。

// 通过短代码获取 WooCommerce 产品价格:[iw_product_price id='']
function iw_product_price_shortcode( $atts ) {
    $atts = shortcode_atts( array(
        'id' => null,
    ), $atts, 'bartag' );

    $html = '';

    if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){
		$_product = wc_get_product( $atts['id'] );
		$html = $_product->get_price_html();
    }
	return $html;
}
add_shortcode( 'iw_product_price', 'iw_product_price_shortcode' );

// 通过短代码获取 WooCommerce 产品名称:[iw_product_name id='']
function iw_product_name_shortcode( $atts ) {
    $atts = shortcode_atts( array(
        'id' => null,
    ), $atts, 'bartag' );

    $html = '';

    if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){
		$_product = wc_get_product( $atts['id'] );
		$html = $_product->get_title();
    }
    return $html; 
} 
add_shortcode( 'iw_product_name', 'iw_product_name_shortcode' );
英文:

I need to show product title and current price via shortcode in the product page without using product id.

I mean my code with automatically detect product id from current product page where i use shortcode and get the title and price.

For example this one, I have to use the specific id inside shortcode like this [iw_product_price id=''] to show product price.

// Get WooCommerce product price by ID via shortcode: [iw_product_price id='']
function iw_product_price_shortcode( $atts ) {
    $atts = shortcode_atts( array(
        'id' => null,
    ), $atts, 'bartag' );

    $html = '';

    if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){
		$_product = wc_get_product( $atts['id'] );
		$html = $_product->get_price_html();
    }
	return $html;
}
add_shortcode( 'iw_product_price', 'iw_product_price_shortcode' );

// Get WooCommerce product name by ID via shortcode: [iw_product_name id='']
function iw_product_name_shortcode( $atts ) {
    $atts = shortcode_atts( array(
        'id' => null,
    ), $atts, 'bartag' );

    $html = '';

    if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){
		$_product = wc_get_product( $atts['id'] );
		$html = $_product->get_title();
    }
    return $html; 
} 
add_shortcode( 'iw_product_name', 'iw_product_name_shortcode' );

答案1

得分: 1

以下是翻译好的代码部分:

// 添加短代码以获取产品ID(或产品对象)并在WooCommerce产品页面中显示带有格式化的产品价格的产品名称:
add_shortcode( 'product_name_price', 'get_product_name_price_shortcode' );
function get_product_name_price_shortcode( $atts ) {
    // 提取短代码属性
    extract( shortcode_atts( array(
        'id' => get_the_ID(),
    ), $atts, 'product_name_price' ) );
    
    global $product;

    $is_a_product =  is_a($product, 'WC_Product');
    
    if( ! $is_a_product && 'product' ===  get_post_type( $id )  ){
        $product = wc_get_product( $id ); // 通过ID获取产品对象
    } 
    if ( $is_a_product ) {
        return '<span class="product-name">' . $product->get_name() . '</span> <br>' .
        $product->get_price_html();
    }
}

作为简单短代码的用法:[product_name_price]

作为带有产品ID参数的短代码用法:[product_name_price id='28']

英文:

The following shortcode will detect the product ID (or the product Object) and will display the product name with the formatted product price in WooCommerce product pages:

add_shortcode( &#39;product_name_price&#39;, &#39;get_product_name_price_shortcode&#39; );
function get_product_name_price_shortcode( $atts ) {
    // Extract shortcode attributes
    extract( shortcode_atts( array(
        &#39;id&#39; =&gt; get_the_ID(),
    ), $atts, &#39;product_name_price&#39; ) );
    
    global $product;

    $is_a_product =  is_a($product, &#39;WC_Product&#39;);
    
    if( ! $is_a_product  &amp;&amp; &#39;product&#39; ===  get_post_type( $id )  ){
        $product = wc_get_product( $id ); // get the product object from the ID
    } 
    if ( $is_a_product ) {
        return &#39;&lt;span class=&quot;product-name&quot;&gt;&#39; . $product-&gt;get_name() . &#39;&lt;/span&gt; &lt;br&gt;&#39; .
        $product-&gt;get_price_html();
    }
}

Usage as a simple shortcode: [product_name_price]

Usage as a shortcode using the product ID argument: [product_name_price id=&#39;28&#39;]

huangapple
  • 本文由 发表于 2023年6月26日 03:57:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76552196.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定