{"id":6296,"date":"2021-10-07T12:20:12","date_gmt":"2021-10-07T12:20:12","guid":{"rendered":"https:\/\/musilda.cz\/?p=6296"},"modified":"2021-10-07T12:20:12","modified_gmt":"2021-10-07T12:20:12","slug":"jak-pridat-vlastni-text-field-v-nastaveni-produktu","status":"publish","type":"post","link":"https:\/\/affinite.io\/cs\/jak-pridat-vlastni-text-field-v-nastaveni-produktu\/","title":{"rendered":"Jak p\u0159idat vlastn\u00ed text field v nastaven\u00ed produktu"},"content":{"rendered":"\n

Zobrazen\u00ed n\u011bjak\u00e9 hodnoty v detailu produktu je pouze polovina pr\u00e1ce, kterou mus\u00edme ud\u011blat, kdy\u017e upravujeme e-shop. <\/p>\n\n\n\n

Tou druhou je m\u00edt mo\u017enost tuto hodnotu natavit u produktu v administraci a ulo\u017eit.<\/p>\n\n\n\n

Dne\u0161n\u00ed snippet bude tedy v podstat\u011b roz\u0161\u00ed\u0159en\u00edm https:\/\/musilda.cz\/jak-pridat-vlastni-tab-do-detailu-produktu\/<\/a><\/p>\n\n\n\n

V n\u011bm jsme do detailu produktu p\u0159idali novou z\u00e1lo\u017eku Produkt video.<\/p>\n\n\n\n

\"\"<\/a><\/figure>\n\n\n\n

V detailu produktu p\u0159id\u00e1me formul\u00e1\u0159ov\u00e9 pole v z\u00e1lo\u017ece pokro\u010dil\u00e9, a\u0165 m\u016f\u017eeme vlo\u017eit \u0159et\u011bzec pro na\u010dten\u00ed videa.<\/p>\n\n\n\n

\"\"<\/a><\/figure>\n\n\n\n

Dne\u0161n\u00ed snippet m\u00e1 dv\u011b funkce, prvn\u00ed je ulo\u017een\u00ed hodnoty z formul\u00e1\u0159e do post meta a ta druh\u00e1 vykresl\u00ed textov\u00e9 pole formul\u00e1\u0159e v detailu produktu.<\/p>\n\n\n\n

if ( is_admin() ) {\n\tadd_action( 'woocommerce_process_product_meta', 'musilda_product_fields_save' );\n\n\tfunction musilda_product_fields_save( $post_id ){\n\n\t\t\/\/Check autosave\n\t\tif ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {\n\t\t\treturn $post_id;\n\t\t}\n\n\t\t\/\/Don't update on Quick Edit\n\t\tif ( defined('DOING_AJAX') ) {\n\t\t\treturn $post_id;\n\t\t}\n\n\t\t\/\/Don't update on Quick Edit\n\t\tif ( !empty( $_REQUEST['bulk_edit'] ) ) {\n\t\t\treturn $post_id;\n\t\t}\n\n\t\tif ( !empty( $_POST['product-video'] ) ) {\n\t\t\t$video = sanitize_text_field( $_POST['product-video'] );\n\t\t\tupdate_post_meta( $post_id, 'product-video', $video );\n\t\t} else {\n\t\t\tdelete_post_meta( $post_id, 'product-video' );\n\t\t}\n\n\t}\n\n\tadd_action( 'woocommerce_product_options_advanced', 'woocommerce_product_options_advanced_video_field' );\n\tfunction woocommerce_product_options_advanced_video_field() {\n\t\tglobal $post;\n\t\t$video = get_post_meta( $post->ID, 'product-video', true );\n\t\tif ( empty( $video ) ) {\n\t\t\t$video = '';\n\t\t}\n\n\t\twoocommerce_wp_text_input(\n\t\t\tarray(\n\t\t\t\t'id'                => 'product-video',\n\t\t\t\t'value'             => $video,\n\t\t\t\t'label'             => __( 'Product video', 'woocommerce' ),\n\t\t\t\t'desc_tip'          => true,\n\t\t\t\t'description'       => __( 'Insert youtube video', 'woocommerce' )\n\t\t\t)\n\t\t);\n\n\t}\n\n}<\/code><\/pre>\n\n\n\n

V\u00fdsledkem je pole v z\u00e1lo\u017ece pokro\u010dil\u00e9:<\/p>\n\n\n\n

\"\"<\/a><\/figure>\n\n\n\n

A nakonec si je\u0161t\u011b uprav\u00edme v\u010derej\u0161\u00ed k\u00f3d:<\/p>\n\n\n\n

function display_video_tab_content() {\n\n\tglobal $post;\n\t$video = get_post_meta( $post->ID, 'product-video', true );\n\n\tif ( !empty( $video ) ) {\n\t\techo '<iframe width=\"560\" height=\"315\" src=\"' . $video . '\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n\t\t\t';\n\t}\n\n}<\/code><\/pre>\n\n\n\n

K dokonalosti u\u017e jen zb\u00fdv\u00e1 omezit zobrazen\u00ed z\u00e1lo\u017eky, pouze, pokud je product video vlo\u017eeno:<\/p>\n\n\n\n

add_filter( 'woocommerce_product_tabs', 'custom_product_tabs' );\nfunction custom_product_tabs( $product_tabs ) {\n\n\tglobal $post;\n\t$video = get_post_meta( $post->ID, 'product-video', true );\n\tif ( !empty( $video ) ) {\n\t\t$product_tabs['product-video'] = array(\n\t\t\t'title' => __( 'Product video', 'musilda' ),\n\t\t\t'callback' => 'display_video_tab_content'\n\t\t);\n\t}\n\n\treturn $product_tabs;\n\n}<\/code><\/pre>\n\n\n\n

Dal\u0161\u00ed u\u017eite\u010dn\u00e9 snippety najdete na str\u00e1nce U\u017eite\u010dn\u00e9 snippety pro WooCommerce<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"

Zobrazen\u00ed n\u011bjak\u00e9 hodnoty v detailu produktu je pouze polovina pr\u00e1ce, kterou mus\u00edme ud\u011blat, kdy\u017e upravujeme e-shop. Tou druhou je m\u00edt mo\u017enost tuto hodnotu natavit u produktu v administraci a ulo\u017eit. Dne\u0161n\u00ed snippet bude tedy v podstat\u011b roz\u0161\u00ed\u0159en\u00edm https:\/\/musilda.cz\/jak-pridat-vlastni-tab-do-detailu-produktu\/ V n\u011bm jsme do detailu produktu p\u0159idali novou z\u00e1lo\u017eku Produkt video. V detailu produktu p\u0159id\u00e1me formul\u00e1\u0159ov\u00e9 pole<\/p>\n","protected":false},"author":1,"featured_media":5600,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[],"class_list":["post-6296","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-woocommerce-snippety"],"_links":{"self":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/posts\/6296"}],"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=6296"}],"version-history":[{"count":0,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/posts\/6296\/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=6296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/categories?post=6296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/tags?post=6296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}