1

我正在尝试为 Woocommerce 中的产品添加新的“已售”状态。我已经找到了许多解决方案,但没有一个解决方案将 Sold 视为缺货。

售出的产品也缺货,但他们永远不会再有货了。

我找到的所有解决方案都将“已售出”状态设置为“可延期交货”或“有货”。但我需要它像“缺货”产品一样由 Woocommerce 处理。

这是我最近的尝试。

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' ),
    'available_on_backorder' => __('On Backorder', 'woocommerce' ),
    'sold' => __( 'Sold', 'woocommerce' ), // New
), '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 ) {
$stock_status = get_post_meta($product->id , '_stock_status' , true );
switch( $stock_status  ) {
    case 'sold':
        $data = array( 'availability' => __( 'Sold', 'woocommerce' ), 'class' => 'out-of-stock' );
    break;
    case 'instock':
        $data = array( 'availability' => __( 'In stock', 'woocommerce' ), 'class' => 'in-stock' );
    break;
    case 'onbackorder':
        $data = array( 'availability' => __( 'On Backorder', 'woocommerce'), 'class' => 'available-on-backorder');
    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', 10,4);

我还注意到在

add_action('woocommerce_get_availability', 'woocommerce_get_custom_availability', 10,4);

将主要改变(可在延期交货时)和库存之间的状态。

4

0 回答 0