在 Woocommerce 中选择产品变体价格后添加自定义动态文本

Add a custom dynamic text after the product variation selected price in Woocommerce

我是 woocommerce 的新手,我尝试了一周以低于可变价格显示 4 倍付款的建议。

我尝试了在网上找到的几种技术和代码,但 none 取得了不错的效果。

我能做到的最好的是:

//Afficher paiement 3xcb//


add_filter('woocommerce_get_price_suffix', 'display_3xcb', 99, 4);

function display_3xcb($html, $price, $product) {
 global $post, $product;
 $variation_id = '7575';
 $cb_price = get_post_meta($variation_id, '_sale_price', true);
 $cb_price = ($cb_price/4);
 $html = '<p> Ou simplement en 4 x ' . $cb_price . '€ sans frais</p>'; 
 return $html;
}

但我的问题是我的 $variation_id 不是动态的。

您知道它如何检索 Selected 变量的 ID 吗?

function the_product_price() {
    global $product;
    global $woocommerce;
    $regular_price = get_post_meta(get_the_ID(), '_regular_price', true);
    if ($regular_price == "") {
        $available_variations = $product->get_available_variations();
        if ($available_variations) {
            $variation_id = $available_variations[0]['variation_id'];  
            $variable_product1 = new WC_Product_Variation($variation_id);
            $regular_price = $variable_product1->regular_price;
        }
    }
    echo '<span class="price"><span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">'.get_woocommerce_currency_symbol().' </span>'.$regular_price.'</span></span>';

}

以上函数获取产品特定顺序价格的正常价格。

对于所选价格的产品变体添加后缀,请改用:

add_filter( 'woocommerce_available_variation', 'custom_variation_price_addition', 10, 3 );
function custom_variation_price_addition( $data, $product, $variation ) {
    $price  = wc_get_price_to_display( $variation );
    $suffix = sprintf( __("Ou simplement en 4 x %s sans frais"), wc_price($price / 4) );

    $data['price_html'] .= '<span class="4xcb"> ' . $suffix . '</span>';

    return $data;
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。