(希望这很有意义) 我需要能够在您进行woocommerce时显示变体的单独价格,以便客户知道变化差异有多大,或者我需要降低价格以便在视觉上有所改变。
我使用了以下代码
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] );
$_currency = get_woocommerce_currency_symbol();
return $term . ' ('.$_currency.' '. $_product->get_price() . ')';
}
return $term;
}
但正如您将从此图像中看到的那样,它无法正确显示并且看起来有点令人困惑。 [Running The Queue Worker
非常感谢任何帮助。