我在主题的functions.php文件中有以下WooCommerce变体价格格式功能:
function wc_wc20_variation_price_format( $price, $product ) {
// Main Price
$prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
$price = $prices[0] !== $prices[1] ? sprintf( __( 'From: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
// Sale Price
$prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) );
sort( $prices );
$saleprice = $prices[0] !== $prices[1] ? sprintf( __( 'From: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
if ( $price !== $saleprice ) {
$price = '<del>' . $saleprice . '</del> <ins>' . $price . '</ins>';
}
return $price;
}
这是在一些地方在线列出的,例如:https://gist.github.com/kloon/8981075,并且有很多人报告它为他们工作。它适用于我,但我间歇性地得到语法错误:
解析错误:语法错误,意外&#39;,&#39;在第12行的wp-content / themes / mytheme / functions.php中
第12行是排序前的行($ price); - 第二次将$ prices变量设置为数组的行。
我在这里看不到任何语法问题。奇怪的是,这并不是一直发生的。有时当我访问产品类别然后返回网站主页时会发生这种情况 - 但不是每次都有。一切都按预期工作,除了这个语法错误在网站出现时完全破坏网站。
修改的
经过一些进一步的调试后,这里有一个更新:
直接在相关行之前,我将$ product-&gt; get_variation_regular_price()的输出记录到文本文件中:
$file = WP_CONTENT_DIR . '/errors.txt';
$open = fopen( $file, "a" );
ob_start();
print_r($product->get_variation_regular_price( 'min', true ));
$min = ob_get_clean();
$write = fputs( $open, "min: " . $min );
ob_start();
print_r($product->get_variation_regular_price( 'min', true ));
$max = ob_get_clean();
$write = fputs( $open, "max: " . $max);
fclose( $open );
这一次,错误如下:
解析错误:语法错误,意外&#39; $ min&#39; (T_VARIABLE),期待 &#39;,&#39;或者&#39;)&#39;在第19行的wp-content / themes / mytheme / functions.php中
第19行是第一个print_r语句。
我的errors.txt现在包含:
min:22.99max:22.99min:22.99max:22.99min:25.99max:25.99min: 22.99max:22.99min:25.99max:25.99min:25.99max:25.99min:22.99max:22.99min:22.99max:22.99min:22.99max:22.99min:22.99max:22.99min:22.99max:22.99min:25.99max :25.99min:22.99max:22.99min:25.99max:25.99min:24.99max:24.99min:24.99max:24.99min:22.99max:22.99min:25.99max:25.99min:22.99max:22.99min:22.99max:22.99 min:22.99max:22.99min:22.99max:22.99min:22.99max:22.99min:22.99max:22.99min:25.99max:25.99min:22.99max:22.99min:25.99max:25.99min:24.99max:24.99min: 24.99max:24.99min:22.99max:22.99min:24.99max:24.99min:25.99max:25.99min:22.99max:22.99min:25.99max:25.99min:24.99max:24.99min:25.99max:25.99min:22.99max :22.99min:22.99max:22.99min:22.99max:22.99min:22.99max:22.99min:25.99max:25.99min:22.99max:22.99min:25.99max:25.99min:24.99max:24.99min:24.99max:24.99 min:22.99max:22.99min:22.99max:22.99min:25.99max:25.99min:22.99max:22.99min:25.99max:25.99min:25.99max:25.99min:22.99max:22.99min:22.99max:22.99min:22.99max:22.99min:22.99max:22.99min:22.99max:22.99min:25.99max:25.99min:22.99max:22.99min:25.99max:25.99min:24.99max:24.99min:24.99max:24.99min:22.99max :22.99min:22.99max:22.99min:22.99max:22.99min:24.99max:24.99min:25.99max:25.99min:22.99max:22.99min:22.99max:22.99min:25.99max:25.99min:22.99max:22.99 min:22.99max:22.99min:22.99max:22.99min:25.99max:25.99min:22.99max:22.99min:25.99max:25.99min:24.99max:24.99min:24.99max:24.99
我不确定哪个特定数字来自该刷新,因为出现语法错误需要进行相当多的刷新,但它应该是该数据末尾附近的混合/最大值。我没有在数据中看到任何与众不同的东西,这对我来说都是正确的。
@Mukesh - 我确信它适用于环境中的大多数人。不幸的是,它只能间歇性地为我工作(每20个刷新中就有一个会抛出语法错误而不是渲染页面)。遗憾的是,该网站仍在登台服务器上进行开发,因此我无法与您共享网址,仅包含我的代码和错误。