【问题标题】:Add a custom stock status in WooCommerce [duplicate]在 WooCommerce 中添加自定义库存状态 [重复]
【发布时间】:2014-11-13 15:34:08
【问题描述】:

我想在产品的股票选项下拉列表中添加一个新选项。默认情况下,有“缺货”、“有货”,我想添加第三个选项。

我找到了显示下拉列表的方法(在 class-wc-meta-box-product-data.php 中)

    // Stock status
    woocommerce_wp_select( array( 'id' => '_stock_status', 'wrapper_class' => 'hide_if_variable', 'label' => __( 'Stock status', 'woocommerce' ), 'options' => array(
        'instock' => __( 'In stock', 'woocommerce' ),
        'outofstock' => __( 'Out of stock', 'woocommerce' )
    ), 'desc_tip' => true, 'description' => __( 'Controls whether or not the product is listed as "in stock" or "out of stock" on the frontend.', 'woocommerce' ) ) );

    do_action( 'woocommerce_product_options_stock_status' );

但我不想直接编辑 Woocommerce 类,这样我们就可以在不丢失任何自定义代码的情况下更新 Woocommerce。有没有办法覆盖这个方法?

【问题讨论】:

  • 抱歉,如果不更改核心文件,您将无法找到您要查找的内容,这些更改将在升级时丢失。 support.woothemes.com/hc/communities/public/questions/…
  • 我已经看过这个链接,但我想知道是否还有其他解决方案。您可以在下面查看我自己的答案。

标签: php wordpress coding-style woocommerce


【解决方案1】:

对于任何感兴趣的人,这里是基于 Laila 方法的完整解决方案。警告!我的解决方案仅适用于 WooCommerce 禁用“管理库存”选项!我没有处理确切数量的库存物品。像往常一样,所有代码都转到functions.php

后端部分

移除原生库存状态下拉字段。添加 CSS 类来区分我的新自定义字段。下拉菜单现在有新选项“On Request”。

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' => __( 'On Request', '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');

遗憾的是,WooCommerce 将仅使用其原生功能保存“instock”或“outofstock”值。所以在所有产品数据处理之后,我必须再次重新保存我的库存状态。

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);

模板部分

最后一件事 - 我必须更改产品 get_availability() 函数返回的数据。当“管理库存”关闭时,WooCommerce 再次只知道“instock”和“outofstock”值。所以我自己检查库存状态。

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' => __( 'On request', 'woocommerce' ), 'class' => 'on-request' );
        break;
    }
    return $data;
}
add_action('woocommerce_get_availability', 'woocommerce_get_custom_availability', 10, 2);

也许它不是防弹解决方案......我最终会更新它。

【讨论】:

  • 这对我有用,谢谢。有现货、缺货、1-3天和3-5天的期权。
  • 看起来像 add_action('woocommerce_get_availability', 'woocommerce_get_custom_availability', 10, 2);应该是一个 add_filter 来完成这项工作。
  • 不,add_actionadd_filter 是相同的功能。见add_action source code。但是是的,应该是 add_filter 以便更好地理解。
【解决方案2】:

好吧,我最终在 Javascript 中隐藏了以前的股票期权下拉菜单

add_action('woocommerce_product_options_stock_status', 'add_custom_stock_type');    
function add_custom_stock_type() {
        // Stock status - We remove the default one
        ?>
        <script type="text/javascript">
            jQuery('_stock_status').remove();
        </script>
        <?php   
    }

并使用本教程创建了一个新的:http://www.remicorson.com/mastering-woocommerce-products-custom-fields/ 不确定这是最干净的解决方案,但至少不会触及核心文件! :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-08
    • 2015-06-29
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多