在 WooCommerce 中显示低于产品价格的自定义分期付款价格

时间:2021-01-25 22:23:54

标签: php wordpress woocommerce product price

我打算在我的 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;
}

有人可以帮助我用代码(我真的不擅长)来显示我想要的文字吗?

1 个答案:

答案 0 :(得分:0)

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

// 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 文件中。它应该有效。

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