我想显示Woocommerce产品页面变体下拉列表中显示的每种产品差异的库存状态(例如库存/缺货).我已将相关功能复制到我的主题的functions.php文件中,并且可以编辑内容,但我不确定如何为每个变体提取所需的库存状态.
// Updated Woocommerce Product Variation Select
if ( ! function_exists( 'wc_dropdown_variation_attribute_options' ) ) {
/**
* Output a list of variation attributes for use in the cart forms.
*
* @param array $args
* @since 2.4.0
*/
/*
function wc_dropdown_variation_attribute_options( $args = array() ) {
$args = wp_parse_args( apply_filters( 'woocommerce_dropdown_variation_attribute_options_args', $args ), array(
'options' => false,
'attribute' => false,
'product' => false,
'selected' => false,
'name' => '',
'id' => '',
'class' => '',
'show_option_none' => __( 'Choose an option', 'woocommerce' ),
) );
$options = $args['options'];
$product = $args['product'];
$attribute = $args['attribute'];
$name = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title( $attribute );
$id = $args['id'] ? $args['id'] : sanitize_title( $attribute );
$class = $args['class'];
$show_option_none = $args['show_option_none'] ? true : false;
$show_option_none_text = $args['show_option_none'] ? $args['show_option_none'] : __( 'Choose an option', 'woocommerce' ); // We'll do our best to hide the placeholder, but we'll need to show something when resetting options.
if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
$attributes = $product->get_variation_attributes();
$options = $attributes[ $attribute ];
}
$html = '';
$html .= '' . esc_html( $show_option_none_text ) . '';
if ( ! empty( $options ) ) {
if ( $product && taxonomy_exists( $attribute ) ) {
// Get terms if this is a taxonomy - ordered. We need the names too.
$terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) );
foreach ( $terms as $term ) {
if ( in_array( $term->slug, $options ) ) {
$html .= 'slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) ) . ' ';
}
}
} else {
foreach ( $options as $option ) {
// This handles lt 2.4.0 bw compatibility where text attributes were not sanitized.
$selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false );
$html .= '' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) ) . ' Output Stock Details Here ';
}
}
}
$html .= '';
echo apply_filters( 'woocommerce_dropdown_variation_attribute_options_html', $html, $args );
}
}
我可以提取整个产品的库存水平,但现在每个变化.
任何帮助将不胜感激.
解决方法:
好的,首先你需要获得这样的产品变化:
$variations = $product->get_available_variations();
在内部选项循环中,您需要遍历变量并找到当前选项库存状态
foreach ($variations as $variation) {
if($variation['attributes'][$name] == $option) {
$stock = $variation['is_in_stock'];
}
}
在变体循环之外,您需要添加库存和缺货变化的措辞
if( $stock == 1) {
$stock_content = ' - In stock';
} else {
$stock_content = ' - Out of stock';
}
然后更改html以包含一个额外的变量($stock_content)
$html .= '' . esc_html( $option . $stock_content ) . '';
所以一个完整的函数将如下所示:
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'show_stock_status_in_dropdown', 10, 2);
function show_stock_status_in_dropdown( $html, $args ) {
$options = $args['options'];
$product = $args['product'];
$attribute = $args['attribute'];
$name = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title( $attribute );
$id = $args['id'] ? $args['id'] : sanitize_title( $attribute );
$class = $args['class'];
$show_option_none = $args['show_option_none'] ? true : false;
$show_option_none_text = $args['show_option_none'] ? $args['show_option_none'] : __( 'Choose an option', 'woocommerce' );
// Get all product variations
$variations = $product->get_available_variations();
if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
$attributes = $product->get_variation_attributes();
$options = $attributes[ $attribute ];
}
$html = '';
$html .= '' . esc_html( $show_option_none_text ) . '';
if ( ! empty( $options ) ) {
if ( $product && taxonomy_exists( $attribute ) ) {
// Get terms if this is a taxonomy - ordered. We need the names too.
$terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) );
foreach ( $terms as $term ) {
if ( in_array( $term->slug, $options ) ) {
$html .= 'slug, false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) ) . '';
}
}
} else {
foreach ( $options as $option ) {
foreach ($variations as $variation) {
if($variation['attributes'][$name] == $option) {
$stock = $variation['is_in_stock'];
}
}
if( $stock == 1) {
$stock_content = ' - (In Stock)';
} else {
$stock_content = ' - (Out of Stock)';
}
// This handles < 2.4.0 bw compatibility where text attributes were not sanitized.
$selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false );
$html .= '' . esc_html( $option . $stock_content ) . '';
}
}
}
$html .= '';
return $html;
}
标签:dropdown,php,wordpress,woocommerce,stock
来源: https://codeday.me/bug/20190918/1811327.html