DIVI WordPress Suche auf WooCommerce Produkte ausweiten

von | Feb. 3, 2025 | Allgemein, Youtube | 0 Kommentare

Schlagwörter: DIVI - WooCommerce

Die Suchfunktion ist eines der Schwachpunkte von WordPress und darum ist Divi hier nicht viel besser. Wenn man zum Beispiel einen Shop mit WordPress und WooCommerce aufbaut und als Pagebuilder DIVI hat, dann wird man die Produkte über das Divi Suchmodul gar nicht finden können.

Sowas ist natürlich sehr unpraktisch 🙂

Im heutigen Video zeige ich dir, wie ihr mit einer einfachen function.php Erweiterung das DIVI Modul so erweitern könnt, dass es auch WooCommerce Produkte auffinden kann.

Video

Gebt dieses Snippet einfach in die function.php Datei eures Child-Themes ein und schon sind eure WooCommerce Produkte in der Suche auffindbar.

Function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/* Search Product Tags */
add_filter('posts_search', 'cb_add_product_tags_to_search', 10, 2);
function cb_add_product_tags_to_search($searchSql, $query = false) {
if ( is_admin() || ! is_a( $query, 'WP_Query' ) || ! $query->is_search ) {
return $searchSql;
}
    if ($searchSql && isset( $_GET['et_pb_searchform_submit'] )) {
        global $wpdb;
        $searchSql = preg_replace(
            '/ (AND|OR) \\('.preg_quote($wpdb->posts).'\\.post_content (NOT )?LIKE \'(.+)\'\\)/U',
            '$0 $1 $2 EXISTS( SELECT 1 FROM '.$wpdb->term_relationships.' JOIN '.$wpdb->term_taxonomy.' USING (term_taxonomy_id) JOIN '.$wpdb->terms.' USING (term_id) WHERE object_id='.$wpdb->posts.'.ID AND taxonomy="product_tag" AND name LIKE \'$3\')',
            $searchSql
        );
    }
    return $searchSql;
}
 
 
add_action( 'wp_loaded', 'cb_remove_default_search' );
 
function cb_remove_default_search() {
    remove_action( 'pre_get_posts', 'et_pb_custom_search' );
    add_action( 'pre_get_posts', 'cb_custom_search' );
}
 
function cb_custom_search( $query = false ) {
    if ( is_admin() || ! is_a( $query, 'WP_Query' ) || ! $query->is_search ) {
        return;
    }
    if ( isset( $_GET['et_pb_searchform_submit'] ) ) {
        $postTypes = array();
        if ( ! isset($_GET['et_pb_include_posts'] ) && ! isset( $_GET['et_pb_include_pages'] ) ) {
            $postTypes = array( 'post' );
        }
        if ( isset( $_GET['et_pb_include_pages'] ) ) {
            $postTypes = array( 'page' );
        }
        if ( isset( $_GET['et_pb_include_posts'] ) ) {
            $postTypes[] = 'post';
        }
        /* BEGIN Add custom post types */
        $postTypes[] = 'product';
        /* END Add custom post types */
        $query->set( 'post_type', $postTypes );
        if ( ! empty( $_GET['et_pb_search_cat'] ) ) {
            $categories_array = explode( ',', $_GET['et_pb_search_cat'] );
            $query->set( 'category__not_in', $categories_array );
        }
        if ( isset( $_GET['et-posts-count'] ) ) {
            $query->set( 'posts_per_page', (int) $_GET['et-posts-count'] );
        }
    }
}

0 Kommentare

Einen Kommentar abschicken

Du kannst auf Fediverse-Profile verlinken, indem du fl:@benutzername in deinem Kommentar eingibst.

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert