我是 woocommerce 的新手,我尝试用一周的时间在可变价格以下显示 4 倍付款的建议。
我尝试了在网上找到的几种技术和代码,但没有一个取得好的结果。
我能做到的最好的就是这个:
//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
不是动态的。
您知道如何检索所选变量的 id 吗?
对于产品变化,请在所选价格后添加后缀,改为使用:
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 文件中。已测试并有效。
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>';
}
上述函数获取产品特定连续价格的正常价格。