【问题标题】:woocommerce how to get a product price with a shortcodewoocommerce 如何使用简码获取产品价格
【发布时间】:2019-08-08 18:39:07
【问题描述】:

我正在尝试做一个短代码来显示一种产品的价格,

我刚刚在我的functions.php中做了这个,应该很容易,我错过了什么吗?!

我只是在我的页面中输入了这个简码:[product_price id="378"]

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

add_shortcode('product_price', 'displayPriceProduct');

我只是收到一个没有详细信息的基本错误,如果我不删除代码,该页面将无法访问

【问题讨论】:

  • 你放弃分号 -> $productprice = wc_get_product($item)
  • 哇,我真笨!一个小时卡住了分号的原因:'(非常感谢你让我开心
  • 魔鬼在细节中:D

标签: php wordpress woocommerce


【解决方案1】:

你可以用这个:

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' );

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

您也可以使用以下代码换取其他价格:

$_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();

或获取产品信息:

$_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() );

或获取产品尺寸:

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

获取产品图片:

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

谢谢

【讨论】:

  • 非常感谢所有这些细节
猜你喜欢
  • 1970-01-01
  • 2016-10-15
  • 1970-01-01
  • 1970-01-01
  • 2018-02-07
  • 2015-02-25
  • 2018-04-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多