Wordpress 如何按ACF值对WooCommerce产品进行分类?
如何按Wordpress 如何按ACF值对WooCommerce产品进行分类?,wordpress,woocommerce,advanced-custom-fields,Wordpress,Woocommerce,Advanced Custom Fields,如何按高级自定义字段值对WooCommerce产品进行排序 我在网站的后端和前端添加了一些自定义字段。myfunctions.php中的前端实现如下所示: add_action( 'woocommerce_before_add_to_cart_form', 'tmy_eventbooking', 20 ); function tmy_eventbooking() { if( !empty( get_field('datum') ) ) { // if your custom field
高级自定义字段
值对WooCommerce
产品进行排序
我在网站的后端和前端添加了一些自定义字段。myfunctions.php
中的前端实现如下所示:
add_action( 'woocommerce_before_add_to_cart_form', 'tmy_eventbooking', 20 );
function tmy_eventbooking() {
if( !empty( get_field('datum') ) ) { // if your custom field is not empty…
echo '<p class="acf-info">Findet statt am: <span>' . get_field('datum') . '</span></p>';
}
if( !empty( get_field('datum_ende') ) ) { // if your custom field is not empty…
echo '<p class="acf-info">Bis: <span>' . get_field('datum_ende') . '</span></p>';
}
if( !empty( get_field('veranstaltungsort') ) ) { // if your custom field is not empty…
echo '<p class="acf-info">Ort: <span>' . get_field('veranstaltungsort') . '</span></p>';
}
return;
}
add_action('woocommerce_前加_至购物车表格','tmy_eventbooking',20);
功能tmy_eventbooking(){
如果(!empty(get_field('datum')){//如果自定义字段不是空的…
echo'Findet statt am:'。获取字段(“数据”)。
;
}
如果(!empty(get_field('datum_ende')){//如果自定义字段不是空的…
echo“Bis:”。获取字段(“数据”)。
;
}
如果(!empty(get_字段('veranstaltungsort')){//如果自定义字段不为空…
echo“Ort:”。获取字段('veranstaltungsort')。
;
}
返回;
}
这个很好用
现在,我希望能够按照这些ACF字段(键入date
和text
)对产品进行排序
例如,如果用户单击“日期”按钮或从下拉菜单中选择位置,则所有事件都会相应地排序。活动是常规的WooCommerce
服务产品
什么是实现这一点的好方法?您需要使用两个过滤器挂钩在归档页面中基于ACF执行自定义排序 使用
woocommerce\u catalog\u orderby
filter钩子添加自定义date
和位置
选项
使用woocommerce\u get\u catalog\u ordering\u args
filter hook进行修改
orderby
和order
基于您选择的选项
有用的链接
// Add these date and location sorting options on the frontend.
function add_new_custom_postmeta_orderby( $sortby ) {
$sortby['date'] = __( 'Sort by date', 'woocommerce' );
$sortby['location'] = __( 'Sort by location', 'woocommerce' );
return $sortby;
}
add_filter( 'woocommerce_catalog_orderby', 'add_new_custom_postmeta_orderby' );
function add_custom_postmeta_ordering_args( $sort_args ) {
$orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
switch( $orderby_value ) {
case 'date':
$sort_args['orderby'] = 'meta_value_num';
$sort_args['order'] = 'desc';
$sort_args['meta_key'] = 'date';
break;
case 'location':
$sort_args['orderby'] = 'meta_value';
$sort_args['order'] = 'asc';
$sort_args['meta_key'] = 'location';
break;
}
return $sort_args;
}
add_filter( 'woocommerce_get_catalog_ordering_args', 'add_custom_postmeta_ordering_args' );