在简码中动态插入 WooCommerce 产品 ID [英] Insert WooCommerce product id dynamically in shortcode
问题描述
设置
我有一家使用 Elementor 的 WooCommerce 商店.
我正在使用 Elementor 单一产品页面模板为每个产品生成产品页面.
我想使用这个插件来频繁显示每个产品页面上的一起购买 (FBT) 元素.
为了显示 FBT 元素,插件的创建者提供了以下短代码:[premmerce_get_bundles_by_main_product_id id="X"]
,其中 X
是 WooCommerce 产品身份证.
问题
X
不是动态的.
如果我填写产品 ID,例如product 2
在短代码中,Elementor 产品页面模板将为我的每个产品显示产品 2
的 FBT 产品.
问题
我可以使 X
动态化吗?最好将 X
自动设置为正在访问的页面的产品 ID.
您可以尝试扩展短代码:
function so_extend_frequent_bought_shortcode() {全球$产品;$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');
现在您只需要在帖子中输入[my_new_shortcode]
.如果您无法编辑您的functions.php,请使用名为代码片段>
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.
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
这篇关于在简码中动态插入 WooCommerce 产品 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!