{"id":5890,"date":"2022-05-07T14:07:04","date_gmt":"2022-05-07T18:07:04","guid":{"rendered":"https:\/\/themespress.ca\/?p=5890"},"modified":"2022-05-11T06:09:01","modified_gmt":"2022-05-11T10:09:01","slug":"adding-metadata-latest-update-woocommerce-product","status":"publish","type":"post","link":"https:\/\/themespress.ca\/en\/adding-metadata-latest-update-woocommerce-product\/","title":{"rendered":"Adding Metadata (latest update) &#8211; WooCommerce Product"},"content":{"rendered":"<div class=\"wpb-content-wrapper\"><div class=\"vc_row wpb_row vc_row-fluid\"><div class=\"wpb_column vc_column_container vc_col-sm-12\"><div class=\"vc_column-inner\"><div class=\"wpb_wrapper\">\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>With this tutorial, we will create a new WordPress metabox for our downloadable WooCommerce products.<\/p>\n<p>Our data would allow us to diplay the date <span class=\"corrected-phrase ng-star-inserted\" data-end=\"218\" data-originaltext=\"que \" data-start=\"215\" data-text=\"o\u00f9\"><span class=\"corrected-phrase__displayed-text corrected-phrase__displayed-text_bubbled\" data-group=\"AutoCorrected\" data-suggestions=\"1\" data-type=\"Grammar\">where<\/span><\/span> the number of days where the updates were done.<\/p>\n<p>So let&#8217;s say you&#8217;re selling downloadable products. You want your customers to see the last date <span class=\"phrase-bubble__suggestion-label ng-tns-c88-3 phrase-bubble__suggestion-label_active phrase-bubble__suggestion-label_only-one ng-star-inserted\"><span class=\"ng-tns-c88-3 ng-star-inserted\">when<\/span><\/span> the product was updated.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<h2 style=\"text-align: left\" class=\"vc_custom_heading vc_do_custom_heading\" >Creating a class for a WordPress metabox<\/h2>\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>As you&#8217;ll notice, our metabox has a date field by default.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<div id=\"ts-enlighterjs-container-4411915\" class=\"ts-enlighterjs-container-single-enabled  \" style=\"width: 100%;  margin-top: 0px; margin-bottom: 0px;\" data-enlighter-doubleclick=\"true\" data-enlighter-windowbutton=\"true\" data-enlighter-windowtext=\"New Window\" data-enlighter-rawbutton=\"true\" data-enlighter-rawtext=\"RAW Code\" data-enlighter-infobutton=\"false\" data-enlighter-infotext=\"EnlighterJS\" data-enlighter-indent=\"2\"><pre id=\"ts-enlighterjs-pre-4411915\" class=\"\" style=\"white-space: pre-wrap; height: 100%; margin: 0; padding: 0;\" data-enlighter-language=\"php\" data-enlighter-theme=\"enlighter\" data-enlighter-group=\"\" data-enlighter-title=\"\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"1\" data-enlighter-highlight=\"\">\/\/ Meta Box Class: Woocommerce Product Update\nclass woocommerceproductupMetabox {\n\n\tprivate $screen = array(\n\t\t&#039;product&#039;,\n\t);\n\n\tprivate $meta_fields = array(\n\t\tarray(\n\t\t\t&#039;label&#039; =&gt; &#039;Recent updates for this product ?&#039;,\n\t\t\t&#039;id&#039; =&gt; &#039;woocommerce-update-product&#039;,\n\t\t\t&#039;default&#039; =&gt; &#039;Aujourdhui&#039;,\n\t\t\t&#039;type&#039; =&gt; &#039;date&#039;,\n\t\t),\n\t);\n\n\tpublic function __construct() {\n\t\tadd_action( &#039;add_meta_boxes&#039;, array( $this, &#039;add_meta_boxes&#039; ) );\n\t\tadd_action( &#039;save_post&#039;, array( $this, &#039;save_fields&#039; ) );\n\t}\n\n\tpublic function add_meta_boxes() {\n\t\tforeach ( $this-&gt;screen as $single_screen ) {\n\t\t\tadd_meta_box(\n\t\t\t\t&#039;woocommerceproductup&#039;,\n\t\t\t\t__( &#039;Woocommerce Product Update&#039;, &#039;textdomain&#039; ),\n\t\t\t\tarray( $this, &#039;meta_box_callback&#039; ),\n\t\t\t\t$single_screen,\n\t\t\t\t&#039;normal&#039;,\n\t\t\t\t&#039;high&#039;\n\t\t\t);\n\t\t}\n\t}\n\n\tpublic function meta_box_callback( $post ) {\n\t\twp_nonce_field( &#039;woocommerceproductup_data&#039;, &#039;woocommerceproductup_nonce&#039; );\n\t\t$this-&gt;field_generator( $post );\n\t}\n\n\tpublic function field_generator( $post ) {\n\t\t$output = &#039;&#039;;\n\t\tforeach ( $this-&gt;meta_fields as $meta_field ) {\n\t\t\t$label = &#039;&lt;label for=&quot;&#039; . $meta_field[&#039;id&#039;] . &#039;&quot;&gt;&#039; . $meta_field[&#039;label&#039;] . &#039;&lt;\/label&gt;&#039;;\n\t\t\t$meta_value = get_post_meta( $post-&gt;ID, $meta_field[&#039;id&#039;], true );\n\t\t\tif ( empty( $meta_value ) ) {\n\t\t\t\tif ( isset( $meta_field[&#039;default&#039;] ) ) {\n\t\t\t\t\t$meta_value = $meta_field[&#039;default&#039;];\n\t\t\t\t}\n\t\t\t}\n\t\t\tswitch ( $meta_field[&#039;type&#039;] ) {\n\t\t\t\tdefault:\n\t\t\t\t\t$input = sprintf(\n\t\t\t\t\t\t&#039;&lt;input %s id=&quot;%s&quot; name=&quot;%s&quot; type=&quot;%s&quot; value=&quot;%s&quot;&gt;&#039;,\n\t\t\t\t\t\t$meta_field[&#039;type&#039;] !== &#039;color&#039; ? &#039;style=&quot;width: 100%&quot;&#039; : &#039;&#039;,\n\t\t\t\t\t\t$meta_field[&#039;id&#039;],\n\t\t\t\t\t\t$meta_field[&#039;id&#039;],\n\t\t\t\t\t\t$meta_field[&#039;type&#039;],\n\t\t\t\t\t\t$meta_value\n\t\t\t\t\t);\n\t\t\t}\n\t\t\t$output .= $this-&gt;format_rows( $label, $input );\n\t\t}\n\t\techo &#039;&lt;table class=&quot;form-table&quot;&gt;&lt;tbody&gt;&#039; . $output . &#039;&lt;\/tbody&gt;&lt;\/table&gt;&#039;;\n\t}\n\n\tpublic function format_rows( $label, $input ) {\n\t\treturn &#039;&lt;tr&gt;&lt;th&gt;&#039;.$label.&#039;&lt;\/th&gt;&lt;td&gt;&#039;.$input.&#039;&lt;\/td&gt;&lt;\/tr&gt;&#039;;\n\t}\n\n\tpublic function save_fields( $post_id ) {\n\t\tif ( ! isset( $_POST[&#039;woocommerceproductup_nonce&#039;] ) )\n\t\t\treturn $post_id;\n\t\t$nonce = $_POST[&#039;woocommerceproductup_nonce&#039;];\n\t\tif ( !wp_verify_nonce( $nonce, &#039;woocommerceproductup_data&#039; ) )\n\t\t\treturn $post_id;\n\t\tif ( defined( &#039;DOING_AUTOSAVE&#039; ) &amp;&amp; DOING_AUTOSAVE )\n\t\t\treturn $post_id;\n\t\tforeach ( $this-&gt;meta_fields as $meta_field ) {\n\t\t\tif ( isset( $_POST[ $meta_field[&#039;id&#039;] ] ) ) {\n\t\t\t\tswitch ( $meta_field[&#039;type&#039;] ) {\n\t\t\t\t\tcase &#039;email&#039;:\n\t\t\t\t\t\t$_POST[ $meta_field[&#039;id&#039;] ] = sanitize_email( $_POST[ $meta_field[&#039;id&#039;] ] );\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase &#039;text&#039;:\n\t\t\t\t\t\t$_POST[ $meta_field[&#039;id&#039;] ] = sanitize_text_field( $_POST[ $meta_field[&#039;id&#039;] ] );\n\t\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tupdate_post_meta( $post_id, $meta_field[&#039;id&#039;], $_POST[ $meta_field[&#039;id&#039;] ] );\n\t\t\t} else if ( $meta_field[&#039;type&#039;] === &#039;checkbox&#039; ) {\n\t\t\t\tupdate_post_meta( $post_id, $meta_field[&#039;id&#039;], &#039;0&#039; );\n\t\t\t}\n\t\t}\n\t}\n}\n\nif (class_exists(&#039;woocommerceproductupMetabox&#039;)) {\n\tnew woocommerceproductupMetabox;\n};\n<\/pre><\/div><h2 style=\"text-align: left\" class=\"vc_custom_heading vc_do_custom_heading\" >Visual of our WooCommerce product metabox<\/h2>\n\t<div  class=\"wpb_single_image wpb_content_element vc_align_left wpb_content_element\">\n\t\t\n\t\t<figure class=\"wpb_wrapper vc_figure\">\n\t\t\t<a href=\"https:\/\/themespress.ca\/wp-content\/uploads\/2022\/05\/Metabox-pour-vos-produits-WooCommerce-1024x144.png\" target=\"_self\" class=\"vc_single_image-wrapper   vc_box_border_grey\"><img decoding=\"async\" width=\"1402\" height=\"197\" src=\"https:\/\/themespress.ca\/wp-content\/uploads\/2022\/05\/Metabox-pour-vos-produits-WooCommerce.png\" class=\"vc_single_image-img attachment-full\" alt=\"Metabox pour vos produits WooCommerce\" title=\"Metabox pour vos produits WooCommerce\" srcset=\"https:\/\/themespress.ca\/wp-content\/uploads\/2022\/05\/Metabox-pour-vos-produits-WooCommerce.png 1402w, https:\/\/themespress.ca\/wp-content\/uploads\/2022\/05\/Metabox-pour-vos-produits-WooCommerce-600x84.png 600w, https:\/\/themespress.ca\/wp-content\/uploads\/2022\/05\/Metabox-pour-vos-produits-WooCommerce-300x42.png 300w, https:\/\/themespress.ca\/wp-content\/uploads\/2022\/05\/Metabox-pour-vos-produits-WooCommerce-1024x144.png 1024w, https:\/\/themespress.ca\/wp-content\/uploads\/2022\/05\/Metabox-pour-vos-produits-WooCommerce-768x108.png 768w, https:\/\/themespress.ca\/wp-content\/uploads\/2022\/05\/Metabox-pour-vos-produits-WooCommerce-1320x185.png 1320w\" sizes=\"(max-width: 1402px) 100vw, 1402px\" \/><\/a>\n\t\t<\/figure>\n\t<\/div>\n<h2 style=\"text-align: left\" class=\"vc_custom_heading vc_do_custom_heading\" >Adding the update date in the loop of our product<\/h2>\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>Donc nous allons utiliser le hook <a href=\"http:\/\/hookr.io\/actions\/woocommerce_before_shop_loop_item_title\/\" target=\"_blank\" rel=\"noopener\"><strong>woocommerce_before_shop_loop_item_title<\/strong><\/a><\/p>\n<p>This hook will allow us to position our <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/get_post_meta\/\" target=\"_blank\" rel=\"noopener\"><strong>get_post_meta<\/strong><\/a> of the <strong>de woocommerce-update-product<\/strong> name before the product&#8217;s title inside the loop, i.e. in product pages&#8217; results.<\/p>\n<p>We can also customize the date. With this hook, we will display the date however we see fit.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<div id=\"ts-enlighterjs-container-7613687\" class=\"ts-enlighterjs-container-single-enabled  \" style=\"width: 100%;  margin-top: 0px; margin-bottom: 0px;\" data-enlighter-doubleclick=\"true\" data-enlighter-windowbutton=\"true\" data-enlighter-windowtext=\"New Window\" data-enlighter-rawbutton=\"true\" data-enlighter-rawtext=\"RAW Code\" data-enlighter-infobutton=\"false\" data-enlighter-infotext=\"EnlighterJS\" data-enlighter-indent=\"2\"><pre id=\"ts-enlighterjs-pre-7613687\" class=\"\" style=\"white-space: pre-wrap; height: 100%; margin: 0; padding: 0;\" data-enlighter-language=\"php\" data-enlighter-theme=\"enlighter\" data-enlighter-group=\"\" data-enlighter-title=\"\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"1\" data-enlighter-highlight=\"\">add_action( &#039;woocommerce_before_shop_loop_item_title&#039;, &#039;themespress_field_display_below_title&#039;, 2 );\nfunction themespress_field_display_below_title(){\n    global $product;\n    \/\/ Get the custom field value\n    $custom_field = get_post_meta( $product-&gt;get_id(), &#039;woocommerce-update-product&#039;, true );\n    \/\/ Display\n    if( ! empty($custom_field) ){\n        echo &#039;&lt;p class=&quot;last-updates&quot; style=&quot;position:absolute;top:30px;background:#f95b26;color:white;padding:2px 5px&quot;&gt;&#039; . &#039;MAJ : &#039; . (new DateTime($custom_field))-&gt;format(&#039;d M Y&#039;) . &#039;&lt;\/p&gt;&#039;;\n    }\n}<\/pre><\/div>\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>Or we can edit the hook so we can display the n number of days since the last update of the WooCommerce product.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<div id=\"ts-enlighterjs-container-9878737\" class=\"ts-enlighterjs-container-single-enabled  \" style=\"width: 100%;  margin-top: 0px; margin-bottom: 0px;\" data-enlighter-doubleclick=\"true\" data-enlighter-windowbutton=\"true\" data-enlighter-windowtext=\"New Window\" data-enlighter-rawbutton=\"true\" data-enlighter-rawtext=\"RAW Code\" data-enlighter-infobutton=\"false\" data-enlighter-infotext=\"EnlighterJS\" data-enlighter-indent=\"2\"><pre id=\"ts-enlighterjs-pre-9878737\" class=\"\" style=\"white-space: pre-wrap; height: 100%; margin: 0; padding: 0;\" data-enlighter-language=\"php\" data-enlighter-theme=\"enlighter\" data-enlighter-group=\"\" data-enlighter-title=\"\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"1\" data-enlighter-highlight=\"\">add_action( &#039;woocommerce_before_shop_loop_item_title&#039;, &#039;themespressdeux_field_display_below_title&#039;, 2 );\nfunction themespressdeux_field_display_below_title(){\n    global $product;\n    \/\/ Get the custom field value\n    $custom_field = get_post_meta( $product-&gt;get_id(), &#039;woocommerce-update-product&#039;, true );\n    \/\/ Display\n\t$now = time(); \/\/ Current time \n\t$your_date = strtotime($custom_field); \/\/ The first date\n\t$datediff = abs($now - $your_date); \/\/ Gives absolute Value\n    if( ! empty($custom_field) ){ \n\t\techo &#039;&lt;p class=&quot;last-updates&quot; style=&quot;position:absolute;top:30px;background:#f95b26;color:white;padding:2px 5px&quot;&gt;&#039; . &#039;MAJ : &#039; . floor($datediff\/(60*60*24)) . &#039;&amp;nbsp;jour(s)&#039; . &#039;&lt;\/p&gt;&#039;;\n    }\n}<\/pre><\/div>\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p><strong>We strongly advise against copying it twice. Choose the first or second example.<\/strong><\/p>\n\n\t\t<\/div>\n\t<\/div>\n<h2 style=\"text-align: left\" class=\"vc_custom_heading vc_do_custom_heading\" >Display the get_post_meta in the WooCommerce product<\/h2>\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>All that&#8217;s left to do now is use the <a href=\"http:\/\/hookr.io\/actions\/woocommerce_before_single_product_summary\/\" target=\"_blank\" rel=\"noopener\">woocommerce_before_single_product_summary<\/a> hook in order to display the information about the product.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<div id=\"ts-enlighterjs-container-2469040\" class=\"ts-enlighterjs-container-single-enabled  \" style=\"width: 100%;  margin-top: 0px; margin-bottom: 0px;\" data-enlighter-doubleclick=\"true\" data-enlighter-windowbutton=\"true\" data-enlighter-windowtext=\"New Window\" data-enlighter-rawbutton=\"true\" data-enlighter-rawtext=\"RAW Code\" data-enlighter-infobutton=\"false\" data-enlighter-infotext=\"EnlighterJS\" data-enlighter-indent=\"2\"><pre id=\"ts-enlighterjs-pre-2469040\" class=\"\" style=\"white-space: pre-wrap; height: 100%; margin: 0; padding: 0;\" data-enlighter-language=\"php\" data-enlighter-theme=\"enlighter\" data-enlighter-group=\"\" data-enlighter-title=\"\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"1\" data-enlighter-highlight=\"\">add_action( &#039;woocommerce_before_single_product_summary&#039; , &#039;themespress_updates_date_before_product_title&#039;, 5 );\nfunction themespress_updates_date_before_product_title() {\n    global $product;\n    \/\/ Get the custom field value\n    $custom_field = get_post_meta( $product-&gt;get_id(), &#039;woocommerce-update-product&#039;, true );\n    \/\/ Display\n\t$now = time(); \/\/ Current time \n\t$your_date = strtotime($custom_field); \/\/ The first date\n\t$datediff = abs($now - $your_date); \/\/ Gives absolute Value\n    if( ! empty($custom_field) ){ \n\t\techo &#039;&lt;p class=&quot;last-updates&quot; style=&quot;position:absolute;top:-40px;right:0;background:#f95b26;color:white;padding:2px 5px&quot;&gt;&#039; . &#039;MAJ : &#039; . floor($datediff\/(60*60*24)) . &#039;&amp;nbsp;jour(s)&#039; . &#039;&lt;\/p&gt;&#039;;\n    }\n}<\/pre><\/div><\/div><\/div><\/div><\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"With this tutorial, we will create a new WordPress metabox for our downloadable WooCommerce products. Our data would allow us to diplay the date where the number of days where<div class=\"read-more\"><a class=\"btn button-secondary\" href=\"https:\/\/themespress.ca\/en\/adding-metadata-latest-update-woocommerce-product\/\">Read More<\/a><\/div>","protected":false},"author":81,"featured_media":5891,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[283],"tags":[328],"class_list":["post-5890","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-woocommerce-config"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/5890","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/users\/81"}],"replies":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/comments?post=5890"}],"version-history":[{"count":0,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/5890\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media\/5891"}],"wp:attachment":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media?parent=5890"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/categories?post=5890"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/tags?post=5890"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}