Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 用短代码显示产品价格,包括商业中的可变产品_Php_Wordpress_Woocommerce_Currency_Price - Fatal编程技术网

Php 用短代码显示产品价格,包括商业中的可变产品

Php 用短代码显示产品价格,包括商业中的可变产品,php,wordpress,woocommerce,currency,price,Php,Wordpress,Woocommerce,Currency,Price,我正在使用优秀的答案,通过一个短代码显示产品价格,但我需要一个解决方案,它还可以显示可变产品的价格范围 价格范围应显示所有车型的最低可购买价格(常规或销售价格),以及所有车型的最高可购买价格。它不需要显示销售价格和常规价格的两个范围,只需显示一个范围,显示您可以实际购买的价格,无论是销售价格还是常规价格 例如: 变更1-价格4.00 变更2-价格4.00销售价格3.00 变更3-价格6.00销售价格5.00 变更4-价格8.00 上述价格范围应为3.00-8.00 代码当前处理单个产品的方式

我正在使用优秀的答案,通过一个短代码显示产品价格,但我需要一个解决方案,它还可以显示可变产品的价格范围

价格范围应显示所有车型的最低可购买价格(常规或销售价格),以及所有车型的最高可购买价格。它不需要显示销售价格和常规价格的两个范围,只需显示一个范围,显示您可以实际购买的价格,无论是销售价格还是常规价格

例如:

  • 变更1-价格4.00
  • 变更2-价格4.00销售价格3.00
  • 变更3-价格6.00销售价格5.00
  • 变更4-价格8.00
上述价格范围应为3.00-8.00

代码当前处理单个产品的方式不需要更改,我只需要向其中添加可变产品

我使用的确切代码如下所示:

function custom_price_shortcode_callback( $atts ) {

$atts = shortcode_atts( array(
    'id' => null,
), $atts, 'product_price' );

$html = '';

if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){
    // Get an instance of the WC_Product object
    $product = wc_get_product( intval( $atts['id'] ) );

    // Get the product prices
    $price         = wc_get_price_to_display( $product, array( 'price' => $product->get_price() ) ); // Get the active price
    $regular_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ); // Get the regular price
    $sale_price    = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ); // Get the sale price

    // Formatting price settings (for the wc_price() function)
    $args = array(
        'ex_tax_label'       => false,
        'decimal_separator'  => '.',
        'thousand_separator' => ',',
        'decimals'           => 2,
        'price_format'       => '%1$s%2$s',
    );

    // Formatting html output
    if( ! empty( $sale_price ) && $sale_price != 0 && $sale_price < $regular_price )
        $html = "<br/><del>" . wc_price( $regular_price, $args ) . "</del> " . wc_price( $sale_price, $args ); // Sale price is set
    else
        $html = "<br/>" . wc_price( $price, $args ); // No sale price set
}
return $html;

 } 
add_shortcode( 'product_price', 'custom_price_shortcode_callback' );
编辑:我可以确认显示的价格使用Aelia货币转换器插件正确转换,因此不需要进行任何更改。我已经从问题中删除了这一部分,并从上面的代码中删除了货币代码,因为这是自动添加的

我尝试了以下方法,几乎可以正常工作,它正确地识别了可变产品类型,并显示了“from”和“to”字样,但没有显示实际价格(因为我认为我需要从某处获取
$from
$to
,但不知道如何获取):

函数自定义\价格\快捷码\回调($atts){
$atts=短码_atts(数组)(
'id'=>null,
),$atts,“产品价格”);
$html='';
如果(intval($atts['id'])>0&&function_存在('wc_get_product')){
$product=wc_get_product(intval($atts['id'));
如果($product->是_类型('variable')){
$args=数组(
“ex_tax_label”=>错误,
'十进制分隔符'=>',
'千分位符'=>',',
“小数”=>2,
“价格格式”=>“%1$s%2$s”,
);
$price=sprintf(x('From%1$s to%2$s','price range:From'),是数字($From)?wc_price($From,$args):$From,是数字($to,$args)?wc_price($to,$args):$to);
$html=“
”$price; } 否则{ //了解产品价格 $price=wc_get_price_to_display($product,array('price'=>$product->get_price());//获取活动价格 $regular_price=wc_get_price_to_display($product,array('price'=>$product->get_regular_price());//获取常规价格 $sale_price=wc_get_price_to_display($product,array('price'=>$product->get_sale_price());//获取销售价格 //格式化价格设置(用于wc_price()函数) $args=数组( “ex_tax_label”=>错误, '十进制分隔符'=>', '千分位符'=>',', “小数”=>2, “价格格式”=>“%1$s%2$s”, ); //格式化html输出 如果(!空($sale\u price)&$sale\u price!=0&&$sale\u price<$regular\u price) $html=“
”.wc\u price($regular\u price,$args)。”“.wc\u price($sale\u price,$args);//已设置销售价格 其他的 $html=“
”。wc_price($price,$args);//未设置销售价格 } } 返回$html; } 添加快捷代码(“产品价格”、“自定义价格”快捷代码“回调”);
如果我将可变产品部分包装为价格范围部分的格式,则可变产品部分将完全停止工作:

if ( $product->is_type( 'variable' ) ){ 
    $price = apply_filters( 'woocommerce_format_price_range', $price, $from, $to );       
    if ( !empty( $price ) ) { 
    $args = array(
        'ex_tax_label'       => false,
        'decimal_separator'  => '.',
        'thousand_separator' => ',',
        'decimals'           => 2,
        'price_format'       => '%1$s%2$s',
    );
    $price = sprintf( _x( 'From %1$s to %2$s', 'Price range: from-to'), is_numeric( $from ) ? wc_price( $from, $args ) : $from, is_numeric( $to ) ?  wc_price( $to, $args ) : $to );
    $html = "<br/>" . $price;
    }
    }
如果($product->is_类型('variable')){
$price=apply_filters('woocommerce_format_price_range',$price,$from,$to);
如果(!empty($price)){
$args=数组(
“ex_tax_label”=>错误,
'十进制分隔符'=>',
'千分位符'=>',',
“小数”=>2,
“价格格式”=>“%1$s%2$s”,
);
$price=sprintf(x('From%1$s to%2$s',价格范围:From-to'),是数字($From)?wc_价格($From,$args):$From,是数字($to)?wc_价格($to,$args):$to);
$html=“
”$price; } }
这也不起作用:

if ( $product->is_type( 'variable' ) ){ 
$price = wc_format_price_range( $from, $to );       
if ( !empty( $price ) ) { 
$price = sprintf( _x( '%1$s &ndash; %2$s', 'Price range: from-to', 'woocommerce' ), is_numeric( $from ) ? wc_price( $from ) : $from, is_numeric( $to ) ? wc_price( $to ) : $to );
$html = "<br/>" . $price;
}
}
如果($product->is_类型('variable')){
$price=wc_格式_价格_范围($to);
如果(!empty($price)){
$price=sprintf(x(“%1$s&ndash;%2$s”,“价格范围:从到”,“woocommerce”),是数字($from)?wc_价格($from):$from,是数字($to)?wc_价格($to):$to);
$html=“
”$price; } }
使用Booster for woocommerce插件。并将此[wcj_product_price]快捷码放置在您想要的任何位置。Its将用货币符号显示简单或可变的产品价格。这个插件对woommerce很有帮助

我使用的是以下解决方案,这与我的初衷不尽相同,因为它不是显示可变产品的价格范围,而是显示一条“发件人”消息(可以更改),然后是一个单一的价格

然而,它现在确实可以通过shortcode为标准产品和可变产品工作,我需要的也是如此

function custom_price_shortcode_callback( $atts ) {

$atts = shortcode_atts( array(
    'id' => null,
), $atts, 'product_price' );

$html = '';

if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){

$product = wc_get_product( intval( $atts['id'] ) );

if ( $product->is_type( 'variable' ) ){ 

        $prefix = sprintf('%s ', __('From','woocommerce'));
        $min_price_regular = $product->get_variation_regular_price( 'min', true );
        $min_price_sale    = $product->get_variation_sale_price( 'min', true );
        $max_price = $product->get_variation_price( 'max', true );
        $min_price = $product->get_variation_price( 'min', true );

    $price = ( $min_price_sale == $min_price_regular ) ? wc_price( $min_price_regular ) : wc_price( $min_price_sale );
    return 
    ( $min_price == $max_price ) ? sprintf('<br/>' . $price) : $html = sprintf('<br/><span class="from-price">%s%s</span>', $prefix, $price);
}


else{

// Get the product prices
$price         = wc_get_price_to_display( $product, array( 'price' => $product->get_price() ) ); // Get the active price
$regular_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ); // Get the regular price
$sale_price    = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ); // Get the sale price

// Formatting price settings (for the wc_price() function)
$args = array(
    'ex_tax_label'       => false,
    'decimal_separator'  => '.',
    'thousand_separator' => ',',
    'decimals'           => 2,
    'price_format'       => '%1$s%2$s',
);

// Formatting html output
if( ! empty( $sale_price ) && $sale_price != 0 && $sale_price < $regular_price )
    $html = "<br/><del>" . wc_price( $regular_price, $args ) . "</del> " . wc_price( $sale_price, $args ); // Sale price is set
else
    $html = "<br/>" . wc_price( $price, $args ); // No sale price set
}
}
return $html;
} 
add_shortcode( 'product_price', 'custom_price_shortcode_callback' );
函数自定义\价格\快捷码\回调($atts){
$atts=短码_atts(数组)(
'id'=>null,
),$atts,“产品价格”);
$html='';
如果(intval($atts['id'])>0&&function_存在('wc_get_product')){
$product=wc_get_product(intval($atts['id'));
如果($product->是_类型('variable')){
$prefix=sprintf('%s','From','woocommerce');
$min\u price\u regular=$product->get\u variation\u regular\u price('min',true);
$min\u price\u sale=$product->get\u variation\u sale\u price('min',true);
$max\u price=$product->get\u variation\u price('max',true);
$min\u price=$product->get\u variation\u price('min',true);
$price=($min\u price\u sale=$min\u price\u regular)?wc\u price($min\u price\u regular):wc\u price($min\u price\u sale);
返回
($min_price==$max_price)?sprintf(“
”。$pric
if ( $product->is_type( 'variable' ) ){ 
$price = wc_format_price_range( $from, $to );       
if ( !empty( $price ) ) { 
$price = sprintf( _x( '%1$s &ndash; %2$s', 'Price range: from-to', 'woocommerce' ), is_numeric( $from ) ? wc_price( $from ) : $from, is_numeric( $to ) ? wc_price( $to ) : $to );
$html = "<br/>" . $price;
}
}
function custom_price_shortcode_callback( $atts ) {

$atts = shortcode_atts( array(
    'id' => null,
), $atts, 'product_price' );

$html = '';

if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){

$product = wc_get_product( intval( $atts['id'] ) );

if ( $product->is_type( 'variable' ) ){ 

        $prefix = sprintf('%s ', __('From','woocommerce'));
        $min_price_regular = $product->get_variation_regular_price( 'min', true );
        $min_price_sale    = $product->get_variation_sale_price( 'min', true );
        $max_price = $product->get_variation_price( 'max', true );
        $min_price = $product->get_variation_price( 'min', true );

    $price = ( $min_price_sale == $min_price_regular ) ? wc_price( $min_price_regular ) : wc_price( $min_price_sale );
    return 
    ( $min_price == $max_price ) ? sprintf('<br/>' . $price) : $html = sprintf('<br/><span class="from-price">%s%s</span>', $prefix, $price);
}


else{

// Get the product prices
$price         = wc_get_price_to_display( $product, array( 'price' => $product->get_price() ) ); // Get the active price
$regular_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ); // Get the regular price
$sale_price    = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ); // Get the sale price

// Formatting price settings (for the wc_price() function)
$args = array(
    'ex_tax_label'       => false,
    'decimal_separator'  => '.',
    'thousand_separator' => ',',
    'decimals'           => 2,
    'price_format'       => '%1$s%2$s',
);

// Formatting html output
if( ! empty( $sale_price ) && $sale_price != 0 && $sale_price < $regular_price )
    $html = "<br/><del>" . wc_price( $regular_price, $args ) . "</del> " . wc_price( $sale_price, $args ); // Sale price is set
else
    $html = "<br/>" . wc_price( $price, $args ); // No sale price set
}
}
return $html;
} 
add_shortcode( 'product_price', 'custom_price_shortcode_callback' );