מוצרים ללא מלאי למטה
Sat Nov 18 2023 18:10:56 GMT+0000 (Coordinated Universal Time)
Saved by
@Shiri
add_filter('apto/get_order_list', 'custom_wooc_apto_get_order_list', 10, 2);
function custom_wooc_apto_get_order_list( $order_list, $sort_view_id )
{
//not for admin, only for Front Side
if ( is_admin() )
return $order_list;
global $APTO;
//retrieve the sort post
$sort_view_data = get_post($sort_view_id);
$sortID = $sort_view_data->post_parent;
//check if is a woocommerce sort
if ( ! $APTO->functions->is_woocommerce($sortID) )
return $order_list;
//identify the products out of stock
$out_of_stock = array();
foreach ( $order_list as $key => $post_id )
{
if ( get_post_meta($post_id, '_manage_stock', TRUE ) != 'yes' )
continue;
if ( get_post_meta($post_id, '_stock', TRUE) < 1 )
{
$out_of_stock[] = $post_id;
unset( $order_list[$key] );
}
}
//re-index the keys
$order_list = array_values($order_list);
//put the OutOf Stock at the end of list
if ( count ( $out_of_stock ) > 0 )
{
foreach ( $out_of_stock as $post_id )
{
$order_list[] = $post_id;
}
}
return $order_list;
}
The above code works if Manage Stock is turned On. If the shop products does not use that and rely on Stock status the following code should be instead:
add_filter('apto/get_order_list', 'custom_wooc_apto_get_order_list', 10, 2);
function custom_wooc_apto_get_order_list( $order_list, $sort_view_id )
{
//not for admin, only for Front Side
if ( is_admin() )
return $order_list;
global $APTO;
//retrieve the sort post
$sort_view_data = get_post($sort_view_id);
$sortID = $sort_view_data->post_parent;
//check if is a woocommerce sort
if ( ! $APTO->functions->is_woocommerce($sortID) )
return $order_list;
//identify the products out of stock
$out_of_stock = array();
foreach ( $order_list as $key => $post_id )
{
if ( get_post_meta($post_id, '_stock_status', TRUE) != 'instock' )
{
$out_of_stock[] = $post_id;
unset( $order_list[$key] );
}
}
//re-index the keys
$order_list = array_values($order_list);
//put the OutOf Stock at the end of list
if ( count ( $out_of_stock ) > 0 )
{
foreach ( $out_of_stock as $post_id )
{
$order_list[] = $post_id;
}
}
return $order_list;
}
content_copyCOPY
📸 פוסט שכדאי לך להציץ בו בפייסבוק https://www.nsp-code.com/sort-woocommerce-products-while-automatically-put-the-out-of-stock-items-at-the-end-of-list/?fbclid=IwAR28yRziwN_eCNXr7F9YkF5PbveBO-huZxjYLVpfEWmGJhpn3JFM7aa4WxI_aem_ASsDaPQHUB59VlTlU46gE4usmelEWE4pMuhklx03CgmAfRsSGXjqzVucao8p6rLWJAY
Comments