duansha7453 2018-11-17 09:55
浏览 57

按可用性对可变产品进行排序

Im using this code in displaying the sum of the product variable stock quantities from a parent variable products based on:
Display the stock availability for all product types in Woocommerce archive pages.

How can I make the sorting options to look at in the above script rather than the _stock meta? here is the script that i am using to sort the products.

protected function get_settings_options() {

    $options = array(
        'by_stock'      => __( 'Available Stock', 'woocommerce-extra-product-sorting-options' ),
    );

    if ( ! self::is_wc_gte( '3.0' ) ) {
        $options['featured_first'] = __( 'Featured First', 'woocommerce-extra-product-sorting-options' );
    }

    return $options;
}
foreach( $new_sorting_options as $option ) {

        switch ( $option ) {

            case 'by_stock':
                $sortby['by_stock']       = __( 'Sort by availability', 'woocommerce-extra-product-sorting-options' );
            break;
        }
    }

    return $sortby;
}


public function add_new_shop_ordering_args( $sort_args ) {

    // If we have the orderby via URL, let's pass it in.
    // This means we're on a shop / archive, so if we don't have it, use the default.
    if ( isset( $_GET['orderby'] ) ) {
        $orderby_value = wc_clean( $_GET['orderby'] );
    } else {
        $orderby_value = apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
    }

    // Since a shortcode can be used on a non-WC page, we won't have $_GET['orderby'] --
    // grab it from the passed in sorting args instead for non-WC pages.
    // Don't use this on WC archives so we don't break the default option
    if ( ! is_post_type_archive( 'product' ) && ! is_shop() && isset( $sort_args['orderby'] ) ) {
        $orderby_value = $sort_args['orderby'];
    }

    $fallback       = apply_filters( 'wc_extra_sorting_options_fallback', 'title', $orderby_value );
    $fallback_order = apply_filters( 'wc_extra_sorting_options_fallback_order', 'ASC', $orderby_value );

    switch( $orderby_value ) {



        case 'by_stock':

            $sort_args['orderby']  = array( 'meta_value_num' => 'DESC', $fallback => $fallback_order );
            $sort_args['meta_key'] = '_stock';

        break;

    }

    return $sort_args;
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥15 可见光定位matlab仿真
    • ¥15 arduino 四自由度机械臂
    • ¥15 wordpress 产品图片 GIF 没法显示