使用下拉菜单分期显示产品价格

我想添加一个下拉菜单,访问者可以在其中选择 12 个月,然后根据该选择,分期显示每月的价格。


假设我有一个价格为120 美元的产品。横幅将告知客户:


“您可以在 installemets 中购买商品,选择号码”


例如,客户将选择 3 个月,在下拉菜单下方的横幅中,结果将为40 美元/月


目前我的代码只能使用 12 个月。


add_action( 'woocommerce_after_add_to_cart_button', 'show_installments', 20 );

function show_installments() {

    global $product; 

    $id = $product->get_id();

    $product = wc_get_product( $id );

    $a   = $product->get_price();

    $b   = 12;

    $min = 25;

    $c   = round( $a / $b, 2);


   if ($a > $min)  {

       echo '<p class="p2_installments_12months">Purchase with'.$c.'€ per month for 12 months </p>';

   }   

}


汪汪一只猫
浏览 113回答 1
1回答

HUH函数

可以使用下面的代码,注意jQuery下拉框和编辑文本的交互也是需要的function show_installments() {&nbsp; &nbsp; global $product;&nbsp;&nbsp; &nbsp; // Get product id&nbsp; &nbsp;&nbsp; &nbsp; $product_id = $product->get_id();&nbsp; &nbsp; // Get price&nbsp; &nbsp; $price = $product->get_price();&nbsp; &nbsp; // Set min price&nbsp; &nbsp; $min_price = 25;&nbsp; &nbsp; if ( $price > $min_price ) {&nbsp; &nbsp; &nbsp; &nbsp; echo '<div class="my_select_box">';&nbsp; &nbsp; &nbsp; &nbsp; woocommerce_form_field( 'month_options', array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'type'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => 'select',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'label'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=> __('You can purchase the item in installemets, select the the number'),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'required'&nbsp; &nbsp; &nbsp; => false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'return'&nbsp; &nbsp; &nbsp; &nbsp;=> false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'options'&nbsp; &nbsp;=> array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ''&nbsp; &nbsp; &nbsp; => 'Select...',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '1'&nbsp; => '1 month',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '2'&nbsp; => '2 months',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '3'&nbsp; => '3 months',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '4'&nbsp; => '4 months',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '5'&nbsp; => '5 months',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '6'&nbsp; => '6 months',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '7'&nbsp; => '7 months',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '8'&nbsp; => '8 months',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '9'&nbsp; => '9 months',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '10'&nbsp; => '10 months',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '11'&nbsp; => '11 months',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '12'&nbsp; => '12 months',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; ), '' );&nbsp; &nbsp; &nbsp; &nbsp; echo '</div>';&nbsp; &nbsp; &nbsp; &nbsp; echo '<div class="p2_installments_12months"></div>';&nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; <script type="text/javascript">&nbsp; &nbsp; &nbsp; &nbsp; jQuery(document).ready(function ($) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var product_price = <?php echo $product->get_price(); ?>;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $( '[name=month_options]' ).change(function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var dropdown_val = this.value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( dropdown_val >= 1 ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var price_per_month = ( product_price / dropdown_val ).toFixed(2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $( '.p2_installments_12months' ).html( '<p class="p2_installments_12months">Purchase with ' + price_per_month + ' € per month for 12 months </p>' );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $( '.p2_installments_12months' ).html( '<p class="p2_installments_12months"></p>' );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; </script>&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; }&nbsp; &nbsp;}add_action( 'woocommerce_after_add_to_cart_button', 'show_installments', 20 );
打开App,查看更多内容
随时随地看视频慕课网APP