将正常价格和销售价格放在 woocommerce 变化下拉菜单上

第一次在这里发帖,请多多包涵。我正在使用以下代码:


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] );

        return $term . woocommerce_price( $_product->get_price() );

    }


    return $term;

}

add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );

此代码在变体菜单中的每个项目旁边放置一个价格。我想做的是让它也发布一个销售价格并通过正常价格(如,这里是正常价格和销售价格)。


我尝试了以下方法:


            $regular_price = woocommerce_price( $_product->get_regular_price() );

            $sale_price = woocommerce_price( $_product->get_price() );

            return $term . $regular_price . $sale_price;

虽然这确实增加了销售价格,但它也有销售价格和正常价格重叠。我想我正在寻找的是一种格式化价格的方法。


任何帮助表示赞赏。


ITMISS
浏览 137回答 1
1回答

紫衣仙女

您可以在过滤器中添加一些 HTML 以添加空格并删除原始价格。您可以使用以下代码&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $regular_price = woocommerce_price( $_product->get_regular_price() );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $sale_price = woocommerce_price( $_product->get_price() );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $term . '&nbsp' . '<strike>' . $regular_price . '</strike>'&nbsp; . '&nbsp'. $sale_price;
打开App,查看更多内容
随时随地看视频慕课网APP