Chr*_*s M 4 php wordpress stock woocommerce dropdown
我想在Woocommerce产品页面上的变体下拉列表中显示每个产品变体的库存状态(例如,库存/缺货)。我已经将相关功能复制到主题的functions.php文件中,并且可以编辑内容,但是不确定如何提取每种变体的所需库存状态。
//更新了Woocommerce产品版本选择
如果(!function_exists('wc_dropdown_variation_attribute_options')){
/ **
*输出用于购物车表单的变体属性列表。
*
* @参数数组$ args
* @自2.4.0起
* /
/ *
函数wc_dropdown_variation_attribute_options($ args = array()){
$ args = wp_parse_args(apply_filters('woocommerce_dropdown_variation_attribute_options_args',$ args),数组(
'选项'=>否,
'attribute'=>否,
'产品'=>否,
'已选择'=>否,
'名称'=>'',
'id'=>'',
'class'=>'',
'show_option_none'=> __('选择一个选项','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']吗?真假;
$ show_option_none_text = $ args ['show_option_none']吗?$ args ['show_option_none']:__('选择一个选项','woocommerce'); //我们将尽力隐藏占位符,但在重置选项时需要显示一些内容。
if(空($ options)&&!空($ product)&&!空($ attribute)){
$ attributes = $ product-> get_variation_attributes();
$ options = $ attributes [$ attribute];
}
$ html ='';
$ html。=''。esc_html($ show_option_none_text)。'';
如果(!empty($ options)){
如果($ product && taxonomy_exists($ attribute)){
//如果这是一个分类法,则获取术语-已排序。我们也需要名称。
$ terms = wc_get_product_terms($ product-> get_id(),$ attribute,array('fields'=>'all'));
foreach($ terms as $ term){
如果(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)))。'';
}
}
}其他{
foreach($ options as $ option){
//处理未清除文本属性的lt 2.4.0 bw兼容性。
$ 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))。'在此处输出库存明细';
}
}
}
$ html。='';
回声apply_filters('woocommerce_dropdown_variation_attribute_options_html',$ html,$ args);
}
}
我可以提取整个产品的库存水平,但是现在可以提取每个版本的库存水平。
任何帮助将不胜感激。
好的,首先你需要得到这样的产品变体:
$variations = $product->get_available_variations();
Run Code Online (Sandbox Code Playgroud)
在期权循环中,您需要遍历变化并找到当前的期权库存状态
foreach ($variations as $variation) {
if($variation['attributes'][$name] == $option) {
$stock = $variation['is_in_stock'];
}
}
Run Code Online (Sandbox Code Playgroud)
在变体循环之外,您需要添加库存和缺货变体的措辞
if( $stock == 1) {
$stock_content = ' - In stock';
} else {
$stock_content = ' - Out of stock';
}
Run Code Online (Sandbox Code Playgroud)
然后更改 html 以包含一个附加变量 ($stock_content)
$html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( $option . $stock_content ) . '</option>';
Run Code Online (Sandbox Code Playgroud)
所以一个完整的函数看起来像这样:
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 = '<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 ) ) {
// 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 .= '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) ) . '</option>';
}
}
} 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 .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( $option . $stock_content ) . '</option>';
}
}
}
$html .= '</select>';
return $html;
}
Run Code Online (Sandbox Code Playgroud)
2019年更新 (仅适用于具有1个下拉列表的可变产品)
无论如何,当只有一个下拉选择字段时(这是一个用于变体的属性),它将真正起作用。
使用多个属性(因此有多个下拉选择字段),它会显示一些错误,具体取决于库存状态属性的术语组合。
请参阅最后显示错误展示情况的屏幕截图……
我已经尝试了Ali_k的代码,但是当可变产品具有多个下拉菜单时,它在我的测试服务器中不起作用。
因此,我对Ali_k的代码进行了一些更改,以使其在我的测试服务器(最新的WooCommerce版本)中正常工作。
要处理延期交货,请参阅:将延期交货库存状态添加到Woocommerce可变产品下拉列表中
代码:
// Function that will check the stock status and display the corresponding additional text
function get_stock_status_text( $product, $name, $term_slug ){
foreach ( $product->get_available_variations() as $variation ){
if($variation['attributes'][$name] == $term_slug ) {
$stock = $variation['is_in_stock'];
break;
}
}
return $stock == 1 ? ' - (In Stock)' : ' - (Out of Stock)';
}
// The hooked function that will add the stock status to the dropdown options elements.
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']; // The product attribute taxonomy
$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 ) ) {
// HERE Added the function to get the text status
$stock_status = get_stock_status_text( $product, $name, $term->slug );
$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 ) . $stock_status ) . '</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 );
// HERE Added the function to get the text status
$stock_status = get_the_stock_status( $product, $name, $option );
$html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) . $stock_status ) . '</option>';
}
}
}
$html .= '</select>';
endif;
return $html;
}
Run Code Online (Sandbox Code Playgroud)
代码在您的活动子主题(或主题)的function.php文件中,或者在任何插件文件中。
经过测试并在可变产品中工作,这些产品仅具有一种差异属性 ...
使用Ali_K的代码,下面是一个具有多个选择字段 的可变产品的错误显示文本的示例(因此,多个属性用于变体):
| 归档时间: |
|
| 查看次数: |
2929 次 |
| 最近记录: |