我使用此代码显示基于以下内容的父变量产品的产品变量库存量之和:
Display the stock availability for all product types in Woocommerce archive pages。
如何在上面的脚本而不是_stock
元中选择排序选项?这是我用来对产品进行排序的脚本。
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;
}