0
votes

Set-up

I have a WooCommerce store with Elementor.

I'm using an Elementor single product page template to generate the product page for each product.

I want to use this plug-in to display a Frequently Bought Together (FBT) element on each product page.

To display the FBT element, the creator of the plug-in provides the following shortcode: [premmerce_get_bundles_by_main_product_id id="X"], where X is a WooCommerce product id.


Issue

The X is not dynamic.

If I fill in the product id of e.g. product 2 in the shortcode, the Elementor product page template will display the FBT products of product 2 for each of my products.


Question

Can I make the X dynamic? Preferably, the X is set automatically to the product's id which page is being visited.

2

2 Answers

2
votes

You can try to extend the shortcode:

function so_extend_frequent_bought_shortcode() {
    global $product;
    $id = $product->get_id();

    return do_shortcode( '[premmerce_get_bundles_by_main_product_id id="' . $id . '"]');
}
add_shortcode( 'my_new_shortcode', 'so_extend_frequent_bought_shortcode' );

Now you just need to put[my_new_shortcode] in your posts. If you can't edit your functions.php, use a plugin called Code Snippets

2
votes

You can only use dynamic data in shortcodes if you output the shortcode like this:

global $product;
$id = $product->get_id();

echo do_shortcode( '[premmerce_get_bundles_by_main_product_id id="' . $id . '"]');

see https://developer.wordpress.org/reference/functions/do_shortcode/