Updating total value of cart dynamically in php using woocommerce products [closed]
Updating total value of cart dynamically in php using woocommerce products [closed]
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 days ago.
Improve this question
I'm quite new to coding, and I'm in way over my head at this point.I'm trying to create a multipage chechout flow plugin for my wordpress site.
On page 1: Select number of bags full of recycleable cans and give credit for each bag selected. I then store the credit using an AJAX Handler, and displaying that value on page 2.
On page 2: Successfully showing credit from page 1, and listing woocommerce products.
My issue: I need to subtract the value of the woocommerce products from the credit value displayed on page 2 dynamically. I had it working once, but moved on to styling the product page, and now i cant get it working again. I've tried rewriting it, and doing it in different ways, but i just cant get it to update the credit. It's just static.
This is my js for updating the credit:
jQuery(document).ready(function($) {
var initialCredit = parseFloat(rtsc_ajax.initial_credit);
updateCreditDisplay(initialCredit);
// Add event listener for checkbox changes and quantity input changes
$('input[type=checkbox][name="selected_products[]"], input[type=number][name^="product_quantity"]').on('change input', updateCredit);
function updateCredit() {
var totalSelected = 0;
$('input[type=checkbox][name="selected_products[]"]').each(function() {
var productId = $(this).val();
var quantity = parseInt($('input[name="product_quantity[' + productId + ']"]').val());
// Retrieve the product price from WC (I've defined it in my php)
var productPrice = parseFloat($('input[name="product_price[' + productId + ']"]').val());
console.log("Product ID: " + productId);
console.log("Quantity: " + quantity);
console.log("Product Price: " + productPrice);
if (quantity > 0) {
totalSelected += (productPrice * quantity);
}
});
var remainingCredit = initialCredit - totalSelected;
updateCreditDisplay(remainingCredit);
}
function updateCreditDisplay(credit) {
$('#rtsc_credit_amount').text(credit.toFixed(2));
}
This is how i show the credit on page 2:
$credit_amount = isset($_SESSION['rtsc_credit_amount']) ? $_SESSION['rtsc_credit_amount'] : 0;
$credit_amount = floatval($credit_amount);
echo '<div id="rtsc_credit_info">Your Credit: <span id="rtsc_credit_amount">' . esc_html($credit_amount) . '</span> kr</div>';
I think it stopped working after i changed how you select the product, insted of the standard checkbox i use the quantity input, and I'm showing it when clicking the product container:
jQuery(document).ready(function($) {
$(document).on('click', '.product-image', function() {
var container = $(this).closest('.rtsc-product-container');
var quantityContainer = container.find('.quantity-container');
var quantityInput = quantityContainer.find('.quantity-input');
var priceElement = container.find('.product-price');
// Toggle the visibility of quantity container
if (quantityContainer.is(":visible")) {
quantityContainer.hide();
quantityInput.val(0);
priceElement.css('padding-bottom', '46px');
} else {
quantityContainer.show();
quantityInput.val(1);
priceElement.css('padding-bottom', '0');
}
// Toggle the checkbox's state
var checkbox = container.find('.product-checkbox');
checkbox.prop('checked', !checkbox.prop('checked'));
});
});
Note: The value is not used for anything else than displaying it
Turbo Multi-language Translator
Make the better internet purchasing globaly
Turbosify SEO Speed Booster
5.0 (7)•Free plan available
Get better conversions by improving store loading speed
Installed
Turbo Multi-language Chat - AI Customer service in one hand