You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress表单:将日期差值设为ID18商品的购物车添加数量

Hey there! Let's tackle this problem so you can add the exact number of days (like 23) as the quantity for product ID 18 to your cart. Here's how to do it step by step:

Solution: Add Date Difference as Product Quantity to Cart

First, we need two key parts: calculating the date difference correctly, then modifying the add-to-cart URL to include that quantity value (most WordPress e-commerce plugins support a quantity parameter in the add-to-cart query string).

Step 1: Calculate the Date Difference

Create a JavaScript function to compute the number of days between your two date inputs. This handles valid date checks and ensures we get a positive integer:

function getDateDifference(startDateInputId, endDateInputId) {
  // Get the date values from your form inputs
  const startDate = new Date(document.getElementById(startDateInputId).value);
  const endDate = new Date(document.getElementById(endDateInputId).value);

  // Validate the dates to avoid errors
  if (isNaN(startDate.getTime()) || isNaN(endDate.getTime())) {
    alert("Please enter valid start and end dates!");
    return 0;
  }

  // Calculate the difference in milliseconds, convert to full days
  const timeDifference = Math.abs(endDate.getTime() - startDate.getTime());
  const dayDifference = Math.floor(timeDifference / (1000 * 60 * 60 * 24));

  // Ensure we don't return a negative or zero value
  return dayDifference > 0 ? dayDifference : 1;
}

Step 2: Update the OnClick Event to Pass Quantity

Modify your button's OnClick logic to use the calculated day difference as the quantity parameter in the add-to-cart URL. This tells your store plugin to add the correct number of items:

// Bind this function to your button's OnClick event
function addDaysAsQuantityToCart() {
  // Replace these IDs with your actual date input field IDs
  const dayCount = getDateDifference("start-date", "end-date");
  
  if (dayCount === 0) return;

  // Construct the cart URL with the quantity parameter
  const cartUrl = `http://localhost/travelagent/?add-to-cart=18&quantity=${dayCount}`;
  window.location.href = cartUrl;
}

How to Implement This in Your WordPress Form Plugin

  • In your form builder, set the button's OnClick event to call addDaysAsQuantityToCart() (instead of the original direct URL redirect).
  • Double-check that your date input fields have the correct IDs (match what you used in getDateDifference).
  • Test with your example: if the date difference is 23 days, the URL will become http://localhost/travelagent/?add-to-cart=18&quantity=23, which adds 23 units of product ID 18 to the cart.

Quick Notes

  • If you're using a plugin other than WooCommerce, check its documentation—most e-commerce tools support a similar quantity query parameter for add-to-cart actions.
  • Add extra validation if needed (e.g., ensuring the end date is after the start date) to avoid unexpected quantity values.

内容的提问来源于stack exchange,提问作者Juan Sánchez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 10:16:30