亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何以編程方式在 Woocommerce 中安排銷售日期

如何以編程方式在 Woocommerce 中安排銷售日期

PHP
慕田峪4524236 2023-04-21 15:13:34
我試圖在保存(發布)帖子時安排 woocommerce 中的銷售期。我已經嘗試了下面的兩種方法,但它們都不起作用。代碼在正確的時間被調用,只是不更新帖子元。這兩種方法都沒有更新銷售時間表。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);}   
查看完整描述

2 回答

?
侃侃無極

TA貢獻2051條經驗 獲得超10個贊

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


第一種方式- 自 WooCommerce 3 以來:


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 文件。兩種方式都有效。


添加:


要僅在發布狀態設置為 "publish" 時發生這種情況,您可以將以下內容添加到IF語句現有條件中:


&& isset($_POST['post_status']) && $_POST['post_status'] === 'publish'


查看完整回答
反對 回復 2023-04-21
?
慕虎7371278

TA貢獻1802條經驗 獲得超4個贊

這不起作用的原因是元數據在 save_post 操作完成后正在更新。所以我正在更新元數據,然后表單中的空值也在更新并清除它們。


所以我這樣做了。


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'));


  }


查看完整回答
反對 回復 2023-04-21
  • 2 回答
  • 0 關注
  • 126 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號