{"id":6528,"date":"2021-11-28T10:39:00","date_gmt":"2021-11-28T10:39:00","guid":{"rendered":"https:\/\/musilda.cz\/?p=6528"},"modified":"2021-11-28T10:39:00","modified_gmt":"2021-11-28T10:39:00","slug":"nastaveni-vlastniho-typu-produktu-v-administraci","status":"publish","type":"post","link":"https:\/\/affinite.io\/cs\/nastaveni-vlastniho-typu-produktu-v-administraci\/","title":{"rendered":"Nastaven\u00ed vlastn\u00edho typu produktu v administraci"},"content":{"rendered":"\n<p>Pro plugin kter\u00fd jsem pracovn\u011b pojmenovat EventPress budeme pot\u0159ebovat p\u0159idat n\u011bjak\u00e9 nastaven\u00ed, kter\u00e9 n\u00e1m umo\u017en\u00ed p\u0159idat vlastn\u00ed funkce. <\/p>\n\n\n\n<p>Jako z\u00e1klad si p\u0159iprav\u00edme registraci na online webin\u00e1\u0159 a nebudeme d\u011blat nic slo\u017eit\u00e9ho, tak\u017ee p\u0159id\u00e1me n\u00e1sleduj\u00edc\u00ed pole:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>maxim\u00e1ln\u00ed po\u010det \u00fa\u010dastn\u00edk\u016f<\/li><li>minim\u00e1ln\u00ed po\u010det \u00fa\u010dastn\u00edk\u016f (minimum pro po\u0159\u00e1d\u00e1n\u00ed)<\/li><li>datum a \u010das kon\u00e1n\u00ed <\/li><\/ul>\n\n\n\n<p>Nic jin\u00e9ho pot\u0159ebovat nebudeme<\/p>\n\n\n\n<p>Do funkce musilda_eventpress_product_option si p\u0159id\u00e1me n\u00e1sleduj\u00edc\u00ed k\u00f3d:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>global $post;\n$min_participants = get_post_meta( $post-&gt;ID, '_min_participants', true );\nif ( empty( $min_participants ) ) {\n\t$min_participants = 1;\n}\n$max_participants = get_post_meta( $post-&gt;ID, '_max_participants', true );\nif ( empty( $max_participants ) ) {\n\t$max_participants = 1;\n}\n\nwoocommerce_wp_text_input(\n\tarray(\n\t\t'id'                =&gt; '_min_participants',\n\t\t'value'             =&gt; $min_participants,\n\t\t'label'             =&gt; __( 'Minimal participants', 'musilda-eventpress' ),\n\t\t'placeholder'       =&gt; __( '1', 'musilda-eventpress' ),\n\t\t'description'       =&gt; __( 'Add minimum participants for event', 'musilda-eventpress' ),\n\t\t'type'              =&gt; 'number',\n\t\t'custom_attributes' =&gt; array(\n\t\t\t'step' =&gt; '1',\n\t\t\t'min'  =&gt; '1',\n\t\t),\n\t)\n);\nwoocommerce_wp_text_input(\n\tarray(\n\t\t'id'                =&gt; '_max_participants',\n\t\t'value'             =&gt; $max_participants,\n\t\t'label'             =&gt; __( 'Maximal participants', 'musilda-eventpress' ),\n\t\t'placeholder'       =&gt; __( '1', 'musilda-eventpress' ),\n\t\t'description'       =&gt; __( 'Add maximal participants for event', 'musilda-eventpress' ),\n\t\t'type'              =&gt; 'number',\n\t\t'custom_attributes' =&gt; array(\n\t\t\t'step' =&gt; '1',\n\t\t\t'min'  =&gt; '1',\n\t\t),\n\t)\n);<\/code><\/pre>\n\n\n\n<p>V\u00fdsledek:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2021\/11\/event-data.png\"><img loading=\"lazy\" decoding=\"async\" width=\"704\" height=\"247\" src=\"https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2021\/11\/event-data.png\" alt=\"\" class=\"wp-image-6529\" srcset=\"https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2021\/11\/event-data.png 704w, https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2021\/11\/event-data-300x105.png 300w\" sizes=\"auto, (max-width: 704px) 100vw, 704px\" \/><\/a><\/figure>\n\n\n\n<p>To bylo to jednodu\u0161\u0161\u00ed, proto\u017ee jsme vyu\u017eili funkci  woocommerce_wp_text_input a vygenerovali si pol\u00ed\u010dka. <\/p>\n\n\n\n<p>Te\u010f ale mus\u00edme p\u0159idat pole pro p\u0159id\u00e1n\u00ed v\u00fdb\u011bru data a \u010dasu kon\u00e1n\u00ed. WordPress sice v sob\u011b m\u00e1 jQuery a tak lze pou\u017e\u00edt jQuery modul dat pickup, ale j\u00e1 se posledn\u00ed dobou jQuery sna\u017e\u00edm vyh\u00fdbat, tak\u017ee pou\u017eiji <a href=\"https:\/\/bulma-calendar.onrender.com\/\" target=\"_blank\" rel=\"noopener\">https:\/\/bulma-calendar.onrender.com\/<\/a><\/p>\n\n\n\n<p>Abych mohl knihovnu pou\u017e\u00edt, mus\u00edm nejprve soubory ulo\u017eit v pluginu a na\u010d\u00edst pomoc\u00ed enqueue:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n * Add admin assets\n *\n *\/\nadd_action( 'admin_enqueue_scripts', 'eventpress_enqueue_admin_styles' );\nfunction eventpress_enqueue_admin_styles() {\n\t$current_screen = get_current_screen();\n\tif ( 'product' == $current_screen-&gt;id ) {\n\t\twp_enqueue_style( 'bulma-calendar', plugins_url( 'assets\/admin\/css\/bulma-calendar.min.css', __FILE__ ), array(), '1.0' );\n\t}\n}\nadd_action( 'admin_enqueue_scripts', 'eventpress_enqueue_admin_scripts' );\nfunction eventpress_enqueue_admin_scripts() {\n\t$current_screen = get_current_screen();\n\tif ( 'product' == $current_screen-&gt;id ) {\n\t\twp_enqueue_script( 'bulma-calendar', plugins_url( 'assets\/admin\/js\/bulma-calendar.min.js', __FILE__ ), array(), '1.0', true );\n\n                wp_enqueue_script( 'event-admin', plugins_url( 'assets\/admin\/js\/main.js', __FILE__ ), array(), '1.0', true );\n\t}\n}<\/code><\/pre>\n\n\n\n<p>Do funkce  musilda_eventpress_product_option   si za max participants input vlo\u017e\u00edme n\u00e1sleduj\u00edc\u00ed k\u00f3d pro input date pickeru:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$event_start_date_time = get_post_meta( $post-&gt;ID, '_event_start_date_time', true );\nif ( empty( $event_start_date_time ) ) {\n\t$event_start_date_time = '';\n}\n\necho '&lt;p class=\"form-field event_start_date_time\"&gt;\n\t&lt;label for=\"event_start_date_time\"&gt;' . esc_html__( 'Event start date time', 'musilda-eventpress' ) . '&lt;\/label&gt;\n\t&lt;input type=\"date\" class=\"short\" name=\"_event_start_date_time\" id=\"event_start_date_time\" value=\"' . esc_attr( $event_start_date_time ) . '\"  maxlength=\"10\" \/&gt;\n&lt;\/p&gt;';<\/code><\/pre>\n\n\n\n<p>Jako posledn\u00ed mus\u00edme do souboru main.js, kter\u00fd vlo\u017e\u00edme do slo\u017eky assets\/admin\/js, p\u0159id\u00e1me k\u00f3d:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var eventStartDatTime = document.querySelector('#event_start_date_time');\nif ( eventStartDatTime ) {\n\tbulmaCalendar.attach( eventStartDatTime, {\n\t\ttype: 'datetime',\n\t\tdateFormat: 'dd.MM.yyyy',\n\t\tweekStart: 1\n\t});\n}<\/code><\/pre>\n\n\n\n<p>Tento k\u00f3d bude inicializovat datepicker, kter\u00fd bude vyvol\u00e1n pomoc\u00ed soubor\u016f bulma calendar, kter\u00e9 um\u00edst\u00edme do slo\u017eek assets\/admin\/js a assets\/admin\/css. V\u0161echny soubory najdete v commitu, kter\u00fd je odk\u00e1z\u00e1n na konci \u010dl\u00e1nku.<\/p>\n\n\n\n<p>Pokud jste ud\u011blali v\u0161e, jak jste m\u011bli, v\u00fdsledek bude vypadat takto:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2021\/11\/event-data-picker.png\"><img loading=\"lazy\" decoding=\"async\" width=\"705\" height=\"293\" src=\"https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2021\/11\/event-data-picker.png\" alt=\"\" class=\"wp-image-6532\" srcset=\"https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2021\/11\/event-data-picker.png 705w, https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2021\/11\/event-data-picker-300x125.png 300w\" sizes=\"auto, (max-width: 705px) 100vw, 705px\" \/><\/a><\/figure>\n\n\n\n<p>A samotn\u00fd v\u00fdb\u011br data a \u010dasu:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2021\/11\/date-picker.png\"><img loading=\"lazy\" decoding=\"async\" width=\"349\" height=\"602\" src=\"https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2021\/11\/date-picker.png\" alt=\"\" class=\"wp-image-6533\" srcset=\"https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2021\/11\/date-picker.png 349w, https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2021\/11\/date-picker-174x300.png 174w\" sizes=\"auto, (max-width: 349px) 100vw, 349px\" \/><\/a><\/figure>\n\n\n\n<p>T\u00edm jsme se dostali t\u00e9m\u011b\u0159 na konec dne\u0161n\u00ed \u010d\u00e1sti, zb\u00fdv\u00e1 n\u00e1m ji\u017e jen ulo\u017eit data, kter\u00e1 vypln\u00edme:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>add_action( 'woocommerce_process_product_meta', 'musilda_product_fields_save' );\nfunction musilda_product_fields_save( $post_id ){\n\n\t\/\/Check autosave\n\tif ( defined('DOING_AUTOSAVE') &amp;&amp; DOING_AUTOSAVE ) {\n\t\treturn $post_id;\n\t}\n\n\t\/\/Don't update on Quick Edit\n\tif ( defined('DOING_AJAX') ) {\n\t\treturn $post_id;\n\t}\n\n\t\/\/Don't update on Quick Edit\n\tif ( !empty( $_REQUEST&#091;'bulk_edit'] ) ) {\n\t\treturn $post_id;\n\t}\n\n\tif ( !empty( $_POST&#091;'_min_participants'] ) ) {\n\t\t$_min_participants = sanitize_text_field( $_POST&#091;'_min_participants'] );\n\t\tupdate_post_meta( $post_id, '_min_participants', $_min_participants );\n\t} else {\n\t\tdelete_post_meta( $post_id, '_min_participants' );\n\t}\n\tif ( !empty( $_POST&#091;'_max_participants'] ) ) {\n\t\t$_max_participants = sanitize_text_field( $_POST&#091;'_max_participants'] );\n\t\tupdate_post_meta( $post_id, '_max_participants', $_max_participants );\n\t} else {\n\t\tdelete_post_meta( $post_id, '_max_participants' );\n\t}\n\tif ( !empty( $_POST&#091;'_event_start_date_time'] ) ) {\n\t\t$_event_start_date_time = sanitize_text_field( $_POST&#091;'_event_start_date_time'] );\n\t\tupdate_post_meta( $post_id, '_event_start_date_time', $_event_start_date_time );\n\t} else {\n\t\tdelete_post_meta( $post_id, '_event_start_date_time' );\n\t}\n\n}<\/code><\/pre>\n\n\n\n<p>V tuto chv\u00edli m\u00e1me p\u0159ipravenou \u010d\u00e1st v administraci a v dal\u0161\u00edm \u010dl\u00e1nku zobraz\u00edme ulo\u017een\u00e1 data na frontendu. <\/p>\n\n\n\n<p>Aktu\u00e1ln\u00ed commit najdete <a href=\"https:\/\/github.com\/Musilda\/musilda-eventpress\/commit\/fbc83948d02fd16a0029ab1843f32ff00ffe8247\" target=\"_blank\" rel=\"noopener\">zde<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Pro plugin kter\u00fd jsem pracovn\u011b pojmenovat EventPress budeme pot\u0159ebovat p\u0159idat n\u011bjak\u00e9 nastaven\u00ed, kter\u00e9 n\u00e1m umo\u017en\u00ed p\u0159idat vlastn\u00ed funkce. Jako z\u00e1klad si p\u0159iprav\u00edme registraci na online webin\u00e1\u0159 a nebudeme d\u011blat nic slo\u017eit\u00e9ho, tak\u017ee p\u0159id\u00e1me n\u00e1sleduj\u00edc\u00ed pole: maxim\u00e1ln\u00ed po\u010det \u00fa\u010dastn\u00edk\u016f minim\u00e1ln\u00ed po\u010det \u00fa\u010dastn\u00edk\u016f (minimum pro po\u0159\u00e1d\u00e1n\u00ed) datum a \u010das kon\u00e1n\u00ed Nic jin\u00e9ho pot\u0159ebovat nebudeme Do funkce musilda_eventpress_product_option<\/p>\n","protected":false},"author":1,"featured_media":5600,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_uag_custom_page_level_css":"","footnotes":""},"categories":[29],"tags":[],"class_list":["post-6528","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-woocommerce-snippety"],"acf":[],"uagb_featured_image_src":{"full":["https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2020\/05\/snippet.jpg",1200,765,false],"thumbnail":["https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2020\/05\/snippet-150x150.jpg",150,150,true],"medium":["https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2020\/05\/snippet-300x191.jpg",300,191,true],"medium_large":["https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2020\/05\/snippet-768x490.jpg",640,408,true],"large":["https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2020\/05\/snippet-1024x653.jpg",640,408,true],"1536x1536":["https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2020\/05\/snippet.jpg",1200,765,false],"2048x2048":["https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2020\/05\/snippet.jpg",1200,765,false],"archive-list":["https:\/\/affinite.io\/cs\/wp-content\/uploads\/sites\/2\/2020\/05\/snippet-400x265.jpg",400,265,true]},"uagb_author_info":{"display_name":"Affinite","author_link":"https:\/\/affinite.io\/cs\/author\/affinite\/"},"uagb_comment_info":0,"uagb_excerpt":"Pro plugin kter\u00fd jsem pracovn\u011b pojmenovat EventPress budeme pot\u0159ebovat p\u0159idat n\u011bjak\u00e9 nastaven\u00ed, kter\u00e9 n\u00e1m umo\u017en\u00ed p\u0159idat vlastn\u00ed funkce. Jako z\u00e1klad si p\u0159iprav\u00edme registraci na online webin\u00e1\u0159 a nebudeme d\u011blat nic slo\u017eit\u00e9ho, tak\u017ee p\u0159id\u00e1me n\u00e1sleduj\u00edc\u00ed pole: maxim\u00e1ln\u00ed po\u010det \u00fa\u010dastn\u00edk\u016f minim\u00e1ln\u00ed po\u010det \u00fa\u010dastn\u00edk\u016f (minimum pro po\u0159\u00e1d\u00e1n\u00ed) datum a \u010das kon\u00e1n\u00ed Nic jin\u00e9ho pot\u0159ebovat nebudeme Do funkce musilda_eventpress_product_option","_links":{"self":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/posts\/6528","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/comments?post=6528"}],"version-history":[{"count":0,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/posts\/6528\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/media\/5600"}],"wp:attachment":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/media?parent=6528"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/categories?post=6528"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/tags?post=6528"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}