php 可变产品的定制库存WooCommerce

ygya80vv  于 2023-01-29  发布在  PHP
关注(0)|答案(2)|浏览(153)

我尝试过这个功能,使WooCommerce中的可变产品的自定义库存状态,但不工作。

add_filter( 'woocommerce_get_availability', 'custom_get_availability', 1, 2);
function custom_get_availability( $availability, $product ) {
    // Only for variable products
    if ( $product->is_type( 'variable' ) ) {
        // Get the stock status of the product
        $stock_status = $product->get_stock_status();
 
        // Custom stock status
        if( $stock_status == 'outofstock' ) {
            $availability['availability'] = __('Custom Out of Stock Message', 'woocommerce');
        }
    }
    return $availability;
}
ruarlubt

ruarlubt1#

add_filter('woocommerce_out_of_stock_message', 'change_variable_out_of_stock_availability', 10, 1);

function change_variable_out_of_stock_availability($out_of_stock_message) {
    return __('Custom Out of Stock Message', 'woocommerce');
}

WooCommerce有可变产品single-product/add-to-cart/variable.php的模板。这个模板可以通过复制到yourtheme/woocommerce/single-product/add-to-cart/variable.php来覆盖。或者这个消息可以用可变产品缺货消息的专用钩子(woocommerce_out_of_stock_message)来修改

9rygscc1

9rygscc12#

你可以试试这个:

add_filter( 'woocommerce_variation_is_in_stock', 'custom_variation_stock_status', 10, 2 );
function custom_variation_stock_status( $is_in_stock, $variation ) {
    if ( ! $is_in_stock ) {
        // Custom stock status
        return 'Custom Out of Stock Message';
    }
    return $is_in_stock;
}

相关问题