我使用此代码在woocommerce上添加股票期权
// New Stock Statuses
function add_custom_stock_type() {
?>
<script type="text/javascript">
jQuery(function(){
jQuery('._stock_status_field').not('.custom-stock-status').remove();
});
</script>
<?php
woocommerce_wp_select( array( 'id' => '_stock_status', 'wrapper_class' => 'hide_if_variable custom-stock-status', 'label' => __( 'Stock status', 'woocommerce' ), 'options' => array(
'instock' => __( 'In Stock', 'woocommerce' ),
'outofstock' => __( 'Out of stock.', 'woocommerce' ),
'onrequest' => __( 'Product is out of stock but available in physical store.', 'woocommerce' ), // The new option without add to cart button
'preorder' => __( 'Product can be preordered.', 'woocommerce' ), // The new option that should have add to cart button
), 'desc_tip' => true, 'description' => __( 'Controls whether or not the product is listed as "in stock" or "out of stock" on the frontend.', 'woocommerce' ) ) );
}
add_action('woocommerce_product_options_stock_status', 'add_custom_stock_type');
function save_custom_stock_status( $product_id ) {
update_post_meta( $product_id, '_stock_status', wc_clean( $_POST['_stock_status'] ) );
}
add_action('woocommerce_process_product_meta', 'save_custom_stock_status',99,1);
function woocommerce_get_custom_availability( $data, $product ) {
switch( $product->stock_status ) {
case 'instock':
$data = array( 'availability' => __( 'In Stock.', 'woocommerce' ), 'class' => 'in-stock' );
break;
case 'outofstock':
$data = array( 'availability' => __( 'Out of Stock.', 'woocommerce' ), 'class' => 'out-of-stock' );
break;
case 'onrequest':
$data = array( 'availability' => __( '', 'woocommerce' ), 'class' => 'on-request' );
echo '<p class="stock on-request">Out of stock, but in physical store</p>';
break;
case 'preorder':
$data = array( 'availability' => __( '', 'woocommerce' ), 'class' => 'preorder' );
echo '<p class="stock on-request">Preorder product.</p>';
break;
}
return $data;
}
add_action('woocommerce_get_availability', 'woocommerce_get_custom_availability', 10, 2);
此代码为WooCommerce中的原始股票期权添加了其他状态,但将新状态设置为缺货,这意味着它会删除产品页面上的添加到购物车按钮。
我想知道,有没有办法增加更多“库存”的库存状态?
我想添加缺货的其他状态,例如现在(删除添加到购物车按钮)和一些仍然有添加到购物车按钮的状态。
这可能吗?
答案 0 :(得分:1)
我一直试图从过去创造这样的东西......并且可以说当前版本的woocommerce并没有提供一种增加自定义股票状态的好方法......
然而,你的代码中缺少的是......
add_filter('woocommerce_product_is_in_stock', 'woocommerce_product_is_in_stock' );
function woocommerce_product_is_in_stock( $is_in_stock ) {
global $product;
// array of custom stock statuses that will have add to cart button
$stock_statuses = array('onrequest','preorder');
if (!$is_in_stock && in_array($product->stock_status, $stock_statuses )) {
$is_in_stock = true;
}
return $is_in_stock;
}
答案 1 :(得分:0)
您是否尝试使用Woocommerce Custom Stock插件? https://wordpress.org/plugins/woo-custom-stock-status/
这有助于我维护自定义股票消息。