gpt4 book ai didi

php - 在添加到购物车之前更改 WooCommerce 订阅产品开始日期

转载 作者:行者123 更新时间:2023-12-02 17:03:15 26 4
gpt4 key购买 nike

我的一个使用 WooCommerce 订阅插件的 WooCommerce 网站有问题。

问题是一种产品仅在特定日期每 2 周发货一次。

下一个发货日期是 11 月 9 日。

我需要弄清楚如何将此添加为产品的默认开始日期。

我尝试使用 WooCommerce 订阅插件的 WC_Subscriptions_Synchroniser 类中描述的功能。

我启用了同步续订功能。

enter image description here

并且由于 11 月 9 日是星期五,还将同步续订日期更改为每周的星期五。

enter image description here

但是,这使得第一个续订日期为 11 月 2 日,也就是即将到来的星期五。

enter image description here

我想弄清楚如何将开始日期延迟到 11 月 9 日,而不是现在显示的 11 月 2 日。

感谢任何建议。

最佳答案

同样的问题。我设法准备了一个 future 的开始日期,但对此有一个硬编码限制。

查看 wcs_function.php 中的这段代码

// validate the start_date field
if ( ! is_string( $args['start_date'] ) || false === wcs_is_datetime_mysql_format( $args['start_date'] ) ) {
return new WP_Error( 'woocommerce_subscription_invalid_start_date_format', _x( 'Invalid date. The date must be a string and of the format: "Y-m-d H:i:s".', 'Error message while creating a subscription', 'woocommerce-subscriptions' ) );
} else if ( wcs_date_to_time( $args['start_date'] ) > current_time( 'timestamp', true ) ) {
return new WP_Error( 'woocommerce_subscription_invalid_start_date', _x( 'Subscription start date must be before current day.', 'Error message while creating a subscription', 'woocommerce-subscriptions' ) );
}

以后不可能创建订阅。这可能与订阅插件限制有关,或者订阅问题的方法非常狭窄。

这意味着即使像我一样通过 woocommerce_add_cart_item_datawcs_recurring_cart_start_datewoocommerce_get_item_data 处理购物车项目属性,您最终也会碰壁。 您要么需要更改 Woocommerce 订阅硬限制,要么非常有创意。

现在,我将向您展示我涉及的 3 个操作/过滤器,这很简单。我已经简化了这些内容,因为它涉及您可能不需要的日期验证和格式设置。

您的产品页面中需要一个 my_delay_post_attr POST 字段。

首先将您在产品页面上添加的一些自定义字段保存到购物车项目中。

add_filter('woocommerce_add_cart_item_data', 'my_woocommerce_add_cart_item_data', 10, 1);
/**
* Read a Y-m-d H:i:s formatted (mysql) date from POST data
* then store it in the cart item.
* @param $cart_item_data array
*/
function my_woocommerce_add_cart_item_data($cart_item_data)
{
if (@$_POST['my_delay_post_attr']) {
$cart_item_data['my_delay_post_attr'] = $_POST['my_delay_post_attr'];
}

return $cart_item_data;
}

现在,让我们在购物车 -> 订购步骤中更改开始日期:

add_filter('wcs_recurring_cart_start_date', 'my_wcs_recurring_cart_start_date', 10, 2);
/**
* @param string $date
* @param \WC_Cart $recurring_cart
*
* @return mixed
*/
function my_wcs_recurring_cart_start_date($date, $recurring_cart) {
$cartContents = $recurring_cart->cart_contents;

if (!$cartContents) {
return $date;
}

if (!count($cartContents)) {
return $date;
}

$key = array_keys($cartContents)[0];

// I'd suggest you not to trust this, and apply some verification here
return @$cartContents[$key]['my_delay_post_attr];
}

最后在购物车页面显示此信息:

add_filter( 'woocommerce_get_item_data', 'my_woocommerce_get_item_data', 10, 2 );

public static function my_woocommerce_get_item_data( $item_data, $cart_item ) {
if (!@$cart_item['my_delay_post_attr']) {
return $item_data;
}

$item_data[] = array(
'key' => 'Start of the subscription',
'value' => $cart_item['my_delay_post_attr'],
'display' => '',
);

return $item_data;
}

编辑:我已经获得 Prospress 支持,这就是他们对 future 订阅的硬性限制的回答:

I understand you're trying to manually create a subscription and are asking about the limitation around the start date needing to be in the future. This limitation is there to support the gateways, as some are very specific and rigid around the timing of subscriptions. You may be safe to create a workaround for that check, but we can't make any promises - the check is there for a reason. If you are insistent on removing it, please run thorough testing on a development environment first to ensure there are no issues with your gateway.

编辑2:

我可以确认 PAYPAL 无法使用此技巧。至于 Prospress 的回应:

We are unsure how Stripe and Mercanet would handle this change. (We do know for sure that PayPal Standard, for example, would not handle this well.)

If you decide to move forward with this, we recommend testing very thoroughly. However, I wonder if there is a more simple option to accomplish what you are trying to achieve? Do you need the actual creation date to be in the future, or do you just need the first payment to be in the future?

If the latter, rather than trying to change date_created, you might consider using a free trial or creating a false free trial. This is the functionality used for subscription synchronization. Also, we recently separated out date_created and start_date in the database. While this functionality isn't fully developed yet, it might give you another point to work with as you move forward with your customization.

关于php - 在添加到购物车之前更改 WooCommerce 订阅产品开始日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53033282/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com