Php Woocommerce:检索可变产品选项的SKU
在Woocommerce中,Php Woocommerce:检索可变产品选项的SKU,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,在Woocommerce中,variable.php中有一块代码,显示变体选项。我需要在标题旁边的列表中显示每个单独变量的SKU。是否有一种类似于$product->get_sku 这是完整的块: global $product; $attribute_keys = array_keys( $attributes ); <?php foreach ( $attributes as $name => $options ) : ?>
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; ?>
谢谢。将您的foreach替换为:
$product = new WC_Product_Variable( $post->ID );
$variations = $product->get_available_variations();
foreach ($variations as $key => $variation) {
$sku = $variation['sku'];
$options = $variation['attributes'];
.
.
.
$product = new WC_Product_Variable( $post->ID );
$variations = $product->get_available_variations();
foreach ($variations as $key => $variation) {
$sku = $variation['sku'];
$options = $variation['attributes'];
.
.
.