Skip to content

Instantly share code, notes, and snippets.

@glebsneg
Created May 6, 2025 13:25
Show Gist options
  • Save glebsneg/475912868a0e0e7fd89270c6a4dd2af3 to your computer and use it in GitHub Desktop.
Save glebsneg/475912868a0e0e7fd89270c6a4dd2af3 to your computer and use it in GitHub Desktop.
Demo WooCommerce with ACF
document.addEventListener('DOMContentLoaded', function () {
const priceElement = document.querySelector('.woocommerce-Price-amount bdi');
const extraPriceElement = document.querySelector('.acf-extra-price');
if (priceElement && extraPriceElement) {
const basePrice = parseFloat(priceElement.textContent.replace(/[^\d.]/g, ''));
const extra = parseFloat(extraPriceElement.dataset.extra);
if (!isNaN(basePrice) && !isNaN(extra)) {
const newPrice = basePrice + extra;
priceElement.textContent = newPrice.toFixed(2);
}
}
});
<?php
// Вывод дополнительной цены на странице товара
add_action('woocommerce_before_add_to_cart_button', 'show_acf_extra_price');
function show_acf_extra_price() {
$extra_price = get_field('extra_price'); // ACF поле
if ($extra_price) {
echo '<div class="acf-extra-price" data-extra="' . esc_attr($extra_price) . '">';
echo 'Дополнительная цена: <strong>+' . wc_price($extra_price) . '</strong>';
echo '</div>';
}
}
// Подключение скрипта для обновления цены
add_action('wp_enqueue_scripts', function() {
if (is_product()) {
wp_enqueue_script('acf-extra-price-update', get_template_directory_uri() . '/js/acf-price.js', ['jquery'], null, true);
}
});
.acf-extra-price {
margin-top: 10px;
font-size: 16px;
color: #444;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment