Ad

Altering BigCommerce's Wordpress Catalog

- 1 answer

I wanted to link my Wordpress blog posts to product categories (programmatically), so I updated the bigcommerce_product taxonomy type to also appear on the post content type using the following code.

function shop_modify_bc_taxonomy() {
  $tax = 'bigcommerce_category';
  $bcArgs = get_taxonomy( $tax ); // returns an object
  $bcArgs->object_type = array_merge($bcArgs->object_type, ['post']); // Add 'post'
  register_taxonomy( $tax, $bcArgs->object_type, (array) $bcArgs );
}

// hook it up to 11 so that it overrides the original register_taxonomy function
add_action( 'init', 'shop_modify_bc_taxonomy', 20 );

Now, however, posts are showing up on the product catalogs when associating them with a term.

If I add the following line to the function BigCommerce\Templates\Product_Archive::get_posts() my problem is solved:

private function get_posts( \WP_Query $query ) {
    $cards = [];
    while ( $query->have_posts() ) {
      $query->the_post();
      
      // Add this line
      if (get_post_type() !== 'bigcommerce_product') {  continue;  }

      $product = new Product( get_the_ID() );
      $card    = Product_Card::factory( [
        Product_Card::PRODUCT => $product,
      ] );
      $cards[] = $card->render();
    }
    wp_reset_query();

    return $cards;
  }

but I don't know how to override this class. Can I perhaps hook into the WP_Query?

Ad

Answer

Using pre_get_posts, we can limit the post_type to Big Commerce Products in our theme's function.php file

function edit_bigcommerce_queries( $query ) {
    if ( ! is_admin() && $query->is_main_query() ) {

        // Is a BigCommerce Category Page
        if ( !empty( $query->query['bigcommerce_category']) {

            $query->set( 'post_type', 'bigcommerce_product');
        }
    }
}
add_action( 'pre_get_posts', 'edit_bigcommerce_queries' );
Ad
source: stackoverflow.com
Ad