{"id":1968,"date":"2014-08-31T09:28:38","date_gmt":"2014-08-31T09:28:38","guid":{"rendered":"http:\/\/musilda.cz\/?p=1968"},"modified":"2014-08-31T09:28:38","modified_gmt":"2014-08-31T09:28:38","slug":"fitrovani-objednavek-ve-woocommerce-dle-casoveho-obdobi","status":"publish","type":"post","link":"https:\/\/affinite.io\/cs\/fitrovani-objednavek-ve-woocommerce-dle-casoveho-obdobi\/","title":{"rendered":"Fitrov\u00e1n\u00ed objedn\u00e1vek ve Woocommerce dle \u010dasov\u00e9ho obdob\u00ed"},"content":{"rendered":"
Pokud provozujete Woocommerce eshop, jist\u011b v\u00edte, \u017ee v administraci objedn\u00e1vek, m\u00e1te mo\u017enost vypsat v\u0161echny za ur\u010dit\u00fd m\u011bs\u00edc. Dnes v\u00e1m uk\u00e1\u017ei, jak upravit filtraci, pro vybran\u00e9 \u010dasov\u00e9 obdob\u00ed.<\/p>\n
V\u011b\u0159\u00edm tomu, \u017ee p\u0159i v\u011bt\u0161\u00edm mno\u017estv\u00ed objedn\u00e1vek, je proch\u00e1zen\u00ed seznamu za m\u011bs\u00edc velmi frustruj\u00edc\u00ed. Proto jsem upravil fitrov\u00e1n\u00ed objedn\u00e1vek tak, aby jste si mohli vybrat datum od a do kter\u00e9ho chcete objedn\u00e1vky vypsat. Vzhledem k tomu, \u017ee by v\u0161e m\u011blo b\u00fdt co nejv\u00edce u\u017eivatelsk\u00e9 p\u0159\u00edv\u011btiv\u00e9, pou\u017eijeme datapicker, co\u017e je jQuery kalend\u00e1\u0159, pro v\u00fdb\u011br data. V\u00fdborn\u00e9 je, \u017ee WordPress ji\u017e datapocker obsahuje, tak\u017ee se nemus\u00edme starat o vkl\u00e1d\u00e1n\u00ed jQuery scriptu od t\u0159et\u00ed strany.<\/p>\n
Nejprve za\u010dneme kontrolou, zda se jedn\u00e1 administraci, a\u0165 zbyte\u010dn\u011b nena\u010d\u00edt\u00e1me funkce i na frontendu.<\/p>\n
if(is_admin()){\n\n}\n\n<\/pre>\nMezi slo\u017een\u00e9 z\u00e1vorky budeme ps\u00e1t cel\u00fd k\u00f3d.<\/p>\n
P\u0159iprav\u00edme si funkci, kter\u00e1 n\u00e1m z vybran\u00fdch dat uprav\u00ed query dotaz.<\/p>\n
function musilda_filter( $where = '' ) {\nglobal $wpdb;\nif( isset( $_REQUEST['start_date'] ) && !empty( $_REQUEST['start_date'] ) ){\n\n$start_date=$_REQUEST['start_date'];\n$where .= \" AND post_date >='$start_date' \";\n\n}\n\nif( isset( $_REQUEST['end_date'] ) && !empty( $_REQUEST['end_date'] ) ){\n\n$end_date=$_REQUEST['end_date'];\n$where .=\" AND post_date <='$end_date' \";\n\n}\n\nreturn $where;\n}\n\n<\/pre>\nDal\u0161\u00edm krokem bude p\u0159id\u00e1n\u00ed where do query. K tomu pou\u017eijeme pre_get_posts a post_where:<\/p>\n
add_filter('pre_get_posts', 'wp_musilda_admin_filters');\n\nfunction wp_musilda_admin_filters($query) {\n\nglobal $pagenow;\n\nif( $query->is_admin && ( 'edit.php' == $pagenow ) ) {\n\nif(isset($_REQUEST['start_date'])||isset($_REQUEST['end_date'])){\n\nadd_filter( 'posts_where', 'musilda_filter' );\n\n}\n\n}\n\nreturn $query;\n\n}\n\n<\/pre>\n <\/p>\n
Proto\u017ee budeme cht\u00edt pou\u017e\u00edt jQuery datepicker, mus\u00edme p\u0159idat script:<\/p>\n
add_action( 'admin_enqueue_scripts', 'enqueue_admin_scripts' );\nfunction enqueue_admin_scripts() {\nglobal $post;\nif ( !empty( $post->post_type )\n&& $post->post_type == 'shop_order' ){\nwp_enqueue_script( 'jquery-ui-datepicker' );\n}\n}\n\n<\/pre>\nKontrola toho, zda se jedn\u00e1 o shop_order je v tomto p\u0159\u00edpad\u011b na m\u00edst\u011b, proto\u017ee by jsme m\u011bli vkl\u00e1dat scripty, jen kdy\u017e je pot\u0159ebujeme.<\/p>\n
Jako posledn\u00ed zobraz\u00edme pole pro v\u00fdb\u011br datum\u016f a funk\u010dnost m\u00e1me hotovou<\/p>\n
add_action('restrict_manage_posts', 'wp_musilda_filters_dropdowns',2);\nfunction wp_musilda_filters_dropdowns() {\n\nglobal $post;\nif ( !empty( $post->post_type )\n&& $post->post_type == 'shop_order' ){\nglobal $wpdb;\n\nif( isset( $_REQUEST['start_date'] )\n&& isset( $_REQUEST['end_date'] ) ){\n\n$start_date = $_REQUEST['start_date'];\n$end_date = $_REQUEST['end_date'];\n\n}else{\n\n$start_date = '';\n$end_date = '';\n\n}\nglobal $post;\n\n?>;\n<input type="text" name="start_date" class="datepick" value="\" placeholder=\"Start date\">\n<input type="text" name="end_date" class="datepick" value="\" placeholder=\"End date\">\n\n\njQuery(document).ready(function(){\njQuery('.datepick').datepicker({\ndateFormat: 'yy-mm-dd'\n});\n});\n\n<!--?php\nreturn;\n}\n\n}\n\n\n\n \n\nFiltrov\u00e1n\u00ed p\u0159ed:\n\n
<\/a>\n\nFiltrov\u00e1n\u00ed po:\n\n
<\/a>\n\n \n\nPokud budete cht\u00edt pou\u017e\u00edt jin\u00fd datepicker, sta\u010d\u00ed zm\u011bnit pou\u017eit\u00fd script. V p\u0159\u00edpad\u011b, \u017ee budete cht\u00edt stejnou filtraci i u ostatn\u00edch druh\u016f obsahu, sta\u010d\u00ed zru\u0161it kontrolu post_type.\n<\/pre>\n\n\n