在帖子创建期间计算 PHP date_diff 并保存到 ACF 字段

Posted

技术标签:

【中文标题】在帖子创建期间计算 PHP date_diff 并保存到 ACF 字段【英文标题】:Calculate PHP date_diff and save to ACF Field during post creation 【发布时间】:2022-01-02 17:50:22 【问题描述】:

我现在正面临以下挑战,如果有人可以帮助我,我将不胜感激。

我有一个 WooCommerce 商店,它会在每个订单完成后自动创建一个新帖子。这些订单保存在自定义帖子类型中,订单详细信息位于 ACF 转发器字段中。对于这个函数,我使用了下面提到的代码。

现在我想在我的代码中添加一个额外的函数来计算订单日期和今天之间的天数。订购日期等于存储帖子的发布日期。所以实际上计算应该基于发布日期和今天。计算完成后,我想将天数存储到 ACF 数字字段中。

例如,如果有人在 11 月 1 日订购了产品,而今天是 11 月 24 日,则结果应该是 23。

我现在使用的代码。

//Create post

function create_post_after_order( $order_id ) 
  if (  $order_id instanceof WC_Order )
    return;
  

//Find items based on orderID
     
  $order = wc_get_order( $order_id );
  $order_items = $order->get_items(); 

//Loop through items
    
    foreach ( $order_items as $item_id => $item_data ) 
    $product_ids[]       = $item_data->get_product_id();
    $product_names[]     = $item_data->get_name();
    $product_quantities[] = $item_data->get_quantity(); 
    $ordeline_subtotals[]     = $item_data->get_subtotal(); 

    $product_details = $item_data->get_product();

    // Get the product price that customer paid
    $product_prices[]        = $product_details->get_price(); 

    //Get sale price (i.e discounted price, if it exists)
    $product_regular_price  = $product_details->get_sale_price(); 

    //Regular price.
    $product_sale_price     = $product_details->get_regular_price();


//Create actual post
    
    $new_post = array(
    'post_title' => "Order $order_id", 
    'post_date' => date('Y-m-d H:i:s'), 
    'post_author' => $user_ID, 
    'post_type' => 'groeiproces', 
    'post_status' => 'publish',
  );    

  $post_id = wp_insert_post($new_post);
    
//Connect ACF Fields

    $orderdetails_key = 'field_61645b866cbd6';
    $product_id_key = 'field_6166a67234fa3';
    $product_name_key = 'field_61645b916cbd7';
    $product_price_key = 'field_6166a68134fa4';
    $product_quantity_key = 'field_6165bd2101987';
    $ordeline_subtotal_key = 'field_6166a68934fa5';
    $product_id = $product_ids;
    $product_name = $product_names;
    $product_price = $product_prices;
    $product_quantity = $product_quantities;
    $ordeline_subtotal = $ordeline_subtotals;

//Save orderdata in ACF repeater field

foreach ($product_id as $index => $product_id) 
    $orderdetails_value[] = array(
    $product_id_key => $product_id, 
    $product_name_key => $product_name[$index],
    $product_price_key => $product_price[$index],
    $product_quantity_key => $product_quantity[$index],
    $ordeline_subtotal_key => $ordeline_subtotal[$index],
);
    update_field( $orderdetails_key, $orderdetails_value, $post_id );

    

add_action( 'woocommerce_thankyou', 'create_post_after_order', 10, 1 );

【问题讨论】:

$user_ID 在这个范围内是如何定义的?此外,该代码中的 date_diff 应该在哪里?你能在某处发表评论吗? 商店是私有的,因此 user_id 由当前登录用户定义。此用户将作为帖子作者添加到帖子中。这使我可以仅在前端用户仪表板中显示与用户相关的帖子。我认为在循环之后我应该添加函数来计算 date_diff。然后我可以使用该参数保存在 ACF 字段中。 应该存储到哪个 ACF 字段?由于$orderdetails_value 是一个数组,你想在其中添加另一个项目还是什么? 不,这是另一个 ACF 字段。这不是一个数组。字段名称为“datum”,字段 ID 为“field_619e20f8a9763”。 【参考方案1】:

由于您的函数中已经有 Order 对象,因此您只需在插入帖子后的某个时间点添加它。

$order_date = date_create( $order->get_date_created() );
$date_diff = date_diff( $order_date, date_create( date( 'Y-m-d' ) ) );

update_field( 'field_619e20f8a9763', $date_diff->days, $post_id );

【讨论】:

以上是关于在帖子创建期间计算 PHP date_diff 并保存到 ACF 字段的主要内容,如果未能解决你的问题,请参考以下文章

php,当 number_format() 遇到 date_diff()

presto计算日期间隔天数或者小时间隔——date_diff函数使用

DATE_DIFF()

使用php计算日期范围内的天数?

即使在创建帖子期间使用 Postman 将文件上传到该字段后,Django Rest Framework 仍显示字段错误

php 使用WP REST API计算纬度和经度坐标之间的距离,并返回按用户c的距离排序的帖子