我为函数添加了各种代码更改。php。其中许多依赖于某些活动插件,因为它们引用这些插件定义的类。
如果插件被禁用,将导致500服务器错误,因为找不到相应的类。
有没有办法先考一门课?
例如,以下函数依赖于WooCommerce扩展中的类。
// Add prices to variations
add_filter( \'woocommerce_variation_option_name\', \'display_price_in_variation_option_name\' );
function display_price_in_variation_option_name( $term ) {
global $wpdb, $product;
$result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = \'$term\'" );
$term_slug = ( !empty( $result ) ) ? $result[0] : $term;
$query = "SELECT postmeta.post_id AS product_id FROM {$wpdb->prefix}postmeta AS postmeta LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id ) WHERE postmeta.meta_key LIKE \'attribute_%\' AND postmeta.meta_value = \'$term_slug\' AND products.post_parent = $product->id";
$variation_id = $wpdb->get_col( $query );
$parent = wp_get_post_parent_id( $variation_id[0] );
if ( $parent > 0 ) { $_product = new WC_Product_Variation( $variation_id[0] );
//$itemPrice = \' \' . strip_tags (woocommerce_price( $_product->get_price() ));
//this is where you can actually customize how the price is displayed
//return $term . \' (\' . $itemPrice . \')\';
$curr = get_woocommerce_currency_symbol(); //get_option(\'woocommerce_currency\');
$regular_price = trim($_product->regular_price);
$sales_price = trim($_product->sale_price);
if($sales_price == \'\' || $regular_price == $sales_price) { return $term . \' (\' . $curr.$regular_price . \')\'; }
else { return $term . \' (Was \' . $curr.$regular_price . \', Now \' . $curr.$sales_price . \')\'; }
} return $term;
}
global $woocommerce;
$product_variation = new WC_Product_Variation($_POST[\'variation_id\']);
$regular_price = $product_variation->regular_price;
但是如果相关插件被禁用,PHP将生成错误,
"PHP message: PHP Fatal error: Uncaught Error: Class \'WC_Product_Variation\' not found in ... /functions.php:266
所以我需要先测试一下WC_Product_Variation
在运行此函数之前定义。