{"id":30072,"date":"2026-04-22T08:32:41","date_gmt":"2026-04-22T08:32:41","guid":{"rendered":"https:\/\/makemehappy.sk\/?p=30072"},"modified":"2026-04-22T08:32:42","modified_gmt":"2026-04-22T08:32:42","slug":"woocommerce-zobrazenie-hmotnosti-shortcode","status":"publish","type":"post","link":"https:\/\/makemehappy.sk\/en\/woocommerce-zobrazenie-hmotnosti-shortcode\/","title":{"rendered":"How to display the weight of a product anywhere using a shortcode"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Niekedy potrebujete zobrazi\u0165 v\u00e1hu produktu hne\u010f ved\u013ea ceny, v popise alebo v \u0161peci\u00e1lnom bloku, a nielen v predvolenej tabu\u013eke parametrov. WooCommerce t\u00fato mo\u017enos\u0165 v z\u00e1klade nem\u00e1, ale pomocou tohto jednoduch\u00e9ho snippetu si vytvor\u00edte vlastov\u00fa \u201eskratku\u201c (shortcode), ktor\u00fa vlo\u017e\u00edte kamko\u013evek.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"ako-to-funguje\">Ako to funguje?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Snippet vytvor\u00ed nov\u00fd shortcode <code>[product_weight]<\/code>. Ke\u010f ho vlo\u017e\u00edte do popisu produktu alebo do Elementoru (\u010di in\u00e9ho buildera), k\u00f3d urob\u00ed nasledovn\u00e9:<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li><strong>Identifikuje produkt:<\/strong> Zist\u00ed, na ktorej produktovej str\u00e1nke sa pr\u00e1ve nach\u00e1dzate.<\/li>\n\n\n\n<li><strong>Vytiahne \u00fadaje:<\/strong> Z datab\u00e1zy si vyp\u00fdta \u010d\u00edseln\u00fa hodnotu hmotnosti.<\/li>\n\n\n\n<li><strong>Zist\u00ed jednotky:<\/strong> Pozrie sa do nastaven\u00ed WooCommerce, \u010di pou\u017e\u00edvate kg, g alebo in\u00e9 jednotky.<\/li>\n\n\n\n<li><strong>Zobraz\u00ed v\u00fdsledok:<\/strong> Spoj\u00ed \u010d\u00edslo s jednotkou a \u00fah\u013eadne ich vyp\u00ed\u0161e. Ak hmotnos\u0165 nie je zadan\u00e1, upozorn\u00ed na to (text si m\u00f4\u017eete v k\u00f3de \u013eahko zmeni\u0165).<\/li>\n<\/ol>\n\n\n\n<div class=\"gspb_button_wrapper gspb_button-id-gsbp-f491f10\" id=\"gspb_button-id-gsbp-f491f10\"><a class=\"wp-block-greenshift-blocks-buttonbox gspb-buttonbox wp-element-button\" href=\"https:\/\/makemehappy.sk\/snippety\/\" target=\"_blank\" rel=\"noopener\"><span class=\"gspb-buttonbox-textwrap\"><span class=\"gspb-buttonbox-text\"><span class=\"gspb-buttonbox-title\">Ako prida\u0165 k\u00f3d<\/span><\/span><\/span><\/a><\/div>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7\">PHP<\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>function custom_product_weight_shortcode() {\n    global $product;\n\n    \/\/ Kontrola, \u010di sme skuto\u010dne na str\u00e1nke produktu\n    if ( ! is_a( $product, 'WC_Product' ) ) {\n        return '';\n    }\n\n    $weight = $product->get_weight();\n\n    \/\/ Ak produkt nem\u00e1 zadan\u00fa hmotnos\u0165\n    if ( ! $weight ) {\n        return 'Hmotnos\u0165 tohto produktu nie je dostupn\u00e1.';\n    }\n\n    \/\/ Vr\u00e1ti sform\u00e1tovan\u00fd text s jednotkou z nastaven\u00ed WooCommerce\n    return 'Hmotnos\u0165 tohto produktu je ' . $weight . ' ' . get_option( 'woocommerce_weight_unit' );\n}\n\n\/\/ Registr\u00e1cia shortcodu &#91;product_weight&#93;\nadd_shortcode( 'product_weight', 'custom_product_weight_shortcode' );<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">function<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">custom_product_weight_shortcode<\/span><span style=\"color: #D4D4D4\">() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">global<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">$product<\/span><span style=\"color: #D4D4D4\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\">\/\/ Kontrola, \u010di sme skuto\u010dne na str\u00e1nke produktu<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> ( ! <\/span><span style=\"color: #DCDCAA\">is_a<\/span><span style=\"color: #D4D4D4\">( <\/span><span style=\"color: #9CDCFE\">$product<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #CE9178\">&#39;WC_Product&#39;<\/span><span style=\"color: #D4D4D4\"> ) ) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #CE9178\">&#39;&#39;<\/span><span style=\"color: #D4D4D4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #9CDCFE\">$weight<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #9CDCFE\">$product<\/span><span style=\"color: #D4D4D4\">-&gt;<\/span><span style=\"color: #DCDCAA\">get_weight<\/span><span style=\"color: #D4D4D4\">();<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\">\/\/ Ak produkt nem\u00e1 zadan\u00fa hmotnos\u0165<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> ( ! <\/span><span style=\"color: #9CDCFE\">$weight<\/span><span style=\"color: #D4D4D4\"> ) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #CE9178\">&#39;Hmotnos\u0165 tohto produktu nie je dostupn\u00e1.&#39;<\/span><span style=\"color: #D4D4D4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\">\/\/ Vr\u00e1ti sform\u00e1tovan\u00fd text s jednotkou z nastaven\u00ed WooCommerce<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #CE9178\">&#39;Hmotnos\u0165 tohto produktu je &#39;<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">$weight<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #CE9178\">&#39; &#39;<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">get_option<\/span><span style=\"color: #D4D4D4\">( <\/span><span style=\"color: #CE9178\">&#39;woocommerce_weight_unit&#39;<\/span><span style=\"color: #D4D4D4\"> );<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">\/\/ Registr\u00e1cia shortcodu &#91;product_weight&#93;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #DCDCAA\">add_shortcode<\/span><span style=\"color: #D4D4D4\">( <\/span><span style=\"color: #CE9178\">&#39;product_weight&#39;<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #CE9178\">&#39;custom_product_weight_shortcode&#39;<\/span><span style=\"color: #D4D4D4\"> );<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"prakticke-tipy\">Praktick\u00e9 tipy<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Kam vlo\u017ei\u0165 shortcode:<\/strong> Sta\u010d\u00ed do textov\u00e9ho editora v administr\u00e1cii produktu nap\u00edsa\u0165 <code>[product_weight]<\/code>.<\/li>\n\n\n\n<li><strong>Viacero produktov:<\/strong> Tento shortcode je navrhnut\u00fd tak, aby fungoval v r\u00e1mci &#8222;Product Loop&#8220; alebo na str\u00e1nke konkr\u00e9tneho produktu. Ak ho vlo\u017e\u00edte na be\u017en\u00fa statick\u00fa str\u00e1nku (napr. \u201eO n\u00e1s\u201c), nezobraz\u00ed ni\u010d, preto\u017ee nebude vedie\u0165, ktor\u00fa v\u00e1hu m\u00e1 vytiahnu\u0165.<\/li>\n\n\n\n<li><strong>Vlastn\u00fd text (riadok 17):<\/strong> Ak chcete zmeni\u0165 vetu &#8222;Hmotnos\u0165 tohto produktu je&#8230;&#8220;, sta\u010d\u00ed upravi\u0165 text v poslednom riadku funkcie pred premennou <code>$weight<\/code>.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Niekedy potrebujete zobrazi\u0165 v\u00e1hu produktu hne\u010f ved\u013ea ceny, v popise alebo v \u0161peci\u00e1lnom bloku, a nielen v predvolenej tabu\u013eke parametrov. WooCommerce t\u00fato mo\u017enos\u0165 v z\u00e1klade nem\u00e1, ale pomocou tohto jednoduch\u00e9ho snippetu si vytvor\u00edte vlastov\u00fa \u201eskratku\u201c (shortcode), ktor\u00fa vlo\u017e\u00edte kamko\u013evek. Ako to funguje? Snippet vytvor\u00ed nov\u00fd shortcode [product_weight]. Ke\u010f ho vlo\u017e\u00edte do popisu produktu alebo do [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":29535,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_gspb_post_css":"#gspb_button-id-gsbp-f491f10{display:flex;justify-content:flex-start}#gspb_button-id-gsbp-f491f10 .gspb-buttonbox-text{display:flex;flex-direction:column}#gspb_button-id-gsbp-f491f10>.gspb-buttonbox{box-sizing:border-box}","footnotes":""},"categories":[105],"tags":[],"class_list":["post-30072","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-snippet"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/makemehappy.sk\/en\/wp-json\/wp\/v2\/posts\/30072","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/makemehappy.sk\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/makemehappy.sk\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/makemehappy.sk\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/makemehappy.sk\/en\/wp-json\/wp\/v2\/comments?post=30072"}],"version-history":[{"count":1,"href":"https:\/\/makemehappy.sk\/en\/wp-json\/wp\/v2\/posts\/30072\/revisions"}],"predecessor-version":[{"id":30073,"href":"https:\/\/makemehappy.sk\/en\/wp-json\/wp\/v2\/posts\/30072\/revisions\/30073"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/makemehappy.sk\/en\/wp-json\/wp\/v2\/media\/29535"}],"wp:attachment":[{"href":"https:\/\/makemehappy.sk\/en\/wp-json\/wp\/v2\/media?parent=30072"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/makemehappy.sk\/en\/wp-json\/wp\/v2\/categories?post=30072"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/makemehappy.sk\/en\/wp-json\/wp\/v2\/tags?post=30072"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}