仅当项目属于特定类别时显示 WooCommerce 我的帐户订单
Display WooCommerce My account Orders only when items belong to specific category
所以我在 WooCoommerce 的 my account/my-orders.php
模板文件上构建了一个自定义部分,以仅显示“课程”类别下的任何产品的订单历史记录。
我目前有这个:
<?php
$my_orders_columns = apply_filters(
'woocommerce_my_account_my_orders_columns',
array(
'order-number' => esc_html__( 'Order', 'woocommerce' ),
'order-date' => esc_html__( 'Date', 'woocommerce' ),
'order-status' => esc_html__( 'Status', 'woocommerce' ),
'order-total' => esc_html__( 'Total', 'woocommerce' ),
'order-actions' => ' ',
)
);
$customer_orders = get_posts(
apply_filters(
'woocommerce_my_account_my_orders_query',
array(
'numberposts' => $order_count,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => wc_get_order_types( 'view-orders' ),
'post_status' => array_keys( wc_get_order_statuses() ),
)
)
);
if ( $customer_orders ) : ?>
<?php if(is_product_category('course')) { ?>
<section class='courseHistory'>
<div class='grid-container full'>
<?php
foreach ( $customer_orders as $customer_order ) :
$order = wc_get_order( $customer_order ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$item_count = $order->get_item_count();
?>
<div class='grid-x'>
<!-- Course loop to go here -->
<?php foreach ( $my_orders_columns as $column_id => $column_name ) : ?>
<div class='large-12 cell marB20 shadow'>
<div class='grid-x'>
<div class='small-12 medium-12 large-3 cell courseImg'>
<!-- Course image here -->
<img src="<?php echo get_template_directory_uri(); ?>/assets/images/course.png"/>
</div>
<div class='small-12 medium-12 large-9 cell courseInfo whiteBg'>
<div class='grid-x grid-margin-x align-middle'>
<div class='small-12 medium-12 large-7 cell'>
<div class='grid-x'>
<div class='large-5 cell'>
<p class='bold title'>Course name:</p>
</div>
<div class='large-auto cell'>
<!-- Course title here -->
<p class='noweight title'><?php echo the_dramatist_get_order_prod_name( $order ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></p>
</div>
</div>
<div class='grid-x'>
<div class='large-5 cell'>
<p class='bold title'>Date of course:</p>
</div>
<div class='large-auto cell'>
<!-- Date of course from calendar here -->
<p class='noweight title'>21.01.21</p>
</div>
</div>
<div class='grid-x'>
<div class='large-5 cell'>
<p class='bold title'>Paid on:</p>
</div>
<div class='large-auto cell'>
<!-- Purchase date here -->
<p class='noweight title'><time datetime="<?php echo esc_attr( $order->get_date_created()->date( 'c' ) ); ?>"><?php echo esc_html( wc_format_datetime( $order->get_date_created() ) ); ?></time></p>
</div>
</div>
<div class='grid-x'>
<div class='large-5 cell'>
<p class='bold title'>Status:</p>
</div>
<div class='large-auto cell'>
<!-- Purchase status here -->
<p class='noweight title'><?php echo esc_html( wc_get_order_status_name( $order->get_status() ) ); ?></p>
</div>
</div>
</div>
<div class='small-12 medium-12 large-5 cell'>
<!-- Link to course to go through to shop info page? -->
<a class='orangeB' href="<?php echo the_permalink(); ?>">View course overview</a>
<!-- Link to PPE for this course here.
I've done it so each PPE product will have a course sub category that can be assigned to it to make filtering easier.
PPE Cat > Course PPE sub cat
-->
<a class='blackB marT20' href="<?php echo the_permalink(); ?>">Get PPE for this course</a>
</div>
</div>
</div>
</div>
</div>
<!-- Course loop end -->
<?php endforeach; ?>
</div>
<?php endforeach; ?>
</div>
</section>
<?php } else { ?>
dsfdsf
<?php } ?>
<?php endif; ?>
这是按照我想要的方式在一个循环中拉取订单历史记录,但我已经在它周围添加了一个条件 if is_product_category('course')
并且它没有拉取任何东西。
感觉我很接近但错过了什么?
您的代码中有一些小错误......要检查产品类别,您需要使用 has_terms()
Wordpress 条件函数检查订单项目(对于每个订单),如下所示:
<?php
$my_orders_columns = apply_filters(
'woocommerce_my_account_my_orders_columns',
array(
'order-number' => esc_html__( 'Order', 'woocommerce' ),
'order-date' => esc_html__( 'Date', 'woocommerce' ),
'order-status' => esc_html__( 'Status', 'woocommerce' ),
'order-total' => esc_html__( 'Total', 'woocommerce' ),
'order-actions' => ' ',
)
);
$customer_orders = get_posts(
apply_filters(
'woocommerce_my_account_my_orders_query',
array(
'numberposts' => $order_count,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => wc_get_order_types( 'view-orders' ),
'post_status' => array_keys( wc_get_order_statuses() ),
)
)
);
$product_category_terms = array('course'); // <== Here the defined product category term(s)
if ( $customer_orders ) : ?>
<section class='courseHistory'>
<div class='grid-container full'>
<?php foreach ( $customer_orders as $customer_order ) :
$order = wc_get_order( $customer_order ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$item_count = $order->get_item_count();
$category_found = false; // Initializing
// Loop through order items
foreach ( $order->get_items() as $item ) {
if ( has_term( $product_category_terms, 'product_cat', $item->get_product_id() ) ) {
$category_found = true;
break;
}
}
// If the product category is not found in order items we jump to next order
if ( ! $category_found ) continue;
?>
<div class='grid-x'>
<!-- Course loop to go here -->
<?php foreach ( $my_orders_columns as $column_id => $column_name ) : ?>
<div class='large-12 cell marB20 shadow'>
<div class='grid-x'>
<div class='small-12 medium-12 large-3 cell courseImg'>
<!-- Course image here -->
<img src="<?php echo get_template_directory_uri(); ?>/assets/images/course.png"/>
</div>
<div class='small-12 medium-12 large-9 cell courseInfo whiteBg'>
<div class='grid-x grid-margin-x align-middle'>
<div class='small-12 medium-12 large-7 cell'>
<div class='grid-x'>
<div class='large-5 cell'>
<p class='bold title'>Course name:</p>
</div>
<div class='large-auto cell'>
<!-- Course title here -->
<p class='noweight title'><?php echo the_dramatist_get_order_prod_name( $order ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></p>
</div>
</div>
<div class='grid-x'>
<div class='large-5 cell'>
<p class='bold title'><?php esc_html_e("Date of course:", "woocommerce"); ?></p>
</div>
<div class='large-auto cell'>
<!-- Date of course from calendar here -->
<p class='noweight title'><?php _e("21.01.21", "woocommerce"); ?></p>
</div>
</div>
<div class='grid-x'>
<div class='large-5 cell'>
<p class='bold title'><?php esc_html_e("Paid on:", "woocommerce"); ?></p>
</div>
<div class='large-auto cell'>
<!-- Purchase date here -->
<p class='noweight title'><time datetime="<?php esc_attr_e( $order->get_date_created()->date( 'c' ) ); ?>"><?php esc_html_e( wc_format_datetime( $order->get_date_created() ) ); ?></time></p>
</div>
</div>
<div class='grid-x'>
<div class='large-5 cell'>
<p class='bold title'><?php esc_html_e("Status:", "woocommerce"); ?></p>
</div>
<div class='large-auto cell'>
<!-- Purchase status here -->
<p class='noweight title'><?php esc_html_e( wc_get_order_status_name( $order->get_status() ) ); ?></p>
</div>
</div>
</div>
<div class='small-12 medium-12 large-5 cell'>
<!-- Link to course to go through to shop info page? -->
<a class='orangeB' href="<?php the_permalink(); ?>"><?php esc_html_e("View course overview", "woocommerce"); ?></a>
<!-- Link to PPE for this course here. I've done it so each PPE product will have a course sub category
that can be assigned to it to make filtering easier. PPE Cat > Course PPE sub cat -->
<a class='blackB marT20' href="<?php the_permalink(); ?>"><?php esc_html_e("Get PPE for this course", "woocommerce"); ?></a>
</div>
</div>
</div>
</div>
</div>
<!-- Course loop end -->
<?php endforeach; ?>
</div>
<!-- Orders loop end -->
<?php endforeach; ?>
</div>
</section>
<?php endif; ?>
应该可以。
所以我在 WooCoommerce 的 my account/my-orders.php
模板文件上构建了一个自定义部分,以仅显示“课程”类别下的任何产品的订单历史记录。
我目前有这个:
<?php
$my_orders_columns = apply_filters(
'woocommerce_my_account_my_orders_columns',
array(
'order-number' => esc_html__( 'Order', 'woocommerce' ),
'order-date' => esc_html__( 'Date', 'woocommerce' ),
'order-status' => esc_html__( 'Status', 'woocommerce' ),
'order-total' => esc_html__( 'Total', 'woocommerce' ),
'order-actions' => ' ',
)
);
$customer_orders = get_posts(
apply_filters(
'woocommerce_my_account_my_orders_query',
array(
'numberposts' => $order_count,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => wc_get_order_types( 'view-orders' ),
'post_status' => array_keys( wc_get_order_statuses() ),
)
)
);
if ( $customer_orders ) : ?>
<?php if(is_product_category('course')) { ?>
<section class='courseHistory'>
<div class='grid-container full'>
<?php
foreach ( $customer_orders as $customer_order ) :
$order = wc_get_order( $customer_order ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$item_count = $order->get_item_count();
?>
<div class='grid-x'>
<!-- Course loop to go here -->
<?php foreach ( $my_orders_columns as $column_id => $column_name ) : ?>
<div class='large-12 cell marB20 shadow'>
<div class='grid-x'>
<div class='small-12 medium-12 large-3 cell courseImg'>
<!-- Course image here -->
<img src="<?php echo get_template_directory_uri(); ?>/assets/images/course.png"/>
</div>
<div class='small-12 medium-12 large-9 cell courseInfo whiteBg'>
<div class='grid-x grid-margin-x align-middle'>
<div class='small-12 medium-12 large-7 cell'>
<div class='grid-x'>
<div class='large-5 cell'>
<p class='bold title'>Course name:</p>
</div>
<div class='large-auto cell'>
<!-- Course title here -->
<p class='noweight title'><?php echo the_dramatist_get_order_prod_name( $order ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></p>
</div>
</div>
<div class='grid-x'>
<div class='large-5 cell'>
<p class='bold title'>Date of course:</p>
</div>
<div class='large-auto cell'>
<!-- Date of course from calendar here -->
<p class='noweight title'>21.01.21</p>
</div>
</div>
<div class='grid-x'>
<div class='large-5 cell'>
<p class='bold title'>Paid on:</p>
</div>
<div class='large-auto cell'>
<!-- Purchase date here -->
<p class='noweight title'><time datetime="<?php echo esc_attr( $order->get_date_created()->date( 'c' ) ); ?>"><?php echo esc_html( wc_format_datetime( $order->get_date_created() ) ); ?></time></p>
</div>
</div>
<div class='grid-x'>
<div class='large-5 cell'>
<p class='bold title'>Status:</p>
</div>
<div class='large-auto cell'>
<!-- Purchase status here -->
<p class='noweight title'><?php echo esc_html( wc_get_order_status_name( $order->get_status() ) ); ?></p>
</div>
</div>
</div>
<div class='small-12 medium-12 large-5 cell'>
<!-- Link to course to go through to shop info page? -->
<a class='orangeB' href="<?php echo the_permalink(); ?>">View course overview</a>
<!-- Link to PPE for this course here.
I've done it so each PPE product will have a course sub category that can be assigned to it to make filtering easier.
PPE Cat > Course PPE sub cat
-->
<a class='blackB marT20' href="<?php echo the_permalink(); ?>">Get PPE for this course</a>
</div>
</div>
</div>
</div>
</div>
<!-- Course loop end -->
<?php endforeach; ?>
</div>
<?php endforeach; ?>
</div>
</section>
<?php } else { ?>
dsfdsf
<?php } ?>
<?php endif; ?>
这是按照我想要的方式在一个循环中拉取订单历史记录,但我已经在它周围添加了一个条件 if is_product_category('course')
并且它没有拉取任何东西。
感觉我很接近但错过了什么?
您的代码中有一些小错误......要检查产品类别,您需要使用 has_terms()
Wordpress 条件函数检查订单项目(对于每个订单),如下所示:
<?php
$my_orders_columns = apply_filters(
'woocommerce_my_account_my_orders_columns',
array(
'order-number' => esc_html__( 'Order', 'woocommerce' ),
'order-date' => esc_html__( 'Date', 'woocommerce' ),
'order-status' => esc_html__( 'Status', 'woocommerce' ),
'order-total' => esc_html__( 'Total', 'woocommerce' ),
'order-actions' => ' ',
)
);
$customer_orders = get_posts(
apply_filters(
'woocommerce_my_account_my_orders_query',
array(
'numberposts' => $order_count,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => wc_get_order_types( 'view-orders' ),
'post_status' => array_keys( wc_get_order_statuses() ),
)
)
);
$product_category_terms = array('course'); // <== Here the defined product category term(s)
if ( $customer_orders ) : ?>
<section class='courseHistory'>
<div class='grid-container full'>
<?php foreach ( $customer_orders as $customer_order ) :
$order = wc_get_order( $customer_order ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$item_count = $order->get_item_count();
$category_found = false; // Initializing
// Loop through order items
foreach ( $order->get_items() as $item ) {
if ( has_term( $product_category_terms, 'product_cat', $item->get_product_id() ) ) {
$category_found = true;
break;
}
}
// If the product category is not found in order items we jump to next order
if ( ! $category_found ) continue;
?>
<div class='grid-x'>
<!-- Course loop to go here -->
<?php foreach ( $my_orders_columns as $column_id => $column_name ) : ?>
<div class='large-12 cell marB20 shadow'>
<div class='grid-x'>
<div class='small-12 medium-12 large-3 cell courseImg'>
<!-- Course image here -->
<img src="<?php echo get_template_directory_uri(); ?>/assets/images/course.png"/>
</div>
<div class='small-12 medium-12 large-9 cell courseInfo whiteBg'>
<div class='grid-x grid-margin-x align-middle'>
<div class='small-12 medium-12 large-7 cell'>
<div class='grid-x'>
<div class='large-5 cell'>
<p class='bold title'>Course name:</p>
</div>
<div class='large-auto cell'>
<!-- Course title here -->
<p class='noweight title'><?php echo the_dramatist_get_order_prod_name( $order ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></p>
</div>
</div>
<div class='grid-x'>
<div class='large-5 cell'>
<p class='bold title'><?php esc_html_e("Date of course:", "woocommerce"); ?></p>
</div>
<div class='large-auto cell'>
<!-- Date of course from calendar here -->
<p class='noweight title'><?php _e("21.01.21", "woocommerce"); ?></p>
</div>
</div>
<div class='grid-x'>
<div class='large-5 cell'>
<p class='bold title'><?php esc_html_e("Paid on:", "woocommerce"); ?></p>
</div>
<div class='large-auto cell'>
<!-- Purchase date here -->
<p class='noweight title'><time datetime="<?php esc_attr_e( $order->get_date_created()->date( 'c' ) ); ?>"><?php esc_html_e( wc_format_datetime( $order->get_date_created() ) ); ?></time></p>
</div>
</div>
<div class='grid-x'>
<div class='large-5 cell'>
<p class='bold title'><?php esc_html_e("Status:", "woocommerce"); ?></p>
</div>
<div class='large-auto cell'>
<!-- Purchase status here -->
<p class='noweight title'><?php esc_html_e( wc_get_order_status_name( $order->get_status() ) ); ?></p>
</div>
</div>
</div>
<div class='small-12 medium-12 large-5 cell'>
<!-- Link to course to go through to shop info page? -->
<a class='orangeB' href="<?php the_permalink(); ?>"><?php esc_html_e("View course overview", "woocommerce"); ?></a>
<!-- Link to PPE for this course here. I've done it so each PPE product will have a course sub category
that can be assigned to it to make filtering easier. PPE Cat > Course PPE sub cat -->
<a class='blackB marT20' href="<?php the_permalink(); ?>"><?php esc_html_e("Get PPE for this course", "woocommerce"); ?></a>
</div>
</div>
</div>
</div>
</div>
<!-- Course loop end -->
<?php endforeach; ?>
</div>
<!-- Orders loop end -->
<?php endforeach; ?>
</div>
</section>
<?php endif; ?>
应该可以。