查询自定义product_type Woocommerce

问题描述 投票:0回答:1

我在Woocommerce中创建了一个自定义product_type,如下所示。

 function register_simple_booking_product_type() {
        class WC_Product_Booking extends WC_Product{ 
            public function __construct( $product ) {
                $this->product_type = 'booking';
                parent::__construct( $product );

            }

        }


    }
    add_action( 'init', 'register_simple_booking_product_type' );

    function add_simple_booking_product( $types ){

        $types[ 'booking' ] = __( 'Simple booking' );

        return $types;

    }
    add_filter( 'product_type_selector', 'add_simple_booking_product' );
    function simple_booking_custom_js() {

        if ( 'product' != get_post_type() ) :
            return;
        endif;

        ?><script type='text/javascript'>
            jQuery( document ).ready( function() {
                jQuery( '.options_group.pricing' ).addClass( 'show_if_booking' ).show();
            });
        </script><?php
    }
    add_action( 'admin_footer', 'simple_booking_custom_js' );

从这里我想通过product_type查询产品。尽管product_type出现在Wordpress数据库的post_meta中,但创建为booking类型的产品仅在我查询simple产品类型时出现。我怎样才能查询booking product_type的产品?

$args = array(
    'post_type' => 'product',
    'tax_query' => array(
        array(
            'taxonomy' => 'product_type',
            'field'    => 'slug',
            'terms'    => 'booking',
        ),
    ),
);
$query = new WP_Query( $args );
php wordpress woocommerce
1个回答
0
投票

如果我理解你的问题是错误的,请纠正我,但如果“product_type”是元数据,则不应使用'tax_query'。查看解决方案的元查询

https://codex.wordpress.org/Class_Reference/WP_Meta_Query

© www.soinside.com 2019 - 2024. All rights reserved.