我正在尝试将自定义 stock_status 添加到 WordPress 中的 woocommerce 3.0 中。
最终目标是在产品编辑页面上添加第三个库存选项“暂停”,并在产品页面上显示该库存状态。
以前我可以使用这里的方法:在 woocommerce 中添加股票期权 https://stackoverflow.com/questions/26912556/add-stock-option-in-woocommerce,它确实可以向产品管理添加额外的库存选项,但看起来 Woocommerce 3.0 有一些东西覆盖了我在实际产品页面上的设置。它也在产品页面上运行,直到我升级到 3.0。
我的functions.php:
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' ),
'onhold' => __( 'On Hold', 'woocommerce' ), // The new option !!!
), '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->get_stock_status ) {
case 'onhold':
$data = array( 'availability' => __( 'On Hold', 'woocommerce' ), 'class' => 'on-hold' );
break;
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;
}
return $data;
}
add_action('woocommerce_get_availability', 'woocommerce_get_custom_availability', 3, 2);
https://pastebin.com/EFtBVY9h https://pastebin.com/EFtBVY9h
当我在管理员上选择它时,我可以在数据库中看到 stock_status 已正确设置为“暂停”(我的自定义状态),但这并未应用于实际产品页面。
为了验证,我修改了price.php以简单地输出库存状态:
<p class="price">
<?php
$stockamount = $product->get_stock_quantity();
$price = $product->get_price_html();
$stockstatus = $product->get_stock_status();
$pricelabelone = "Out of Stock";
$pricelabeltwo = "On Hold";
echo $stockstatus;
?>
</p>
但是,即使我将产品设置为“暂停”(这是保存),产品页面始终输出“有库存”(测试产品:http://aegis-staging.byethost7.com/dgh/product/santa-cruz-h13-custom-42-cocobolo-and-moon-spruce/ http://aegis-staging.byethost7.com/dgh/product/santa-cruz-h13-custom-42-cocobolo-and-moon-spruce/).
我缺少什么?