【问题标题】:Display a custom installment price below product price in WooCommerce在 WooCommerce 中显示低于产品价格的自定义分期付款价格
【发布时间】:2021-04-29 18:13:49
【问题描述】:

我计划在我的 WooCommerce 产品页面上显示所有产品的新价格。这是每月的分期付款价格。我需要在正常价格(可变价格和简单价格)以下显示,如下所示:

“每月付款”总计=正常价格/月(我想要 3、6、8、12 等)我知道我必须每行添加它 我已经尝试过使用此代码,但它没有显示任何内容 - 我开始只有 3 个月。零利息所以真的是价格/3

add_action( 'woocommerce_single_product_summary', 'show_emi', 20 );
function show_emi() {
   global $product; 

   $id = $product->get_id();

   $product = wc_get_product( $id );

   $a = $product->get_price();
   $b = 3;
   $total = $a/$b;
   
   echo $total;
}

有人可以帮我编写代码(我真的不擅长)以显示我想要的文本吗?

【问题讨论】:

  • @PullataPraveen 我看到你回答了一个类似的问题,希望你也能在这里提供帮助。提前谢谢你
  • 拜托,我们将不胜感激。

标签: php wordpress woocommerce product price


【解决方案1】:

尝试以下示例,该示例将在产品和可变产品变体的单个产品页面上显示低于产品价格的每月格式化分期付款价格:

// Custom function that gets installment formatted price string *(the default divider value is 3)*
function get_installment_price_html( $active_price, $divider = 3 ) {
    return sprintf( __("installment: %s  per month", "woocommerce"), wc_price( get_installment_price( $active_price / $divider ) ) );
}

// Display installment formatted price on all product types except variable
add_action( 'woocommerce_single_product_summary', 'display_product_installment_price', 15 );
function display_product_installment_price() {
    global $product; 

    // Not for variable products
    if ( $product->is_type( 'variable' ) ) {
        // get active price for display
        $active_price = wc_get_price_to_display( $product );

        if ( $active_price ) {
            // Display formatted installment price below product price
            echo get_installment_price_html( $active_price );
        }
    }
}

// Display installment formatted price on product variations
add_filter( 'woocommerce_available_variation', 'display_variation_installment_price', 10, 3) ;
function display_variation_installment_price( $variation_data, $product, $variation ) {
    $active_price = $variation_data['display_price']; // Get active price for display

    if ( $active_price ) {
        // Display formatted installment price below product price
        $variation_data['price_html'] .= '<br>' . get_installment_price_html( $active_price );
    }
    return $variation_data;
}

代码位于活动子主题(或活动主题)的functions.php 文件中。它应该可以工作。

注意:您需要进行一些更改,以获得您在问题中描述的不同分期付款价格……

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-08
    • 2013-02-25
    • 2022-12-03
    • 1970-01-01
    • 2021-02-04
    • 2017-11-24
    • 2020-11-13
    相关资源
    最近更新 更多