Php 为特定属性和术语向WooCommerce产品添加自定义按钮
对于具有WooCommerce的网站,当产品具有特定属性+值时,我需要向单个产品页面添加按钮。在这种情况下:当产品具有属性Php 为特定属性和术语向WooCommerce产品添加自定义按钮,php,wordpress,woocommerce,attributes,product,Php,Wordpress,Woocommerce,Attributes,Product,对于具有WooCommerce的网站,当产品具有特定属性+值时,我需要向单个产品页面添加按钮。在这种情况下:当产品具有属性pa_aanbod添加按钮时,当产品具有属性值Verhuur时,添加另一个按钮 我通过下面所示的课程达到了这一目的。问题是,当我向产品添加另一个随机属性时,按钮组被复制,因此,对于每个新添加的属性,都会添加一个额外的按钮组 我的问题是:如何确保按钮组只添加一次,并且在向产品添加其他属性时不重复?我认为这与foreach语句有关,但我不确定如何解决这个问题 if (!class
pa_aanbod
添加按钮时,当产品具有属性值Verhuur
时,添加另一个按钮
我通过下面所示的课程达到了这一目的。问题是,当我向产品添加另一个随机属性时,按钮组被复制,因此,对于每个新添加的属性,都会添加一个额外的按钮组
我的问题是:如何确保按钮组只添加一次,并且在向产品添加其他属性时不重复?我认为这与foreach语句有关,但我不确定如何解决这个问题
if (!class_exists('WcAddFormButtons')) {
class WcAddFormButtons
{
public function __construct()
{
add_action( 'woocommerce_product_meta_end', array( $this, 'single_product_add_formbuttons', ), 50);
}
/**
* Check the terms of product taxonomy pa_aanbod
* to get the right buttons associated with the forms
*
* @since 1.0.0
*/
public function single_product_add_formbuttons()
{
global $product;
$attributes = $product->get_attributes();
$product_title = get_the_title( $product->get_id() );
if ( ! $attributes ) {
return;
}
foreach ( $attributes as $attribute ) :
// skip variations
if ($attribute['is_variation']) {
continue;
}
if ($attribute['is_taxonomy']) {
$terms = wp_get_post_terms($product->get_id(), 'pa_aanbod');
echo '<div class="button-group">';
foreach ($terms as $term) :
switch ($term->name) {
case 'Verhuur' :
echo '<a class="vc_general vc_btn vc_btn-style-normal vc_btn-color-main-brand-color full-width" href="' . site_url() . '/direct-huren/?makkina_machine_name=' . $product_title . '">Direct huren</a>';
break;
}
endforeach;
echo '<a class="vc_general vc_btn vc_btn-style-normal vc_btn-color-main-brand-color full-width" href="' . site_url() . '/offerte-aanvragen/?makkina_machine_name=' . $product_title . '">Offerte aanvragen</a>';
echo '<a class="ask-demo" href="' . site_url() . '/demo-aanvragen/?makkina_machine_name=' . $product_title . '">Demo aanvragen</a>';
echo '</div>';
}
endforeach;
}
}
new WcAddFormButtons();
}
如果(!class_存在('WcAddFormButtons')){
类WcAddFormButtons
{
公共函数构造()
{
添加操作('woocommerce\u product\u meta\u end',数组('single\u product\u add\u formbuttons',),50);
}
/**
*检查产品分类的术语pa_aanbod
*获取与表单关联的正确按钮的步骤
*
*@自1.0.0以来
*/
公共功能单个产品添加表单按钮()
{
全球$产品;
$attributes=$product->get_attributes();
$product\U title=获取标题($product->get\U id());
如果(!$attributes){
返回;
}
foreach($attributes作为$attribute):
//跳过变体
如果($attribute['is_variation'])){
继续;
}
if($attribute['is_taxonomy'])){
$terms=wp_get_post_terms($product->get_id(),'pa_aanbod');
回声';
foreach($terms作为$term):
开关($term->name){
“Verhuur”案:
回声';
打破
}
endforeach;
回声';
回声';
回声';
}
endforeach;
}
}
新的WcAddFormButtons();
}
以下是添加附加属性时避免重复的正确代码:
if (!class_exists('WcAddFormButtons')) {
class WcAddFormButtons
{
public function __construct()
{
add_action( 'woocommerce_product_meta_end', array( $this, 'single_product_add_formbuttons', ), 50);
}
/**
* Check the terms of product taxonomy pa_aanbod
* to get the right buttons associated with the forms
*
* @since 1.0.0
*/
public function single_product_add_formbuttons()
{
global $product;
## -- Set HERE below your product attribute taxonomy and term name -- ##
$taxonomy = 'pa_aanbod';
$term_name = 'Verhuur';
$attributes = $product->get_attributes();
$product_title = get_the_title( $product->get_id() );
if ( ! $attributes ) return;
foreach ( $attributes as $attribute ) :
$attribute_data = $attribute->get_data();
if ( $attribute_data['variation'] != 1 && $attribute_data['is_taxonomy'] == 1 && $attribute_data['name'] == $taxonomy ) {
$terms = wp_get_post_terms( $product->get_id(), $taxonomy );
echo '<div class="button-group">';
foreach( $terms as $term ) if( $term->name == $term_name ) {
echo '<a class="vc_general vc_btn vc_btn-style-normal vc_btn-color-main-brand-color full-width" href="' . site_url() . '/direct-huren/?makkina_machine_name=' . $product_title . '">Direct huren</a>';
break;
}
echo '<a class="vc_general vc_btn vc_btn-style-normal vc_btn-color-main-brand-color full-width" href="' . site_url() . '/offerte-aanvragen/?makkina_machine_name=' . $product_title . '">Offerte aanvragen</a>';
echo '<a class="ask-demo" href="' . site_url() . '/demo-aanvragen/?makkina_machine_name=' . $product_title . '">Demo aanvragen</a>';
echo '</div>';
}
endforeach;
}
}
new WcAddFormButtons();
}
如果(!class_存在('WcAddFormButtons')){
类WcAddFormButtons
{
公共函数构造()
{
添加操作('woocommerce\u product\u meta\u end',数组('single\u product\u add\u formbuttons',),50);
}
/**
*检查产品分类的术语pa_aanbod
*获取与表单关联的正确按钮的步骤
*
*@自1.0.0以来
*/
公共功能单个产品添加表单按钮()
{
全球$产品;
##--在产品属性分类法和术语名称下面设置--##
$taxonomy='pa_aanbod';
$term_name='Verhuur';
$attributes=$product->get_attributes();
$product\U title=获取标题($product->get\U id());
如果(!$attributes)返回;
foreach($attributes作为$attribute):
$attribute_data=$attribute->get_data();
如果($attribute_data['variation']!=1&&$attribute_data['is_taxonomy']==1&&&$attribute_data['name']==$taxonomy){
$terms=wp\u get\u post\u terms($product->get\u id(),$taxonomy);
回声';
foreach($terms作为$term)if($term->name==$term\u name){
回声';
打破
}
回声';
回声';
回声';
}
endforeach;
}
}
新的WcAddFormButtons();
}
此代码经过测试并正常工作