【问题标题】:Querying custom product_type Woocommerce查询自定义 product_type Woocommerce
【发布时间】:2018-09-28 21:06:04
【问题描述】:

我在 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 );

【问题讨论】:

  • 您是否尝试通过 term_id 而不是 slug 进行查询,只是为了排除这种情况?

标签: php wordpress woocommerce


【解决方案1】:

如果我对您的问题的理解有误,请纠正我,但如果“product_type”是元数据,则不应使用“tax_query”。查看元查询以获取解决方案

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

【讨论】:

    猜你喜欢
    • 2017-05-06
    • 1970-01-01
    • 1970-01-01
    • 2018-12-24
    • 2021-10-24
    • 1970-01-01
    • 1970-01-01
    • 2019-07-02
    • 2013-08-28
    相关资源
    最近更新 更多