I can’t display custom WooComerce shortcodes for product filtering

Question

Well, I would like to edit my index webpage in order to display geolocalized products.

In order to achieve this, I created the shortcode on the WC_Shortcodes class, with this line:

'geolocalized'               => __CLASS__ . '::geolocalized',

And then:

 /**
     * Output geolocalized products.
     *
     * @param array $atts Attributes.
     * @return string
     */
    public static function geolocalized( $atts ) {
        $atts = array_merge(
            array(
                'limit'        => '12',
                'columns'      => '4',
                'orderby'      => 'date',
                'order'        => 'DESC',
                'category'     => '',
                'cat_operator' => 'IN',
            ),
            (array) $atts
        );

        $atts['visibility'] = 'featured';

        $shortcode = new WC_Shortcode_Products( $atts, 'geolocalized' );

        return $shortcode->get_content();
    }

I researched the WC_Shortcode_Products class in order to look after some code that could guide me to create a query or something to request products to the database without luck.

The only thing I found was parse_query_args and I’m not pretty sure what does this do. I’m very newbie on WordPress modifications and I found the code a little bit messy.

Also, I found an interface called WC_Product_Data_Store_Interface, which I added the following functions:

public function get_geolocalized_products();

Which has the following content:

public function get_geolocalized_products()
    {
        // TODO: Implement get_geolocalized_products() method.
    }

There is a sibling method called get_on_sale_products which has the following code:

public function get_on_sale_products() {
        global $wpdb;

        $exclude_term_ids            = array();
        $outofstock_join             = '';
        $outofstock_where            = '';
        $non_published_where         = '';
        $product_visibility_term_ids = wc_get_product_visibility_term_ids();

        if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) && $product_visibility_term_ids['outofstock'] ) {
            $exclude_term_ids[] = $product_visibility_term_ids['outofstock'];
        }

        if ( count( $exclude_term_ids ) ) {
            $outofstock_join  = " LEFT JOIN ( SELECT object_id FROM {$wpdb->term_relationships} WHERE term_taxonomy_id IN ( " . implode( ',', array_map( 'absint', $exclude_term_ids ) ) . ' ) ) AS exclude_join ON exclude_join.object_id = id';
            $outofstock_where = ' AND exclude_join.object_id IS NULL';
        }

        // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
        return $wpdb->get_results(
            "
            SELECT posts.ID as id, posts.post_parent as parent_id
            FROM {$wpdb->posts} AS posts
            INNER JOIN {$wpdb->wc_product_meta_lookup} AS lookup ON posts.ID = lookup.product_id
            $outofstock_join
            WHERE posts.post_type IN ( 'product', 'product_variation' )
            AND posts.post_status = 'publish'
            AND lookup.onsale = 1
            $outofstock_where
            AND posts.post_parent NOT IN (
                SELECT ID FROM `$wpdb->posts` as posts
                WHERE posts.post_type = 'product'
                AND posts.post_parent = 0
                AND posts.post_status != 'publish'
            )
            GROUP BY posts.ID
            "
        );
        // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
    }

But, as I said I’m not pretty sure if this code will help me in anyway. I tried to look after this function over the code, but I didn’t find anything useful.

0
z3nth10n 1 year 2020-07-24T12:10:25-05:00 0 Answers 44 views 0

Leave an answer

Browse
Browse