以编程方式将登录表单短代码添加到每个已发布的产品页面
Add login form shortcode programatically to every published product page
我是 运行 Woocommerce 上的一家批发店。需要登录才能查看价格。这是设置和正常工作。现在,我希望在每个产品页面上添加一个登录表单,以仅向访问者(未登录用户)显示。
我正在使用 WooCommerce 目录可见性插件。这个插件提供了我上面描述的功能,但我的主题有点搞砸了。插件作者说要与主题开发人员交谈,主题开发人员说要与插件作者交谈。所以现在我正在尝试找到解决方法。
第一期: 该插件带有一个短代码 [woocommerce_logon_form],它将显示一个登录表单。我不想手动将此添加到每个现有产品,因为我的网站上有数千种产品。我正在寻找一种通过产品页面布局代码获取它的方法。
我发现此代码(将添加到 functions.php)运行良好:
// adds notice at single product page above add to cart
add_action( 'woocommerce_single_product_summary', 'return_policy', 20 );
function return_policy() {
echo '<p id="rtrn">30-day return policy offered. See Terms and Conditions for details.</p>';
}
但是,它只会显示文本。添加短代码而不是示例文本时将不起作用。
第二期:即使客户已经登录,短代码也会显示表单。
我目前正在使用这个漂亮的代码,它根据用户是否登录显示或隐藏内容:
add_shortcode( 'access', 'access_check_shortcode' );
function access_check_shortcode( $attr, $content = null ) {
extract( shortcode_atts( array( 'capability' => 'read' ), $attr ) );
if ( current_user_can( $capability ) && !is_null( $content ) && !is_feed() )
return $content;
return '';
}
add_shortcode( 'visitor', 'visitor_check_shortcode' );
function visitor_check_shortcode( $atts, $content = null ) {
if ( ( !is_user_logged_in() && !is_null( $content ) ) || is_feed() )
return $content;
return '';
}
该短代码非常适合文本,但不适用于其他短代码。
因此,这些短代码的组合:[visitor][woocommerce_logon_form][/visitor] 不会向访问者显示登录表单。相反,它只会将其显示为文本 [woocommerce_logon_form].
请帮忙!我相信这可能很容易被具有编码技能的人解决。
感谢您为回答这个问题所做的努力。请记住,我对代码的理解非常有限,如果您还可以指出在哪个文件中添加或修改代码,那就太好了。
To make your shortcode working in php code or in php/html code you need to use a native WordPress function do_shortcode()
… You can use it with your shortcode for example in your 1st function this way:
add_action( 'woocommerce_single_product_summary', 'return_policy', 20 );
function return_policy() {
echo do_shortcode('[woocommerce_logon_form]');
}
这会起作用……
要查看可以代替 woocommerce_single_product_summary
使用的所有不同挂钩,请查看这 2 个模板代码以选择更方便的挂钩:
您也可以将其添加到您现有的短代码之一中,这样:
add_shortcode( 'visitor', 'visitor_check_shortcode' );
function visitor_check_shortcode( $atts, $content = null ) {
if ( ( !is_user_logged_in() && !is_null( $content ) ) || is_feed() )
return do_shortcode('[woocommerce_logon_form]');
return '';
}
这也行。
参考这个答案:
So as you can see your problem is solved on both issues
我是 运行 Woocommerce 上的一家批发店。需要登录才能查看价格。这是设置和正常工作。现在,我希望在每个产品页面上添加一个登录表单,以仅向访问者(未登录用户)显示。
我正在使用 WooCommerce 目录可见性插件。这个插件提供了我上面描述的功能,但我的主题有点搞砸了。插件作者说要与主题开发人员交谈,主题开发人员说要与插件作者交谈。所以现在我正在尝试找到解决方法。
第一期: 该插件带有一个短代码 [woocommerce_logon_form],它将显示一个登录表单。我不想手动将此添加到每个现有产品,因为我的网站上有数千种产品。我正在寻找一种通过产品页面布局代码获取它的方法。
我发现此代码(将添加到 functions.php)运行良好:
// adds notice at single product page above add to cart
add_action( 'woocommerce_single_product_summary', 'return_policy', 20 );
function return_policy() {
echo '<p id="rtrn">30-day return policy offered. See Terms and Conditions for details.</p>';
}
但是,它只会显示文本。添加短代码而不是示例文本时将不起作用。
第二期:即使客户已经登录,短代码也会显示表单。
我目前正在使用这个漂亮的代码,它根据用户是否登录显示或隐藏内容:
add_shortcode( 'access', 'access_check_shortcode' );
function access_check_shortcode( $attr, $content = null ) {
extract( shortcode_atts( array( 'capability' => 'read' ), $attr ) );
if ( current_user_can( $capability ) && !is_null( $content ) && !is_feed() )
return $content;
return '';
}
add_shortcode( 'visitor', 'visitor_check_shortcode' );
function visitor_check_shortcode( $atts, $content = null ) {
if ( ( !is_user_logged_in() && !is_null( $content ) ) || is_feed() )
return $content;
return '';
}
该短代码非常适合文本,但不适用于其他短代码。 因此,这些短代码的组合:[visitor][woocommerce_logon_form][/visitor] 不会向访问者显示登录表单。相反,它只会将其显示为文本 [woocommerce_logon_form].
请帮忙!我相信这可能很容易被具有编码技能的人解决。 感谢您为回答这个问题所做的努力。请记住,我对代码的理解非常有限,如果您还可以指出在哪个文件中添加或修改代码,那就太好了。
To make your shortcode working in php code or in php/html code you need to use a native WordPress function
do_shortcode()
… You can use it with your shortcode for example in your 1st function this way:
add_action( 'woocommerce_single_product_summary', 'return_policy', 20 );
function return_policy() {
echo do_shortcode('[woocommerce_logon_form]');
}
这会起作用……
要查看可以代替 woocommerce_single_product_summary
使用的所有不同挂钩,请查看这 2 个模板代码以选择更方便的挂钩:
您也可以将其添加到您现有的短代码之一中,这样:
add_shortcode( 'visitor', 'visitor_check_shortcode' );
function visitor_check_shortcode( $atts, $content = null ) {
if ( ( !is_user_logged_in() && !is_null( $content ) ) || is_feed() )
return do_shortcode('[woocommerce_logon_form]');
return '';
}
这也行。
参考这个答案:
So as you can see your problem is solved on both issues