[英]Set the product category to New in for imported products automatically in woocommerce
我將嘗試解釋我的目標是什么以及我已經做了什么。
我正在將一堆產品導入 WooCommerce,但現在我想添加一個功能,如果產品不超過 60 天,它會自動將產品類別設置為“新產品”。 在 60 天之后,它應該再次移出該類別。
可以說,我正在使用WordPress和WooCommerce學習php,所以如果我不知道某些事情,我很抱歉。
function add_product_to_new_in()
{
global $product;
// Get the date for the product published and current date
$start = date('n/h/Y', strtotime($product->get_date_created()));
$today = date('n/j/Y');
// Get the date for the start of the event and today's date
$start = new \DateTime($start);
$end = new \DateTime($today);
//FInd the difference
$difference = $start->diff($end);
$days = $difference->d;
// If the difference is less than 60, apply "NEW IN cat"
if ($days = (60 < $days)) {
wp_set_object_terms($product, 40, 'product_cat');
}
}
如果您能以任何方式幫助我,我將不勝感激!
您可以在每次頁面加載時觸發的init
鈎子上執行此操作。
首先,您必須獲得所有產品。
$args = array(
'post_type' => 'product',
'posts_per_page' => -1
);
$products = new WP_Query( $args );
if ( $products->have_posts() ) { while ( $products->have_posts() ) { $products->the_post();
// your logic will go here.
} wp_reset_postdata(); }
現在計算天數。
$product = wc_get_product( get_the_ID() );
$datetime = $product->get_date_created();
$timezone = $datetime->getTimezone();
$now_time = new WC_DateTime();
$now_time->setTimezone($timezone);
$timestamp_diff = $now_time->getTimestamp() - $datetime->getTimestamp();
$data = timestamp_to_array( $timestamp_diff );
$days = $data['d'];
然后你可以使用wp_set_object_terms
和wp_remove_object_terms
來設置和刪除類別。
// If the difference is less than 60, apply "NEW IN cat"
if ( $days < 60 ) {
wp_set_object_terms( get_the_ID(), 40, 'product_cat' );
}else{
wp_remove_object_terms( get_the_ID(), 40, 'product_cat' );
}
完整的代碼。 在您的活動主題functions.php 文件中添加以下代碼。
function add_category_to_product_for_certain_days(){
$args = array(
'post_type' => 'product',
'posts_per_page' => -1
);
$products = new WP_Query( $args );
if ( $products->have_posts() ) { while ( $products->have_posts() ) { $products->the_post();
$product = wc_get_product( get_the_ID() );
$datetime = $product->get_date_created();
$timezone = $datetime->getTimezone();
$now_time = new WC_DateTime();
$now_time->setTimezone($timezone);
$timestamp_diff = $now_time->getTimestamp() - $datetime->getTimestamp();
$data = timestamp_to_array( $timestamp_diff );
$days = $data['d'];
// If the difference is less than 60, apply "NEW IN cat"
if ( $days < 60 ) {
wp_set_object_terms( get_the_ID(), 40, 'product_cat' );
}else{
wp_remove_object_terms( get_the_ID(), 40, 'product_cat' );
}
} wp_reset_postdata(); }
}
add_action( 'init', 'add_category_to_product_for_certain_days', 10, 1 );
function timestamp_to_array( $timestamp ) {
$d = floor($timestamp/86400);
$_d = ($d < 10 ? '0' : '').$d;
$h = floor(($timestamp-$d*86400)/3600);
$_h = ($h < 10 ? '0' : '').$h;
$m = floor(($timestamp-($d*86400+$h*3600))/60);
$_m = ($m < 10 ? '0' : '').$m;
$s = $timestamp-($d*86400+$h*3600+$m*60);
$_s = ($s < 10 ? '0' : '').$s;
return array('d' => $_d, 'h' => $_h, 'm' => $_m, 's' => $_s);
}
測試和工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.