【问题标题】:stock quantity woocommorce库存数量 woocommerce
【发布时间】:2021-06-10 08:34:18
【问题描述】:

我的库存显示有问题。 我们目前的代码,如果“变种产品”的数量超过5个,则不显示库存信息。我们也想将它应用于“简单产品”。我们应该如何修改我们的代码?

function get_variation_stock_status( $product, $name, $term_slug ){
    foreach ( $product->get_available_variations() as $variation ){
        if ( $variation['attributes'][$name] == $term_slug ) {
            $variation_obj = wc_get_product( $variation['variation_id'] );

            // if the stock of the product is greater than 20 it returns
            if ( $variation_obj->get_stock_quantity() > 5 ) {
                return '';
            }
            
            $stock_qty = $variation_obj->get_stock_quantity();
            break;
        }
    }
   return $stock_qty == 0 ? ' - ' . __(pll__('Stokta Yok'), 'mytheme-hello') : ' - ' . $stock_qty . '  ' .  __(pll__('adet Stokta'), 'mytheme-hello');
}

如果您能提供帮助,我将非常高兴。 提前致谢。

【问题讨论】:

    标签: php wordpress woocommerce stock


    【解决方案1】:

    您可以使用is_type() 简单地检查产品类型。检查下面的代码。

    function get_variation_stock_status( $product, $name, $term_slug ){
    
        if ( $product->is_type( 'simple' ) ) {
    
            // if the stock of the product is greater than 20 it returns
            if ( $product->get_stock_quantity() > 5 ) {
                return '';
            }
    
            $stock_qty = $product->get_stock_quantity();
    
        } elseif ( $product->is_type( 'variable' ) ) {
            
            foreach ( $product->get_available_variations() as $variation ){
    
                if ( $variation['attributes'][$name] == $term_slug ) {
    
                    $variation_obj = wc_get_product( $variation['variation_id'] );
    
                    // if the stock of the product is greater than 20 it returns
                    if ( $variation_obj->get_stock_quantity() > 5 ) {
                        return '';
                    }
                    
                    $stock_qty = $variation_obj->get_stock_quantity();
                    break;
                }
    
            }
            
        }
        
        return $stock_qty == 0 ? ' - ' . __(pll__('Stokta Yok'), 'mytheme-hello') : ' - ' . $stock_qty . '  ' .  __(pll__('adet Stokta'), 'mytheme-hello');
        
    }
    

    【讨论】:

    • 感谢您的回复,但没有任何改变。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-17
    • 1970-01-01
    • 2016-05-06
    相关资源
    最近更新 更多