Php 如何在Woocommerce中以编程方式安排销售日期

Php 如何在Woocommerce中以编程方式安排销售日期,php,wordpress,woocommerce,product,hook-woocommerce,Php,Wordpress,Woocommerce,Product,Hook Woocommerce,我试图在woocommerce中安排保存(发布)帖子时的销售期 我试过下面两种方法,但都不起作用。代码在正确的时间被调用,只是没有更新post meta 这两种方法都没有更新销售计划 add_action('save_post_product', array($this, 'knpv_new_product_from_draft'), 10, 2); add_action('edit_post_product', array($this, 'knpv_new_product_from_draft

我试图在woocommerce中安排保存(发布)帖子时的销售期

我试过下面两种方法,但都不起作用。代码在正确的时间被调用,只是没有更新post meta

这两种方法都没有更新销售计划

add_action('save_post_product', array($this, 'knpv_new_product_from_draft'), 10, 2);
add_action('edit_post_product', array($this, 'knpv_new_product_from_draft'), 10, 2);
public function knpv_new_product_from_draft($post_id, $post){   


    //Get todays date and the date 15 days from now
    $datefrom = strtotime(date('Y-m-d'));
    $dateto = strtotime(date('Y-m-d', strtotime('+15 days',$datefrom)));

    //Method 1
    $product = wc_get_product($post_id);

    if( !empty(get_post_meta($post_id, '_sale_price', true)) ){         
        $product->set_date_on_sale_from( $datefrom );
        $product->set_date_on_sale_to( $dateto );
    }

    $product->save();       

    //Method 2    
    $var = update_post_meta($post_id, '_sale_price_dates_from', $datefrom);
    $var2 = update_post_meta($post_id, '_sale_price_dates_to',   $dateto);

}   

您可以使用以下方法之一:

第一条道路-自第三条:

add_action( 'woocommerce_admin_process_product_object', array($this, 'save_wc_product_meta_data') );
public function save_wc_product_meta_data($product) {   

    if( isset( $_POST['_sale_price'] ) && $_POST['_sale_price'] >= 0 ){
        $product->set_date_on_sale_from( strtotime(date('Y-m-d')));
        $product->set_date_on_sale_to( strtotime( date('Y-m-d', strtotime('+15 days'))));
    }
} 

第二种方式旧方式:

add_action( 'woocommerce_process_product_meta', array($this, 'save_wc_product_meta_data') );
public function save_wc_product_meta_data($product_id) {   

    if( get_post_meta($product_id, '_sale_price', true) >= 0 ){
        update_post_meta($product_id, '_sale_price_dates_from', strtotime(date('Y-m-d')));
        update_post_meta($product_id, '_sale_price_dates_to', strtotime( date('Y-m-d', strtotime('+15 days'))));
    }
} 
代码进入活动子主题(或活动主题)的functions.php文件。两种方法都有效


添加:

若要仅在post状态设置为“发布”时实现此操作,您可以在现有条件下向
语句添加以下内容:


这不起作用的原因是,在save_post操作完成后,元数据正在更新。所以我在更新meta,然后表单中的空值也在更新和清除它们

所以我就这样做了

add_action('save_post_product', array($this, 'knpv_new_product_from_draft'), 10, 2);
add_action('edit_post_product', array($this, 'knpv_new_product_from_draft'), 10, 2);
public function knpv_new_product_from_draft($post_id, $post){  
  //If the post is being published. 
  if (get_post_status($post_id) == 'publish') {

    //Set the values from the posted form data.
    $_POST['_sale_price_dates_from'] = date('Y-m-d');
    $_POST['_sale_price_dates_to'] = date('Y-m-d', strtotime($datefrom.' +15 days'));

  }
} 

谢谢你的回复,如果我能请你扩展的话。根据OP,我只希望在产品从草稿到发布时实现此功能。@AlexKnopp对于从草稿到发布状态的产品来说,这是一个不同的问题/答案,因为这不会由所有的钩子来处理。。。请接受此答案,因为它适用于您最初提出的问题,并且对其他人有用。然后问一个类似的新问题,专门询问从草稿到发布的转换状态。非常感谢。如果没有人在我之前回答你的新问题,我会回答。你可以在这里用新的问题链接通知我。原来的问题确实包括我试图在post save上这样做的事实。“我试图在woocommerce中安排保存(发布)帖子时的销售期。”@AlexKnopp我已经更新了我的答案,添加了一个选项,允许将代码限制为“发布”帖子状态。不幸的是,这不起作用。不过我已经设法把它分类了,我会把我的答案贴出来。
add_action('save_post_product', array($this, 'knpv_new_product_from_draft'), 10, 2);
add_action('edit_post_product', array($this, 'knpv_new_product_from_draft'), 10, 2);
public function knpv_new_product_from_draft($post_id, $post){  
  //If the post is being published. 
  if (get_post_status($post_id) == 'publish') {

    //Set the values from the posted form data.
    $_POST['_sale_price_dates_from'] = date('Y-m-d');
    $_POST['_sale_price_dates_to'] = date('Y-m-d', strtotime($datefrom.' +15 days'));

  }
}