简体   繁体   English

woocommerce如何使用简码获得产品价格

[英]woocommerce how to get a product price with a shortcode

I'm trying to do a shortcode who will display the price of one product, 我正在尝试编写一个短代码,以显示一种产品的价格,

i just did that in my functions.php, it should be easy, did i miss something ?! 我只是在我的functions.php中做到了,这应该很容易,我错过了什么吗?

in my page i just put this shortcode : [product_price id="378"] 在我的页面中,我只输入了以下简短代码:[product_price id =“ 378”]

//shortcode woocommerce prix
function displayPriceProduct($item) {
   $productprice = wc_get_product($item)
    return $productprice->get_price();
}

add_shortcode('product_price', 'displayPriceProduct');

i just get a basic error without details, the page is not accessible anymore if i dont remove the code 我只是收到一个基本错误,没有详细信息,如果我不删除代码,该页面将无法访问

you can use this : 您可以使用此:

function displayPriceProduct( $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 = "price = " . $_product->get_price();
    }
    return $html;
}
add_shortcode( 'woocommerce_product_price', 'displayPriceProduct' );

Your shortcode : [woocommerce_product_price id="99"] 您的简码: [woocommerce_product_price id="99"]

also you can use below code for other price : 您也可以将以下代码用于其他价格:

$_product->get_price();
$_product->get_regular_price();
$_product->get_sale_price();
$_product->get_date_on_sale_from();
$_product->get_date_on_sale_to();
$_product->get_total_sales();

or get product info : 或获取产品信息:

$_product->get_type();
$_product->get_name();
$_product->get_slug();
$_product->get_date_created();
$_product->get_date_modified();
$_product->get_status();
$_product->get_featured();
$_product->get_catalog_visibility();
$_product->get_description();
$_product->get_short_description();
$_product->get_sku();
$_product->get_menu_order();
$_product->get_virtual();
get_permalink( $_product->get_id() );

or get product Dimensions : 或获取产品尺寸:

$_product->get_weight();
$_product->get_length();
$_product->get_width();
$_product->get_height();
$_product->get_dimensions();

Get Product Images : 获取产品图片:

$_product->get_image_id();
$_product->get_image();
$_product->get_gallery_image_ids();

thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM