如何解决PHP WooCommerce 自定义选项卡功能内容移出所有选项卡内容包装器
我通过使用 WooCommerce API 和用于添加自定义选项卡的文档制作了自定义选项卡功能。
这是我的代码:
/**
* Adding custom WooCommerce Tabs
*/
add_filter( 'woocommerce_product_tabs','woo_new_product_tab' );
function woo_new_product_tab( $tabs ) {
// Array with category ID that doesn't require extra tabs
$target_products_ids = array(
18,// Supplies
43,// Glass
80,// Education
15 // Uncat
);
// Get the global product object
global $product;
// Stores the current product's category ID(s)
$product_cats_ids = wc_get_product_term_ids( $product->get_id(),'product_cat' );
// Comapres both arrays and prints out which element was not found from $target_products_ids in $product_cats_ids
$difference = array_diff($target_products_ids,$product_cats_ids);
// As long as one of the elements from $difference is removed,the tabs for the current product should be unset
if(count($difference) != count($target_products_ids)) return $tabs;
// Creating global variables to be used within inner-functions (not 'product')
global $product,$current_meta_data,$indexFeature,$indexFaqs,$indexTechnicalDetails,$indexSupport;
$id = $product->get_id();
// Connects to WC Rest API
$woocommerce = new Client(
'www.example.com','###',[
'wp_api' => true,'version' => 'wc/v3','query_string_auth' => true # Force Basic Authentication as query string true and using under HTTPS
]
);
$current_product = $woocommerce->get('products/'.$id);
$current_meta_data = $current_product->meta_data;
// Retrieves the current meta data for this current product
$keys = array_column($current_meta_data,'key');
// Within that array of meta data,it finds the index of the given string (meta data name)
$indexFeature = array_search('_features',$keys);
$indexFaqs = array_search('_faqs',$keys);
$indexTechnicalDetails = array_search('_technical_details',$keys);
$indexSupport = array_search('_support',$keys);
// Will add the tab to the current product if the meta data's content is not empty
if(!empty( $current_meta_data[$indexFeature]->value )) {
$tabs['features2'] = array(
'title' => __( 'Features','woocommerce' ),'priority' => 50,'callback' => function() {
global $current_meta_data,$indexFeature;
echo $current_meta_data[$indexFeature]->value;
}
);
}
if(!empty( $current_meta_data[$indexFaqs]->value )) {
$tabs['faqs2'] = array(
'title' => __( 'FAQs','priority' => 51,$indexFaqs;
echo $current_meta_data[$indexFaqs]->value;
}
);
}
if(!empty( $current_meta_data[$indexTechnicalDetails]->value )) {
$tabs['technical_details2'] = array(
'title' => __( 'Technical Details','priority' => 52,$indexTechnicalDetails;
echo $current_meta_data[$indexTechnicalDetails]->value;
}
);
}
if(!empty( $current_meta_data[$indexSupport]->value )) {
$tabs['support2'] = array(
'title' => __( 'Support','priority' => 53,$indexSupport;
echo $current_meta_data[$indexSupport]->value;
}
);
}
// Returns the changed tabs to this current product
return $tabs;
}
它适用于许多其他产品,但其中一个决定打破我。就好像函数运行但不小心将标签内容子级留在了父级之外。
整个标签容器下面的内容实际上是来自支持标签的内容。
有趣的是,如果我要单击“支持”选项卡,那么一切都会中断。我将无法点击任何其他标签。
无论如何,作为一种解决方法,我使用以下 javascript 将子级附加回其父级:
var tab_out_range = $("div.v-product-description-row div.et_pb_column > div.et_pb_tab");
if(tab_out_range.length > 0) {
console.log(tab_out_range);
tab_out_range.each(i => {
$(tab_out_range[i]).detach().appendTo('div.v-product-description-row div.et_pb_all_tabs');
});
}
它运行良好......但我想知道是否有更好的选择来解决这个问题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。