无法使用功能来禁用带有售罄消息的缺货产品变体 [英] Unable to get functionality to work to disable out of stock product variant with sold out message

查看:20
本文介绍了无法使用功能来禁用带有售罄消息的缺货产品变体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的functions.php 中,我尝试在我的产品变体的下拉菜单中添加售罄消息.因此,例如,如果我的衬衫有小号、中号和大号款式,而大号却缺货,在下拉菜单中,用户应该会看到大号选项被禁用,并且在大号"旁边包含一条售罄的消息.其他变体应保持活动状态.

In my functions.php, I am trying to add a sold out message in the drop down menu for my product variants. So for example if I have shirt that has variants of small, medium and large and the large is out of stock, in the drop down menu the user should see the large option is disabled and contains a sold out message next to 'Large'. The other variants should remain active.

我在下面的代码中遇到的问题如下:

The issue I have with my code below is the following:

  • 注释的代码,这会禁用缺货的正确产品变体,但不会添加售罄消息.
  • 活动代码确实添加了售罄消息,但它会禁用所有产品变体,即使只有一种变体缺货.

如何修复代码以完成我需要它做的事情?

How can I fix the code to do what I need it to do?

/**
 * Disable out of stock variations
 * https://github.com/woocommerce/woocommerce/blob/826af31e1e3b6e8e5fc3c1004cc517c5c5ec25b1/includes/class-wc-product-variation.php
 * @return Boolean
 */

// function wcbv_variation_is_active( $active, $variation ) {
//  if( ! $variation->is_in_stock() ) {
//  return false;
//  }
//  return $active;
// }
// add_filter( 'woocommerce_variation_is_active', 'wcbv_variation_is_active', 10, 2 );


add_action( 'woocommerce_variation_is_active', 'woocommerce_sold_out_dropdown' );
function woocommerce_sold_out_dropdown() {
?>
<script type="text/javascript">
jQuery( document ).bind( 'woocommerce_update_variation_values', function() {

jQuery( '.variations select option' ).each( function( index, el ) {
var sold_out = '<?php _e( 'sold out', 'woocommerce' ); ?>';
var re = new RegExp( ' - ' + sold_out + '$' );
el = jQuery( el );

if ( el.is( ':disabled' ) ) {
 if ( ! el.html().match( re ) ) el.html( el.html() + ' - ' + sold_out );
} else {
if ( el.html().match( re ) ) el.html( el.html().replace( re,'' ) );
}
} );
} );
</script>
 <?php
}

推荐答案

您可以使用此代码(您的代码)获得所需内容:

You can get what you need with this code (your code):

// disable options for unavailable variants
add_filter( 'woocommerce_variation_is_active', 'wcbv_variation_is_active', 10, 2 );
function wcbv_variation_is_active( $active, $variation ) {
    if ( ! $variation->is_in_stock() ) {
        return false;
    }
    return $active;
}

现在,要根据股票状态更改每个选项的名称,您可以像这样使用 woocommerce_variation_option_name 钩子:

And now, to change the name of each individual option based on stock status you can use the woocommerce_variation_option_name hook like so:

add_filter( 'woocommerce_variation_option_name','add_stock_status_after_option_name', 10, 1 );
function add_stock_status_after_option_name( $option ) {
    // only in frontend
    if ( is_admin() ) {
        return $option;
    }
    // get variable product object
    global $product;
    $variation_ids = $product->get_children();
    foreach ( $variation_ids as $variation_id ) {
        $variation = wc_get_product( $variation_id );
        $variation_attributes = $variation->get_variation_attributes();
        foreach ( $variation_attributes as $key => $value ) {
            // slugify option name
            $option_slug = sanitize_title( $option );
            // check if the current option is equal to the variation slug
            if ( $value == $option_slug ) {
                // check if it is out of stock
                if ( ! $variation->is_in_stock() ) {
                    return $option . ' (Out of stock)';
                }
            }
        }
    }
    return $option;
}

代码已经过测试并且可以正常工作.它需要添加到您主题的functions.php.

The code has been tested and works correctly. It needs to be added to your theme's functions.php.

这篇关于无法使用功能来禁用带有售罄消息的缺货产品变体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆