WooCommerce显示产品差异旁边的价格

时间:2016-03-06 12:33:07

标签: wordpress woocommerce woothemes

我有一个产品的变体列表,但我想显示它旁边的价格。这样,用户可以立即看到每个变体之间的价格差异。 http://cl.ly/15150P3D1U3k

任何方式如何在WooCommerce中知道我的网站是多语言的?

谢谢!

1 个答案:

答案 0 :(得分:0)

请试试这个。我刚刚在最新的WordPress和WooCommerce中测试过。只需将其添加到您的函数文件中即可。

add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );

function display_price_in_variation_option_name( $term ) {
    global $wpdb, $product;

    if ( empty( $term ) ) return $term;
    if ( empty( $product->id ) ) return $term;

    $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] );
         return $term . ' (' . wp_kses( woocommerce_price( $_product->get_price() ), array() ) . ')';
    }
    return $term;

}