gpt4 book ai didi

php - 在 Woocommerce 的下拉变体属性选项中显示价格和库存状态

转载 作者:行者123 更新时间:2023-12-05 03:03:49 24 4
gpt4 key购买 nike

在我的可变产品的 woocommerce 上,每个变体都有特定的价格。

如何在属性下拉选项中显示变化价格和库存状态?

screenshot of variation dropdown

最佳答案

Note: This only works when there is ONLY ONE dropdown select field (so one attribute for the variations set in the variable product). With multiple attributes (so multiple dropdown select fields) it displays something that can be wrong depending on the variations stock status attributes terms combination.

以下代码将在唯一的下拉变体属性选项中显示变体价格和库存状态:

// Utility function to get the price or the stock status of a variation from it's attribute value
function get_variation_price_stock_string( $product, $name, $term_slug ){
foreach ( $product->get_available_variations() as $variation ){
if($variation['attributes'][$name] == $term_slug ){
$stock_status = $variation['is_in_stock'] == 1 ? __('In stock') : __('Out of stock');
return ' (' . strip_tags( $variation['price_html'] ) . ') ' . $stock_status;
}
}
}

// Add the price and stock status to the dropdown options items.
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'show_stock_status_in_dropdown', 10, 2);
function show_stock_status_in_dropdown( $html, $args ) {
// Only if there is a unique variation attribute (one dropdown)
if( sizeof($args['product']->get_variation_attributes()) == 1 ) :

$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' );

if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
$attributes = $product->get_variation_attributes();
$options = $attributes[ $attribute ];
}

$html = '<select id="' . esc_attr( $id ) . '" class="' . esc_attr( $class ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" data-show_option_none="' . ( $show_option_none ? 'yes' : 'no' ) . '">';
$html .= '<option value="">' . esc_html( $show_option_none_text ) . '</option>';

if ( ! empty( $options ) ) {
if ( $product && taxonomy_exists( $attribute ) ) {
$terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) );

foreach ( $terms as $term ) {
if ( in_array( $term->slug, $options ) ) {
// Get the price and stock status
$price_stock_html = get_variation_price_stock_string( $product, $name, $term->slug );
// Insert the price and stock status
$html .= '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) . $price_stock_html ) . '</option>';
}
}
} else {
foreach ( $options as $option ) {
$selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false );
// Get the price and stock status
$price_stock_html = get_variation_price_stock_string( $product, $name, $term->slug );
// Insert the price and stock status
$html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) . $price_stock_html ) . '</option>';
}
}
}
$html .= '</select>';

endif;

return $html;
}

代码进入事件子主题(或事件主题)的 function.php 文件。测试和工作。

enter image description here

基于:

关于php - 在 Woocommerce 的下拉变体属性选项中显示价格和库存状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53489981/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com