bigcommerce / bigcommerce-for-wordpress

A headless commerce integration for WordPress, powered by BigCommerce
https://www.bigcommerce.com/wordpress/
GNU General Public License v2.0
109 stars 49 forks source link

Support for Elementor Pro single post templates on product pages #408

Open corgalore opened 1 year ago

corgalore commented 1 year ago

Expected behavior

Be able to assign an Elementor Single Post template (Elementor Pro) to the BigCommerce Product post type and see the assigned Elementor template render in public-facing view (https://www.domain.com/products/my-product-slug/).

Actual behavior

Instead the single-bigcommerce_product.php template from the Bigcommerce plugin or theme-overridden version is loaded.

Steps to reproduce behavior

  1. Create an Elementor Single Post template (Elementor Pro) and drop a couple elements into the layout for testing.
  2. Add a Condition assigning the template to the BigCommerce Product post type.
  3. View a Product from its public-facing url.

Workaround or possible solution

I've tried to short-circuit the template filtering with some success, but it always results in an Exception occurring somewhere. Is it possible to support Elementor single post templates with this plugin? It would be great to build layouts right in the visual editor.

corgalore commented 11 months ago

I found a solution for using Elementor to build BigCommerce Product pages.

Unhook the current template:

remove_action( 'bigcommerce/template/product/single', bigcommerce()->templates->product_single, 10 );

Hook your own template:

add_action( 'bigcommerce/template/product/single', 'render_single_template', 10, 2 );

The template loader:

function render_single_template( $output, $post_id ) {

    $slug = 'my-elementor-template-slug';

    $template = get_page_by_path( $slug, OBJECT, 'elementor_library' );

    $elementor = \Elementor\Plugin::instance();

    return $elementor->frontend->get_builder_content_for_display( $template->ID );
}