【发布时间】:2018-08-21 20:18:01
【问题描述】:
在 Woocommerce 中,variable.php 中有一段代码用于显示变体选项。我需要在标题旁边的列表中显示每个单独变体的 SKU。有没有一种类似于$product->get_sku; 的方法可以做到这一点?
这是完整的块:
global $product;
$attribute_keys = array_keys( $attributes );
<?php foreach ( $attributes as $name => $options ) : ?>
<tr class="attribute-<?php echo sanitize_title($name); ?>">
<?php
$sanitized_name = sanitize_title( $name );
if ( isset( $_REQUEST[ 'attribute_' . $sanitized_name ] ) ) {
$checked_value = $_REQUEST[ 'attribute_' . $sanitized_name ];
} elseif ( isset( $selected_attributes[ $sanitized_name ] ) ) {
$checked_value = $selected_attributes[ $sanitized_name ];
} else {
$checked_value = '';
}
?>
<td class="value">
<?php
if ( ! empty( $options ) ) {
if ( taxonomy_exists( $name ) ) {
// Get terms if this is a taxonomy - ordered. We need the names too.
$terms = wc_get_product_terms( $product->get_id(), $name, array( 'fields' => 'all' ) );
foreach ( $terms as $term ) {
if ( ! in_array( $term->slug, $options ) ) {
continue;
}
print_attribute_radio( $checked_value, $term->slug, $term->name, $sanitized_name );
}
} else {
foreach ( $options as $option ) {
echo $product->get_sku; // This is where the variation SKU would go
print_attribute_radio( $checked_value, $option, $option, $sanitized_name );
}
}
}
echo end( $attribute_keys ) === $name ? apply_filters( 'woocommerce_reset_variations_link', '<a class="reset_variations" href="#">' . __( 'Clear', 'woocommerce' ) . '</a>' ) : '';
?>
</td>
</tr>
<?php endforeach; ?>
这是变体 SKU 在此部分中的位置:
foreach ( $options as $option ) {
echo $product->get_sku; // This is where the variation SKU would go
print_attribute_radio( $checked_value, $option, $option,
$sanitized_name );
}
谢谢。
【问题讨论】:
标签: php wordpress woocommerce