Created
May 6, 2025 13:25
-
-
Save glebsneg/475912868a0e0e7fd89270c6a4dd2af3 to your computer and use it in GitHub Desktop.
Demo WooCommerce with ACF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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