Woocommerce 3+中显示的有日期限制的海关产品销售价格



我有一个代码,在销售时会在价格后面打印文本,现在我使用这个代码时遇到了一个错误,这是我以前没有遇到的。它说:"PHP警告:date((期望参数2是整数",它指的是这行:

$sales_price_date_to = date( "j.M.Y", $sales_price_to );

有什么解决办法吗?

add_filter( 'woocommerce_get_price_html', 'custom_price_html', 100, 2 );
function custom_price_html( $price, $product ){
global $post;
$sales_price_to   = get_post_meta( $product->id, '_sale_price_dates_to', true );
$sales_price_date_to   = date( "j.M.Y", $sales_price_to );
if ( is_single() ) {
if($product->is_type( 'simple' ) && $sales_price_to != "" )  {
$sales_price_date_to = date("j.m.Y", $sales_price_to);
return str_replace( '</ins>', ' </ins> <span class="notice-price">(on offer until '.$sales_price_date_to.')</span>', $price );
}  else if ( $product->is_type( 'variable' ) ){
$handle         = new WC_Product_Variable( $product->id);
$variations1    = $handle->get_children();
$content_variations = "";
$count_each_var = 0;
foreach ($variations1 as $value) {
$count_each_var++;
$single_variation   = new WC_Product_Variation($value);
$var_id             = $single_variation->variation_id; 
$sales_price_to_variable    = get_post_meta($var_id, '_sale_price_dates_to', true);
if( $sales_price_to_variable != '' ){
$sales_price_date_to        = date("j.m.Y", $sales_price_to_variable);
if($count_each_var == 1) { 
$class_val = 'class="active"';
} else {
$class_val = "";
}
$content_variations         .= "<i data-id='". $var_id ."' data-order='".$count_each_var."' $class_val >".$sales_price_date_to."</i>";
} else {
$content_variations .= "";
}
}
if( $content_variations != ''){
return str_replace( '</ins>', ' </ins> <span class="notice-price">(on offer until '.$content_variations.')</span>', $price );
} else {
return $price;
}
} else {
return apply_filters( 'woocommerce_get_price', $price );
}
}
}

我一直在想办法,但我做不到。

自从Wooccommerce 3以来,您的代码已经过时了,因为很多事情都发生了变化。

我已经完全重新访问了您的代码,使其成为Wooccommerce3+版本的最新版本:

add_filter( 'woocommerce_get_price_html', 'custom_price_html', 100, 2 );
function custom_price_html( $price, $product ){
if ( is_product() ) {
// Simple products and variations
if( $product->is_type( 'simple' ) || $product->is_type( 'variation' )  )
{
$sales_price_to = $product->get_date_on_sale_to();
if( ! empty($sales_price_to) ){
$replacement = ' </ins> <span class="notice-price">(on offer until ';
return str_replace( '</ins>', $replacement . date( 'j.M.Y', $sales_price_to->getTimestamp() ) . ')</span>', $price );
}
}
// Variable products
else if ( $product->is_type( 'variable' ) )
{
$content = '';
// Loop through variations
foreach ( $product->get_children() as $key => $variation_id ) {
$variation       = wc_get_product($variation_id);
$sales_price_to  = $variation->get_date_on_sale_to();
if( ! empty($sales_price_to) ){
$date_to  = date( 'j.M.Y', $sales_price_to->getTimestamp() );
$class    = $key == 0 ? 'class="active"' : '';
$content .= '<i data-id="'.$variation_id.'" data-order="'.($key + 1).'" '.$class.'>'. $date_to .'</i>';
}
}
if( ! empty($content) ){
return $price . ' <span class="notice-price">(on offer until ' . $content .')</span>';
}
}
}
return $price;
}

代码位于活动子主题(或活动主题(的function.php文件中。测试并工作。

最新更新