<script> window.addEventListener('load', function () { document.getElementById("amount_number").addEventListener("blur", function() { let inputValue = this.value ; console.log(inputValue); const dataluq = new FormData(); dataluq.append( 'action', 'woochargesiusersetamount' ); dataluq.append( 'amount_number', inputValue ); fetch('<?php echo admin_url( 'admin-ajax.php' ) ; ?>', { method: 'POST', // or 'PUT' body: dataluq }) .then(response => response.json()) .then(data => { console.log(data); let event = new Event("update_checkout"); document.body.dispatchEvent(event); } ); }); }) </script>
before calculate total hook
add_action('woocommerce_before_calculate_totals', $plugin_public, 'luq_woocommerce_before_calculate_totals', 10); public function luq_woocommerce_before_calculate_totals( $cart_object ) { if( !isset($_SESSION) ) session_start(); if($_REQUEST['lprice']){ $_SESSION['amount_number'] = $_REQUEST['lprice']; } if(isset($_SESSION['amount_number']) ){ foreach ( $cart_object->get_cart() as $hash => $value ) { // check if the product is in a specific category and check if its ID isn't 12345 if($value['product_id'] == 726 ) { $value['data']->set_price(intval($_SESSION['amount_number'])); } } } }