지식 DB

상용 서비스를 위한 진짜 워드프레스 정보

우커머스 상품 판매자별 인덱싱 추가하기

발행:

| 업데이트:

지식

WP스퀘어의 모든 정보는 국내 상용 서비스를 기준으로 작성됩니다.

FacetWP에서 데이터 소스 중 우커머스 관련 소스 중 판매자 소스는 지원되지 않습니다.
글 > 글 작성자 를 통해서도 할수 있지만 이경우는 모든 Post_type 에 대해서 인덱싱이 됩니다.

이로 인해 “product_variation” 역시 인덱싱이 되며, “Product” 타입과 “product_variation” 의 작성자가 다른경우가 존재하기 때문에 “Product” 타입의 작성자만 인덱싱이 필요할때가 있습니다.

add_filter("facetwp_facet_sources", function($sources){
        $sources['woocommerce']['choices']['woo/vender'] = __( '판매자' );
        return $sources;
    }, 20);
Code language: PHP (php)

해당 코드로 Facet 의 데이터 소스에 ‘판매자’ 라는 소스 를 추가합니다.

add_filter("facetwp_indexer_post_facet", function($return, $params){
        $facet = $params['facet'];
        $defaults = $params['defaults'];
        $post_id = (int) $defaults['post_id'];
        $post_type = get_post_type( $post_id );

        // Index out of stock products?
        $index_all = ( 'yes' === FWP()->helper->get_setting( 'wc_index_all', 'no' ) );
        $index_all = apply_filters( 'facetwp_index_all_products', $index_all );

        if ( 'product' == $post_type || 'product_variation' == $post_type ) {
            $product = wc_get_product( $post_id );

            if ( ! $product || ( ! $index_all && ! $product->is_in_stock() ) ) {
                return true; // skip
            }
        }

        // Default handling
        if ( 'product' != $post_type || empty( $facet['source'] ) ) {
            return $return;
        }

        if ( 'woo/vender' === $facet['source'] ) {
            $post = get_post( $post_id );
            $value = $post->post_author;
            cym_debug("post_id =" .$post_id .  " / author = ". $value);
            // $value = $product->post_author;
            $user = get_user_by( 'id', $value );
            $display_value = ( $user instanceof WP_User ) ? $user->display_name : $value;

            $defaults['facet_value'] = $value;
            $defaults['facet_display_value'] = $display_value;
            FWP()->indexer->index_row( $defaults );
            return true;
        }

        return $return;
    }, 20 , 2);Code language: PHP (php)

해당 코드로 Product 타입의 판매자를 인덱싱 할수 있습니다.

태그

글쓴이

Photo of author

메아쿨파

팀과 함께 워드프레스 관련 자문과 개발을 하고 있습니다. 상용 서비스르를 필요로 하는 기업과 스타트업, 높은 수준의 워드프레스를 만들고 싶은 에이전시, WP스퀘어 필진으로 참여하고 싶으신 분은 문의하기로 남겨 주시기 바랍니다.