首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何添加过滤器来隐藏缺货产品时,搜索向上销售和交叉销售管理面板上的链接产品选项卡(在woocommerce)?

如何添加过滤器来隐藏缺货产品时,搜索向上销售和交叉销售管理面板上的链接产品选项卡(在woocommerce)?
EN

Stack Overflow用户
提问于 2021-02-11 05:38:51
回答 1查看 84关注 0票数 1

在单个产品上,在管理面板中,在链接产品选项卡上(在woocommerce上),我可以设置向上销售和交叉销售产品。我可以添加我希望通过搜索链接到的产品。

目前,搜索给出了所有的产品,甚至是那些脱销的产品。我希望如果我写下缺货的产品的id,它不会出现在列表中。

负责json结果的函数是

代码语言:javascript
运行
复制
public static function json_search_products_and_variations() {
self::json_search_products( '', true );
}

public static function json_search_products( $term = '', $include_variations = false ) {
check_ajax_referer( 'search-products', 'security' );

if ( empty( $term ) && isset( $_GET['term'] ) ) {
    $term = (string) wc_clean( wp_unslash( $_GET['term'] ) );
}

if ( empty( $term ) ) {
    wp_die();
}

if ( ! empty( $_GET['limit'] ) ) {
    $limit = absint( $_GET['limit'] );
} else {
    $limit = absint( apply_filters( 'woocommerce_json_search_limit', 30 ) );
}

$include_ids = ! empty( $_GET['include'] ) ? array_map( 'absint', (array) wp_unslash( $_GET['include'] ) ) : array();
$exclude_ids = ! empty( $_GET['exclude'] ) ? array_map( 'absint', (array) wp_unslash( $_GET['exclude'] ) ) : array();

$exclude_types = array();
if ( ! empty( $_GET['exclude_type'] ) ) {
    // Support both comma-delimited and array format inputs.
    $exclude_types = wp_unslash( $_GET['exclude_type'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
    if ( ! is_array( $exclude_types ) ) {
        $exclude_types = explode( ',', $exclude_types );
    }

    // Sanitize the excluded types against valid product types.
    foreach ( $exclude_types as &$exclude_type ) {
        $exclude_type = strtolower( trim( $exclude_type ) );
    }
    $exclude_types = array_intersect(
        array_merge( array( 'variation' ), array_keys( wc_get_product_types() ) ),
        $exclude_types
    );
}

$data_store = WC_Data_Store::load( 'product' );
$ids        = $data_store->search_products( $term, '', (bool) $include_variations, false, $limit, $include_ids, $exclude_ids );


$products = array();


foreach ( $ids as $id ) {
    $product_object = wc_get_product( $id );

    if ( ! wc_products_array_filter_readable( $product_object ) ) {
        continue;
    }

    $formatted_name = $product_object->get_formatted_name();
    $managing_stock = $product_object->managing_stock();

    if ( in_array( $product_object->get_type(), $exclude_types, true ) ) {
        continue;
    }

    if ( $managing_stock && ! empty( $_GET['display_stock'] ) ) { 
        $stock_amount = $product_object->get_stock_quantity();
        /* Translators: %d stock amount */
        $formatted_name .= ' – ' . sprintf( __( 'Stock: %d', 'woocommerce' ), wc_format_stock_quantity_for_display( $stock_amount, $product_object ) );
    }

    $products[ $product_object->get_id() ] = rawurldecode( $formatted_name );
        

}

wp_send_json( apply_filters( 'woocommerce_json_search_found_products', $products ) );

}

在wp_send_json(...)之前如何从$products数组中排除出库产品?

EN

回答 1

Stack Overflow用户

发布于 2021-02-11 07:18:44

之前

代码语言:javascript
运行
复制
$products[ $product_object->get_id() ] = rawurldecode( $formatted_name );

代码

代码语言:javascript
运行
复制
if ('outofstock' === $product_object->get_stock_status()){
            continue;
}

对我很管用。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66145446

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档