感谢任何可以提供帮助的人。我正在尝试使用 PHP 获取从任何给定当天算起 X 天的交货日期。这与 WordPress 中的 Google 调查选择加入代码和 WooCommerce 一起使用。
参考这个线程:Google 调查选择加入代码的 WooCommerce 填写字段 https://stackoverflow.com/questions/61844607/woocommerce-fill-in-fields-for-google-survey-opt-in-code
谷歌想要动态值,解释如下:
我已经准备好了大部分代码,但这个动态日期很难弄清楚。
我认为最简单的解决方案是在产品订单当天添加天数,这可能发生在任何一天。
我的问题是:如何让 PHP 在这种情况下计算它?
我的理解是,有 DateTime 和 strtotime,但 DateTime 是执行此操作的更新且“正确”的方法?
这是我到目前为止所得到的,但我不确定它是否正确:
//Google Survey code
function wh_CustomReadOrder($order_id) {
//getting order object
$order = wc_get_order($order_id);
$email = $order->billing_email;
?>
<script src="https://apis.google.com/js/platform.js?onload=renderOptIn" async defer></script>
<script>
window.renderOptIn = function () {
window.gapi.load('surveyoptin', function () {
window.gapi.surveyoptin.render(
{
"merchant_id": [merchant id],
"order_id": "<?php echo $order_id; ?>",
"email": "<?php echo $email; ?>",
"delivery_country": "CA",
"estimated_delivery_date": "<?php
$inOneWeek = new \DateTime("+7 day");
echo $date->format("Y-m-d");
?>"
}
);
});
};
</script>
<?php
}
add_action('woocommerce_thankyou', 'wh_CustomReadOrder');
您可以通过以下方式应用它,在代码中添加注释和解释。
使用的函数:
-
date_i18n() https://developer.wordpress.org/reference/functions/date_i18n/- 基于 Unix 时间戳和时区偏移量(以秒为单位)的总和,检索本地化格式的日期。
-
date https://www.php.net/manual/en/function.date.php - Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given. In other words, timestamp is optional and defaults to the value of time().
- Y - 年份的完整数字表示形式,4 位数字
- m - 月份的数字表示形式,带前导零
- d - 一个月中的第几天,2 位数字(带前导零)
还使用:“如何获取 WooCommerce 订单详细信息” https://stackoverflow.com/questions/39401393/how-to-get-woocommerce-order-details/
//Google Survey code
function wh_CustomReadOrder($order_id) {
// Get order object
$order = wc_get_order($order_id);
// Get billing email
$email = $order->get_billing_email();
// Get order date
$date_created = $order->get_date_created();
// Add days
$days = 7;
// Date created + 7 days
$estimated_delivery_date = date_i18n( 'Y-m-d', strtotime( $date_created ) + ( $days * 24 * 60 * 60 ) );
?>
<script src="https://apis.google.com/js/platform.js?onload=renderOptIn" async defer></script>
<script>
window.renderOptIn = function () {
window.gapi.load('surveyoptin', function () {
window.gapi.surveyoptin.render({
"merchant_id": [merchant id],
"order_id": "<?php echo $order_id; ?>",
"email": "<?php echo $email; ?>",
"delivery_country": "CA",
"estimated_delivery_date": "<?php echo $estimated_delivery_date; ?>"
});
});
};
</script>
<?php
}
add_action('woocommerce_thankyou', 'wh_CustomReadOrder', 10, 1 );
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)