我有一段代码,该代码在销售时会在价格后打印文本,现在我在使用此代码时遇到了错误,这是我以前没有得到的。它说:“ 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 );
}
}
}
曾经试图修复它,但是我不能。
答案 0 :(得分:1)
自Woocommerce 3以来,您的代码确实已经过时了,因为很多事情已经改变。
我已经完全重新审视了您的代码,使其成为Woocommerce 3+版本的最新代码:
public static List<String> readLines(File file) throws Exception {
if (!file.exists()) {
return new ArrayList<String>();
}
BufferedReader reader = new BufferedReader(new FileReader(file));
List<String> results = new ArrayList<String>();
String line = reader.readLine();
while (line != null) {
results.add(line);
line = reader.readLine();
}
return results;
}
代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。