1 回答
TA贡献2021条经验 获得超8个赞
这应该足够了,注释并添加到我的代码中
对于 和
single
产品variable
,SKU 表行添加到附加信息选项卡。SKU 表格行会根据
variable
产品的下拉选择菜单进行相应更新
function display_product_attributes( $product_attributes, $product ) {
// Simple product
if ( $product->is_type('simple' ) ) {
// Get product SKU
$get_sku = ( $sku = $product->get_sku() ) ? $sku : esc_html__( 'N/A', 'woocommerce' );
// Add
$product_attributes[ 'sku-field sku-field-single' ] = array(
'label' => __('SKU', 'woocommerce'),
'value' => $get_sku,
);
}
// Variable product
elseif ( $product->is_type('variable' ) ) {
// Get childIDs in an array
$children_ids = $product->get_children();
// Loop
foreach ( $children_ids as $child_id ) {
// Get product
$product = wc_get_product( $child_id );
// Get product SKU
$get_sku = ( $sku = $product->get_sku() ) ? $sku : esc_html__( 'N/A', 'woocommerce' );
// Add
$product_attributes[ 'sku-field sku-field-variable sku-field-variable-' . $child_id ] = array(
'label' => __('SKU', 'woocommerce'),
'value' => $get_sku,
);
}
?>
<script>
jQuery(document).ready(function($) {
// Hide all rows
$( '.sku-field-variable' ).css( 'display', 'none' );
// Change
$( 'input.variation_id' ).change( function() {
// Hide all rows
$( '.sku-field-variable' ).css( 'display', 'none' );
if( $( 'input.variation_id' ).val() != '' ) {
var var_id = $( 'input.variation_id' ).val();
// Display current
$( '.sku-field-variable-' + var_id ).css( 'display', 'table-row' );
}
});
});
</script>
<?php
}
return $product_attributes;
}
add_filter('woocommerce_display_product_attributes', 'display_product_attributes', 10, 2);
- 1 回答
- 0 关注
- 98 浏览
添加回答
举报