触发 WooCommerce 钩子,使用文章内容中的短代码

huangapple go评论64阅读模式
英文:

Firing a Woocommerce hook using a shortcode in post content

问题

我正在尝试将动作挂钩woocommerce_single_product_summary分配给一个短代码,以便在模板中不使用do_action的固定位置,而是让最终用户可以在Gutenberg中创建的布局中自由移动页面上的整个single_product_summary部分。

我在functions.php中有以下内容:

function getsummary_shortcode( $atts ) {
    ob_start();
    do_action( 'woocommerce_single_product_summary' );
    $woocommerce_single_product = ob_get_clean();
    return $woocommerce_single_product;
}

add_shortcode('getsummary','getsummary_shortcode');

然而,这似乎会无限循环输出single_product_summary的内容。

这实际上可行吗?我在这里找到了一些尝试做类似事情的帖子,但没有一个尝试做我所做的事情。

英文:

I am trying to assign the action hook woocommerce_single_product_summary to a shortcode so that, rather than do_action being used in a fixed place in the template, the end-user can move the whole single_product_summary section of the page around freely within a layout they've created in Gutenberg.

I have the following in functions.php:

    function getsummary_shortcode( $atts ) {
    ob_start();
    do_action( 'woocommerce_single_product_summary' );
    $woocommerce_single_product = ob_get_clean();
    return $woocommerce_single_product;
}

add_shortcode('getsummary','getsummary_shortcode');

However, this seems to output the single_product_summary content in an infinite loop.

Is it actually possible to do this? I have found a few posts here trying to do something vaguely similar to this, but none trying to do quite what I'm doing.

答案1

得分: 2

你可能需要取消注册并重新注册你的 getsummary 短代码在你的函数回调中,以防止它被 woocommerce_single_product_summary 钩子调用。

function getsummary_shortcode( $atts ) {
    remove_shortcode('getsummary');
    ob_start();
    do_action( 'woocommerce_single_product_summary' );
    $woocommerce_single_product = ob_get_clean();
    add_shortcode('getsummary','getsummary_shortcode');
    return $woocommerce_single_product;
}

add_shortcode('getsummary','getsummary_shortcode');
英文:

You may need to deregister, and reregister your getsummary shortcode within your function callback to prevent it being called by the woocommerce_single_product_summary hook

function getsummary_shortcode( $atts ) {
    remove_shortcode('getsummary');
    ob_start();
    do_action( 'woocommerce_single_product_summary' );
    $woocommerce_single_product = ob_get_clean();
    add_shortcode('getsummary','getsummary_shortcode');
    return $woocommerce_single_product;
}

add_shortcode('getsummary','getsummary_shortcode');

huangapple
  • 本文由 发表于 2023年4月4日 04:27:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75923520.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定