Description
<?php
add_filter('woocommerce_get_price_html', 'custom_display_acf_price_addition', 10, 2);
function custom_display_acf_price_addition($price_html, $product) {
// Base product price
$base_price = floatval($product->get_regular_price());
$total_acf_price = 0;
// List of the ACF field groups (your repeaters)
$field_groups = [
'design_presentation',
'store_management',
'payments_shipping',
'marketing_seo',
'communication_support',
'security_infrastructure',
'training_support',
];
// Loop through each field group (repeater)
foreach ($field_groups as $group_key) {
// Check if the repeater field has rows (services)
if (have_rows($group_key, $product->get_id())) {
// Loop through each row in the repeater field
while (have_rows($group_key, $product->get_id())) {
the_row();
// Get the service price from the sub-field (service_price)
$price = get_sub_field('service_price');
// If the price exists, add it to the total price
if (!empty($price)) {
$total_acf_price += floatval($price);
}
}
}
}
// Total price including ACF services
$total_price = $base_price + $total_acf_price;
// Return the formatted price HTML
return '<span class="woocommerce-Price-amount amount">' . wc_price($total_price) . '</span>';
}
<?php
add_filter('woocommerce_get_price_html', 'custom_display_acf_price_addition', 10, 2);
function custom_display_acf_price_addition($price_html, $product) {
$base_price = floatval($product->get_regular_price());
$total_acf_price = 0;
$field_groups = [
'design_presentation',
'store_management',
'payments_shipping',
'marketing_seo',
'communication_support',
'security_infrastructure',
'training_support',
];
foreach ($field_groups as $group_key) {
if (have_rows($group_key, $product->get_id())) {
while (have_rows($group_key, $product->get_id())) {
the_row();
$price = get_sub_field('service_price');
if (!empty($price)) {
$total_acf_price += floatval($price);
}
}
}
}
// Debug: Log total ACF price to check if it is being calculated correctly
error_log('Total ACF Price: ' . $total_acf_price);
$total_price = $base_price + $total_acf_price;
return '<span class="woocommerce-Price-amount amount">' . wc_price($total_price) . '</span>';
}
Reviews
There are no reviews yet.