php - 在 Woocommerce 中显示所选变体的产品属性术语
问题描述
在 Woocommerce 中,我使用以下代码来获取产品属性术语。
function action_woocommerce_product_meta_end() {
global $product;
$term_ids = wp_get_post_terms( $product->get_id(), 'pa_apple-id', array('fields' => 'ids') );
echo get_the_term_list( $product->get_id(), 'pa_apple-id', '<span class="posted_in">' . _n( 'Vendor:', 'Vendors:', count( $term_ids ), 'woocommerce' ) . ' ', ', ', '</span>' );
}
add_action( 'woocommerce_product_meta_end', 'action_woocommerce_product_meta_end', 10, 0 );
但这显示了所有条款:
Instead, I would like to display only the corresponding unique term for the product attribute pa_apple-id
, when a variation is selected.
解决方案
2021 年更新:
以下答案已过时,取而代之的是以下线程:在 WooCommerce 单一产品上动态设置和显示自定义产品变化值
这要复杂得多,因为它是客户端的实时事件,因此需要 javascript/jQuery 和更多代码:
// Set the defined product attribute taxonomy
function vendor_defined_taxonomy() {
// The targeted product attribute taxonomy
return 'pa_apple-id';
}
// Display the vendors on product meta
add_action( 'woocommerce_product_meta_end', 'display_product_vendors', 10 );
function display_product_vendors() {
$taxonomy = vendor_defined_taxonomy();
$term_ids = wp_get_post_terms( get_the_ID(), $taxonomy, array('fields' => 'ids') );
if( sizeof($term_ids) > 0 ){
$label = _n("Vendor:", "Vendors:", count($term_ids), "woocommerce");
echo get_the_term_list( get_the_ID(), $taxonomy, '<span class="posted_in vendors">' . $label . ' ', ', ', '</span>' );
}
}
// Display the selected variation vendor in a hidden imput field
add_filter( 'woocommerce_available_variation', 'selected_variation_vendor_value', 10, 3 );
function selected_variation_vendor_value( $data, $product, $variation ) {
$taxonomy = vendor_defined_taxonomy();
if( isset($data['attributes']['attribute_'.$taxonomy]) )
$term = get_term_by( 'slug', $data['attributes']['attribute_'.$taxonomy], $taxonomy );
if( isset($term) && is_a($term, 'WP_Term' ) )
$data['variation_description'] .= '<input type="hidden" name="vendor-hidden" id="vendor-hidden" value="'.$term->name.'">';
return $data;
}
// Replace the vendors on product meta by the selected variation vendor
add_action('woocommerce_after_variations_form', 'custom_product_jquery_script');
function custom_product_jquery_script() {
global $product;
$taxonomy = vendor_defined_taxonomy();
$terms_string = $product->get_attribute($taxonomy);
if( ! empty($terms_string) ) :
?>
<script type="text/javascript">
jQuery(function($) {
var form = 'form.variations_form', selected = 'input[name="variation_id"]',
vendorVal = 'input#vendor-hidden', vendorTarget = 'span.vendors',
vendorHtml = $(vendorTarget).text(), vendorLabel = '<?php _e("Vendor:", "woocommerce"); ?>';
// Once DOM is loaded
setTimeout(function() {
if($(selected).val() != ''){
$(vendorTarget).text(vendorLabel+' '+$(vendorVal).val());
}
}, 300 );
// On variation select
$(form).on( 'blur', 'select', function() {
if($(selected).val() != ''){
$(vendorTarget).text(vendorLabel+' '+$(vendorVal).val());
} else {
$(vendorTarget).text(vendorHtml);
}
});
});
</script>
<?php
endif;
}
代码位于活动子主题(或活动主题)的 function.php 文件中。测试和工作。
推荐阅读
- r - 灰色ggplot2中正方形上方的填充区域
- swift - 使用数据结果 nil 创建 SecKey
- reactjs - 如何在不将数据传递到状态的情况下从 db 获取数据?
- c++ - Qml WinRt 如何设置窗口大小
- azure - 如何从 powershell 更新 Azure APIM 中的通知模板
- android - 无法解析外部依赖项 com.android.tools:desugar_jdk_libs:1.0.9 因为没有定义存储库
- python - qtCreator-Python图形效果qml模块未找到错误?
- c# - Hangfire 作业未触发 - 没有错误
- spring-boot - 我正在尝试将数据写入 csv 并在尝试访问 api 时出现错误(HttpServletResponse 导致问题但无法弄清楚)
- java - 如何通过打包和服务器上传让thymeleaf文件出现在服务器WEB-INF文件夹之外?