Change the product id and then number of days in front of it.
Add this class to target section: hide-completion-section
PHP Code:
// Hide Elementor section by class and show message until unlocked
add_action('wp_footer', 'auto_unlock_section_script');
function auto_unlock_section_script() {
// Only run for logged-in users
if (!is_user_logged_in()) return;
$user_id = get_current_user_id();
// 🔧 Define product IDs and unlock delays (in days)
$product_unlock_days = [
1361 => 4,
1362 => 7,
1364 => 10,
1365 => 14,
1366 => 55,
];
// ✅ Get all completed WooCommerce orders for the user
$orders = wc_get_orders([
'customer_id' => $user_id,
'status' => 'completed',
'limit' => -1,
'orderby' => 'date_completed',
'order' => 'ASC'
]);
$earliest_unlock_timestamp = null;
// 🔠Loop through all orders and products to find unlock date
foreach ($orders as $order) {
foreach ($order->get_items() as $item) {
$product_id = $item->get_product_id();
// 🎯 Check if product is in our unlock list
if (array_key_exists($product_id, $product_unlock_days)) {
$order_date = strtotime($order->get_date_completed());
$unlock_after_days = $product_unlock_days[$product_id];
// â³ Calculate unlock date for that product
$unlock_date = strtotime("+$unlock_after_days days", $order_date);
// Keep the **earliest** unlock date across all products
if (is_null($earliest_unlock_timestamp) || $unlock_date < $earliest_unlock_timestamp) {
$earliest_unlock_timestamp = $unlock_date;
}
}
}
}
$now = time();
// 🧠Determine current unlock status + message
if (is_null($earliest_unlock_timestamp)) {
$status = 'locked';
$message = '🔒 This section is locked. Please purchase a product to unlock.';
} elseif ($now >= $earliest_unlock_timestamp) {
$status = 'unlocked';
$message = '';
} else {
$status = 'locked';
$days_remaining = ceil(($earliest_unlock_timestamp - $now) / 86400);
$message = '🔒 This section will unlock in ' . $days_remaining . ' day' . ($days_remaining > 1 ? 's' : '') . '.';
}
// ✅ Output inline JavaScript and CSS to manage visibility
?>
<script>
document.addEventListener('DOMContentLoaded', function () {
const sections = document.querySelectorAll('.hide-completion-section');
const status = '<?php echo esc_js($status); ?>';
const message = <?php echo json_encode($message); ?>;
sections.forEach(function(section) {
if (status === 'unlocked') {
// 🔓 Show section by removing custom 'display' override
section.style.removeProperty('display');
} else {
// 🔒 Add remaining days message and hide section
const msgEl = document.createElement('div');
msgEl.className = 'unlock-countdown-message';
msgEl.textContent = message;
section.parentNode.insertBefore(msgEl, section);
section.style.display = 'none';
}
});
});
</script>
<style>
/* 🎨 Style for the countdown/unlock message */
.unlock-countdown-message {
margin: 1em 0;
color: #cc0000;
font-style: italic;
}
</style>
<?php
}
