├── screenshot.png ├── screenshot-1.png ├── wc-custom-product-data-fields.php ├── assets ├── css │ └── wcpdf-main.css └── js │ ├── wcpdf-main.js.bak │ └── wcpdf-main.js ├── fields-init.php ├── README.md ├── readme.txt └── class-wc-product-data-fields.php /screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kharissulistiyo/WooCommerce-Custom-Product-Data-Fields/HEAD/screenshot.png -------------------------------------------------------------------------------- /screenshot-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kharissulistiyo/WooCommerce-Custom-Product-Data-Fields/HEAD/screenshot-1.png -------------------------------------------------------------------------------- /wc-custom-product-data-fields.php: -------------------------------------------------------------------------------- 1 |
Back to WordPress Admin."); 34 | } 35 | } 36 | } 37 | add_action('admin_init', 'wc_productdata_options_wp_requred'); 38 | } 39 | 40 | 41 | 42 | // Checks if the WooCommerce plugins is installed and active. 43 | if(in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))){ 44 | 45 | require_once dirname( __FILE__ ) . '/class-wc-product-data-fields.php'; 46 | 47 | } 48 | -------------------------------------------------------------------------------- /assets/css/wcpdf-main.css: -------------------------------------------------------------------------------- 1 | .wc_cpdf_tab .form-field{ 2 | /* border-bottom: 1px solid #ddd; */ 3 | } 4 | 5 | #image-uploader-meta-box-list:after{ 6 | display:block; 7 | content:''; 8 | clear:both; 9 | } 10 | #image-uploader-meta-box-list li { 11 | float: left; 12 | width: 150px; 13 | height:auto; 14 | text-align: center; 15 | margin: 10px 10px 10px 0; 16 | } 17 | input.iumb{ 18 | width:50%; 19 | } 20 | #image-uploader-meta-box-list li img{ 21 | max-width:150px; 22 | } 23 | a.iumb-add.none, a.change-image.none, a.remove-image.none{ 24 | display:none; 25 | visibility:hidden; 26 | } 27 | 28 | 29 | /** 30 | * @ December 15 31 | */ 32 | 33 | 34 | /* Image field */ 35 | 36 | .image-field-wrapper{ 37 | margin: 15px 0; 38 | } 39 | 40 | .image-field-wrapper::before, 41 | .image-field-wrapper::after{ 42 | content: ''; 43 | display: block; 44 | clear: both; 45 | } 46 | 47 | .image-field-wrapper > div{ 48 | float: left; 49 | } 50 | 51 | .image-field-wrapper .image-field-label{ 52 | width: 162px; 53 | } 54 | 55 | .image-field-wrapper .image-field-label span{ 56 | margin-left: 12px; 57 | } 58 | 59 | .image-field-wrapper .image-field-upload{ 60 | width: 59%; 61 | } 62 | 63 | .image-field-wrapper .image-field-upload .wcpdf_image_url{ 64 | height: 28px; 65 | margin-right: 3px; 66 | } 67 | 68 | .image-field-wrapper .image-field-upload p{ 69 | margin: 0; 70 | } 71 | 72 | .preview-image-wrapper, 73 | .gal-item{ 74 | max-width: 110px; 75 | position: relative; 76 | } 77 | 78 | .gallery .preview-image-wrapper{ 79 | max-width: 100%; 80 | } 81 | 82 | .gallery .preview-image-wrapper::before, 83 | .gallery .preview-image-wrapper::after{ 84 | content: ''; 85 | display: block; 86 | clear: both; 87 | } 88 | 89 | .gallery .gal-item{ 90 | float: left; 91 | margin-right: 7px; 92 | } 93 | 94 | a.wcpdf-remove-image{ 95 | text-decoration: none; 96 | color: #f00; 97 | display: inline-block; 98 | position: absolute; 99 | top: -2px; 100 | right: -4px; 101 | background: #fff; 102 | line-height: 1; 103 | padding: 2px 2px 1px 2px; 104 | border-radius: 100%; 105 | } 106 | 107 | a.wcpdf-remove-image em{ 108 | display: none; 109 | } 110 | 111 | a.wcpdf-remove-image::before{ 112 | font-family: WooCommerce; 113 | font-weight: 400; 114 | font-variant: normal; 115 | text-transform: none; 116 | content: "\e013"; 117 | } 118 | 119 | 120 | .preview-image-wrapper img{ 121 | margin-top: 5px; 122 | padding: 2px; 123 | border: 1px solid #ddd; 124 | } 125 | 126 | .wcpdf_saved_image, 127 | .preview-image-wrapper img{ 128 | max-width: 100px; 129 | height: auto; 130 | } 131 | -------------------------------------------------------------------------------- /assets/js/wcpdf-main.js.bak: -------------------------------------------------------------------------------- 1 | (function($){ 2 | 3 | "use strict"; 4 | 5 | var file_frame; 6 | 7 | $(document).on('click', 'a.iumb-add', function(e) { 8 | 9 | e.preventDefault(); 10 | 11 | if (file_frame) file_frame.close(); 12 | 13 | file_frame = wp.media.frames.file_frame = wp.media({ 14 | title: $(this).data('uploader-title'), 15 | // Tell the modal to show only images. 16 | library: { 17 | type: 'image' 18 | }, 19 | button: { 20 | text: $(this).data('uploader-button-text'), 21 | }, 22 | multiple: false 23 | }); 24 | 25 | file_frame.on('select', function() { 26 | var listIndex = $('#image-uploader-meta-box-list li').index($('#image-uploader-meta-box-list li:last')), 27 | selection = file_frame.state().get('selection'); 28 | 29 | selection.map(function(attachment) { 30 | attachment = attachment.toJSON(), 31 | 32 | console.log('Attachment', attachment.url); 33 | 34 | $('.iumb').val(attachment.url); 35 | 36 | $('#image-uploader-meta-box-list').append('
  • '); 37 | $('#image-uploader-meta-box a.iumb-add').addClass('none'); 38 | $('a.change-image').removeClass('none').show(); 39 | $('a.remove-image').removeClass('none').show(); 40 | 41 | index = listIndex; 42 | 43 | // $('#image-uploader-meta-box-list').append('
  • '); 44 | 45 | $('.iumb').remove(); 46 | // $('input[name="_iumb"]').val(attachment.url); 47 | 48 | // $('#image-uploader-meta-box a.iumb-add').addClass('none'); 49 | // $('a.change-image').removeClass('none').show(); 50 | // $('a.remove-image').removeClass('none').show(); 51 | 52 | }); 53 | }); 54 | 55 | makeSortable(); 56 | 57 | file_frame.open(); 58 | 59 | }); 60 | 61 | $(document).on('click', 'a.change-image', function(e) { 62 | 63 | e.preventDefault(); 64 | 65 | var that = $(this); 66 | 67 | if (file_frame) file_frame.close(); 68 | 69 | file_frame = wp.media.frames.file_frame = wp.media({ 70 | title: $(this).data('uploader-title'), 71 | button: { 72 | text: $(this).data('uploader-button-text'), 73 | }, 74 | multiple: false 75 | }); 76 | 77 | alert('Hae!'); 78 | 79 | file_frame.on( 'select', function() { 80 | attachment = file_frame.state().get('selection').first().toJSON(); 81 | 82 | alert('Hae!'); 83 | 84 | that.parent().find('input:hidden').attr('value', attachment.id); 85 | that.parent().find('img.image-preview').attr('src', attachment.sizes.thumbnail.url); 86 | }); 87 | 88 | file_frame.open(); 89 | 90 | }); 91 | 92 | function resetIndex() { 93 | $('#image-uploader-meta-box-list li').each(function(i) { 94 | $(this).find('input:hidden').attr('name', 'iumb'); 95 | }); 96 | } 97 | 98 | function makeSortable() { 99 | $('#image-uploader-meta-box-list').sortable({ 100 | opacity: 0.6, 101 | stop: function() { 102 | resetIndex(); 103 | } 104 | }); 105 | } 106 | 107 | $(document).on('click', '#image-uploader-meta-box a.remove-image', function(e) { 108 | 109 | 110 | $('#image-uploader-meta-box a.iumb-add').removeClass('none'); 111 | $('a.change-image').hide(); 112 | $(this).hide(); 113 | 114 | $('input[name="iumb"]').val(''); 115 | $('input[name="_iumb"]').val(''); 116 | 117 | $('#image-uploader-meta-box-list li').animate({ opacity: 0 }, 200, function() { 118 | $(this).remove(); 119 | resetIndex(); 120 | }); 121 | 122 | e.preventDefault(); 123 | 124 | }); 125 | 126 | makeSortable(); 127 | 128 | })(jQuery); 129 | -------------------------------------------------------------------------------- /assets/js/wcpdf-main.js: -------------------------------------------------------------------------------- 1 | (function($){ 2 | 3 | "use strict"; 4 | 5 | var file_frame; 6 | 7 | if ($('a.wcpdf-remove-image').length > 0){ 8 | $(document).on('click', 'a.wcpdf-remove-image', function(e) { 9 | 10 | e.preventDefault(); 11 | 12 | $(this).parent().parent().children('.wcpdf_image_id').val(''); 13 | $(this).parent().parent().children('.wcpdf_image_url').val(''); 14 | $(this).parent().children('img').remove(); 15 | 16 | if($('.gal-item').length > 0){ 17 | $(this).next('input').val('').remove(); 18 | } 19 | 20 | $(this).remove(); 21 | 22 | }); 23 | } 24 | 25 | if ($('a.wcpdf-uppload-image').length > 0){ 26 | $(document).on('click', 'a.wcpdf-uppload-image', function(e) { 27 | 28 | e.preventDefault(); 29 | 30 | var $fieldID = $(this).parent().children('.wcpdf_image_id' ); 31 | var $fieldURL = $(this).parent().children( '.wcpdf_image_url' ); 32 | var $preViewWrapper = $(this).parent().children('.preview-image-wrapper'); 33 | var $savedImage = $(this).parent().children('.preview-image-wrapper').find('.iumb_saved_image'); 34 | 35 | if (file_frame) file_frame.close(); 36 | 37 | file_frame = wp.media.frames.file_frame = wp.media({ 38 | title: $(this).data('uploader-title'), 39 | library: { 40 | type: 'image' 41 | }, 42 | button: { 43 | text: $(this).data('uploader-button-text'), 44 | }, 45 | multiple: false 46 | }); 47 | 48 | file_frame.on('select', function() { 49 | var listIndex = $('#image-uploader-meta-box-list li').index($('#image-uploader-meta-box-list li:last')), 50 | selection = file_frame.state().get('selection'); 51 | 52 | selection.map(function(attachment) { 53 | 54 | var attachment = attachment.toJSON(); 55 | var imageURL = attachment.url; 56 | var imageID = attachment.id; 57 | 58 | $('.iumb').val(attachment.url); 59 | 60 | if( attachment.url != '' ){ 61 | 62 | $savedImage.remove(); 63 | 64 | var preview = ''+ 65 | 'Remove'; 66 | 67 | $($preViewWrapper).html(preview); 68 | 69 | $fieldURL.val(imageURL); 70 | $fieldID.val(imageID); 71 | 72 | } 73 | 74 | }); 75 | 76 | }); 77 | 78 | file_frame.open(); 79 | 80 | }); 81 | } 82 | 83 | 84 | 85 | 86 | /** 87 | * ========================== 88 | * Gallery images start 89 | * ========================== 90 | */ 91 | 92 | if ($('a.wcpdf-uppload-image-gallery').length > 0){ 93 | $(document).on('click', 'a.wcpdf-uppload-image-gallery', function(e) { 94 | 95 | e.preventDefault(); 96 | 97 | var $fieldID = $(this).parent().children('.wcpdf_image_id' ); 98 | var $fieldURL = $(this).parent().children( '.wcpdf_image_url' ); 99 | var $preViewWrapper = $(this).parent().children('.preview-image-wrapper'); 100 | var $savedImage = $(this).parent().children('.preview-image-wrapper').find('.iumb_saved_image'); 101 | 102 | if (file_frame) file_frame.close(); 103 | 104 | file_frame = wp.media.frames.file_frame = wp.media({ 105 | title: $(this).data('uploader-title'), 106 | library: { 107 | type: 'image' 108 | }, 109 | button: { 110 | text: $(this).data('uploader-button-text'), 111 | }, 112 | multiple: true 113 | }); 114 | 115 | file_frame.on('select', function() { 116 | var listIndex = $('#image-uploader-meta-box-list li').index($('#image-uploader-meta-box-list li:last')), 117 | selection = file_frame.state().get('selection'); 118 | 119 | selection.map(function(attachment) { 120 | 121 | var attachment = attachment.toJSON(); 122 | var imageURL = attachment.url; 123 | var imageID = attachment.id; 124 | 125 | $('.iumb').val(attachment.url); 126 | 127 | if( attachment.url != '' ){ 128 | 129 | $savedImage.remove(); 130 | 131 | var preview = '
    '+ 132 | ''+ 133 | 'Remove'+ 134 | ''+ 135 | '
    '; 136 | 137 | $($preViewWrapper).append(preview); 138 | 139 | $fieldURL.val(imageURL); 140 | $fieldID.val(imageID); 141 | 142 | } 143 | 144 | }); 145 | 146 | }); 147 | 148 | file_frame.open(); 149 | 150 | }); 151 | } 152 | 153 | 154 | /** 155 | * ========================== 156 | * Gallery images end 157 | * ========================== 158 | */ 159 | 160 | 161 | // Color Picker 162 | 163 | if($('.wc_cpdf_colorpicker').length > 0){ 164 | $('.wc_cpdf_colorpicker').wpColorPicker(); 165 | } 166 | 167 | 168 | // Date picker 169 | 170 | if($('.wc_cpdf_datepicker').length > 0){ 171 | 172 | $( '.wc_cpdf_datepicker' ).each( function() { 173 | $('.wc_cpdf_datepicker').datepicker({ 174 | defaultDate: '', 175 | dateFormat: 'yy-mm-dd', 176 | numberOfMonths: 1, 177 | showButtonPanel: true 178 | }); 179 | }); 180 | 181 | } 182 | 183 | 184 | })(jQuery); 185 | -------------------------------------------------------------------------------- /fields-init.php: -------------------------------------------------------------------------------- 1 | __('Custom Data 2sss', 'wc_cpdf'), 25 | ), 26 | 27 | array( 28 | 'id' => '_mytext2', 29 | 'type' => 'text', 30 | 'label' => __('Text', 'wc_cpdf'), 31 | 'placeholder' => __('A placeholder text goes here.', 'wc_cpdf'), 32 | 'class' => 'large', 33 | 'description' => __('Field description.', 'wc_cpdf'), 34 | 'desc_tip' => true, 35 | ), 36 | 37 | array( 38 | 'id' => '_mynumber2', 39 | 'type' => 'number', 40 | 'label' => __('Number', 'wc_cpdf'), 41 | 'placeholder' => __('Number.', 'wc_cpdf'), 42 | 'class' => 'short', 43 | 'description' => __('Field description.', 'wc_cpdf'), 44 | 'desc_tip' => true, 45 | ), 46 | 47 | array( 48 | 'id' => '_mytextarea2', 49 | 'type' => 'textarea', 50 | 'label' => __('Textarea', 'wc_cpdf'), 51 | 'placeholder' => __('A placeholder text goes here.', 'wc_cpdf'), 52 | 'style' => 'width:70%;height:140px;', 53 | 'description' => __('Field description.', 'wc_cpdf'), 54 | 'desc_tip' => true, 55 | ), 56 | 57 | array( 58 | 'id' => '_mycheckbox2', 59 | 'type' => 'checkbox', 60 | 'label' => __('Checkbox', 'wc_cpdf'), 61 | 'description' => __('Field description.', 'wc_cpdf'), 62 | 'desc_tip' => true, 63 | ), 64 | 65 | array( 66 | 'id' => '_myselect2', 67 | 'type' => 'select', 68 | 'label' => __('Select', 'wc_cpdf'), 69 | 'options' => array( 70 | 'option_1' => 'Option 1', 71 | 'option_2' => 'Option 2', 72 | 'option_3' => 'Option 3' 73 | ), 74 | 'description' => __('Field description.', 'wc_cpdf'), 75 | 'desc_tip' => true, 76 | ), 77 | 78 | array( 79 | 'id' => '_myradio2', 80 | 'type' => 'radio', 81 | 'label' => __('Radio', 'wc_cpdf'), 82 | 'options' => array( 83 | 'radio_1' => 'Radio 1', 84 | 'radio_2' => 'Radio 2', 85 | 'radio_3' => 'Radio 3' 86 | ), 87 | 'description' => __('Field description.', 'wc_cpdf'), 88 | 'desc_tip' => true, 89 | ), 90 | 91 | array( 92 | 'id' => '_myhidden2', 93 | 'type' => 'hidden', 94 | 'value' => 'Hidden Value', 95 | ), 96 | 97 | 98 | // New field type goes here 99 | // Multiselect 100 | array( 101 | 'id' => '_mymultiselect22', 102 | 'type' => 'multiselect', 103 | 'label' => __('Multiselect', 'wc_cpdf'), 104 | 'placeholder' => __('Multiselect maan!', 'wc_cpdf'), 105 | 'options' => array( 106 | 'option_1' => 'Option 1', 107 | 'option_2' => 'Option 2', 108 | 'option_3' => 'Option 3', 109 | 'option_4' => 'Option 4', 110 | 'option_5' => 'Option 5' 111 | ), 112 | 'description' => __('Field description.', 'wc_cpdf'), 113 | 'desc_tip' => true, 114 | 'class' => 'medium' 115 | ), 116 | 117 | // image 118 | array( 119 | 'id' => '_myimage_12', 120 | 'type' => 'image', 121 | 'label' => __('Image 1', 'wc_cpdf'), 122 | 'description' => __('Field description.', 'wc_cpdf'), 123 | 'desc_tip' => true, 124 | ), 125 | 126 | array( 127 | 'id' => '_myimage_22', 128 | 'type' => 'image', 129 | 'label' => __('Image 2', 'wc_cpdf'), 130 | 'description' => __('Field description.', 'wc_cpdf'), 131 | 'desc_tip' => true, 132 | ), 133 | 134 | 135 | array( 136 | 'id' => '_mygallery2', 137 | 'type' => 'gallery', 138 | 'label' => __('Gallery', 'wc_cpdf'), 139 | 'description' => __('Field description.', 'wc_cpdf'), 140 | 'desc_tip' => true, 141 | ), 142 | 143 | // Color 144 | array( 145 | 'id' => '_mycolor2', 146 | 'type' => 'color', 147 | 'label' => __('Select color', 'wc_cpdf'), 148 | 'placeholder' => __('A placeholder text goes here.', 'wc_cpdf'), 149 | 'class' => 'large', 150 | 'description' => __('Field description.', 'wc_cpdf'), 151 | 'desc_tip' => true, 152 | ), 153 | 154 | array( 155 | 'id' => '_mycolor22', 156 | 'type' => 'color', 157 | 'label' => __('Select color 2', 'wc_cpdf'), 158 | 'placeholder' => __('A placeholder text goes here.', 'wc_cpdf'), 159 | 'class' => 'large', 160 | 'description' => __('Field description.', 'wc_cpdf'), 161 | 'desc_tip' => true, 162 | ), 163 | 164 | // Datepicker 165 | 166 | array( 167 | 'id' => '_mydatepicker2', 168 | 'type' => 'datepicker', 169 | 'label' => __('Select date', 'wc_cpdf'), 170 | 'placeholder' => __('A placeholder text goes here.', 'wc_cpdf'), 171 | 'class' => 'large', 172 | 'description' => __('Field description.', 'wc_cpdf'), 173 | 'desc_tip' => true, 174 | ), 175 | 176 | array( 177 | 'type' => 'divider' 178 | ), 179 | 180 | array( 181 | 'id' => '_mydatepicker22', 182 | 'type' => 'datepicker', 183 | 'label' => __('Select date 2', 'wc_cpdf'), 184 | 'placeholder' => __('A placeholder text goes here.', 'wc_cpdf'), 185 | 'class' => 'large', 186 | 'description' => __('Field description.', 'wc_cpdf'), 187 | 'desc_tip' => true, 188 | ) 189 | 190 | ); 191 | 192 | /** First product data tab ends **/ 193 | /** ===================================== */ 194 | 195 | 196 | /** Second product data tab starts **/ 197 | /** ===================================== */ 198 | 199 | $custom_product_data_fields['custom_data_2'] = array( 200 | 201 | array( 202 | 'tab_name' => __('Custom Data ABC', 'wc_cpdf'), 203 | ), 204 | 205 | array( 206 | 'id' => '_mytext_abcd', 207 | 'type' => 'text', 208 | 'label' => __('Text ABCD', 'wc_cpdf'), 209 | 'placeholder' => __('A placeholder text goes here.', 'wc_cpdf'), 210 | 'class' => 'large', 211 | 'description' => __('Field description.', 'wc_cpdf'), 212 | 'desc_tip' => true, 213 | ) 214 | 215 | ); 216 | 217 | return $custom_product_data_fields; 218 | 219 | } 220 | 221 | endif; 222 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | WooCommerce Custom Product Data Fields 2 | ====================================== 3 | 4 | WooCommerce Custom Product Data Fields 5 | 6 | WooCommerce Custom Product Data Fields is a simple framework which will help you to build extra product data fields, e.g. secondary product title, vendor info, custom message for individual product, etc. 7 | 8 | You can use this framework as a library of your ‘brand-new’ WooCommerce Extension. 9 | 10 | [Download]: http://wordpress.org/plugins/woocommerce-custom-product-data-fields/ 11 | 12 | [Download][] stable version from WordPress.org plugin repository. 13 | 14 | 15 | ## Installation 16 | 17 | 1. Upload this plugin to the /wp-content/plugins/ directory. 18 | 2. Activate the plugin through the Plugins menu in WordPress. 19 | 3. Define your custom product data fields in your theme `functions.php` file. See `fields-init.php` inside this plugin folder. 20 | 21 | 22 | ## Available Fields 23 | 24 | At the very first release, supported fields are: 25 | 26 | * text 27 | * number 28 | * textarea 29 | * checkbox 30 | * select 31 | * radio 32 | * hidden 33 | * multiselect 34 | * image 35 | * gallery 36 | * colorpicker 37 | * datepicker 38 | * devider 39 | 40 | ## Defining Your Fields 41 | 42 | To make your own fields (as seen on the screenshot above), put this example fields in functions.php of your theme. 43 | 44 | ``` 45 | /** 46 | * WooCommerce product data tab definition 47 | * 48 | * @return array 49 | */ 50 | 51 | add_action('wc_cpdf_init', 'prefix_custom_product_data_tab_init', 10, 0); 52 | if(!function_exists('prefix_custom_product_data_tab_init')) : 53 | 54 | function prefix_custom_product_data_tab_init(){ 55 | 56 | 57 | $custom_product_data_fields = array(); 58 | 59 | 60 | /** First product data tab starts **/ 61 | /** ===================================== */ 62 | 63 | $custom_product_data_fields['custom_data_1'] = array( 64 | 65 | array( 66 | 'tab_name' => __('Custom Data', 'wc_cpdf'), 67 | ), 68 | 69 | array( 70 | 'id' => '_mytext', 71 | 'type' => 'text', 72 | 'label' => __('Text', 'wc_cpdf'), 73 | 'placeholder' => __('A placeholder text goes here.', 'wc_cpdf'), 74 | 'class' => 'large', 75 | 'description' => __('Field description.', 'wc_cpdf'), 76 | 'desc_tip' => true, 77 | ), 78 | 79 | array( 80 | 'id' => '_mynumber', 81 | 'type' => 'number', 82 | 'label' => __('Number', 'wc_cpdf'), 83 | 'placeholder' => __('Number.', 'wc_cpdf'), 84 | 'class' => 'short', 85 | 'description' => __('Field description.', 'wc_cpdf'), 86 | 'desc_tip' => true, 87 | ), 88 | 89 | array( 90 | 'id' => '_mytextarea', 91 | 'type' => 'textarea', 92 | 'label' => __('Textarea', 'wc_cpdf'), 93 | 'placeholder' => __('A placeholder text goes here.', 'wc_cpdf'), 94 | 'style' => 'width:70%;height:140px;', 95 | 'description' => __('Field description.', 'wc_cpdf'), 96 | 'desc_tip' => true, 97 | ), 98 | 99 | array( 100 | 'id' => '_mycheckbox', 101 | 'type' => 'checkbox', 102 | 'label' => __('Checkbox', 'wc_cpdf'), 103 | 'description' => __('Field description.', 'wc_cpdf'), 104 | 'desc_tip' => true, 105 | ), 106 | 107 | array( 108 | 'id' => '_myselect', 109 | 'type' => 'select', 110 | 'label' => __('Select', 'wc_cpdf'), 111 | 'options' => array( 112 | 'option_1' => 'Option 1', 113 | 'option_2' => 'Option 2', 114 | 'option_3' => 'Option 3' 115 | ), 116 | 'description' => __('Field description.', 'wc_cpdf'), 117 | 'desc_tip' => true, 118 | ), 119 | 120 | array( 121 | 'id' => '_myradio', 122 | 'type' => 'radio', 123 | 'label' => __('Radio', 'wc_cpdf'), 124 | 'options' => array( 125 | 'radio_1' => 'Radio 1', 126 | 'radio_2' => 'Radio 2', 127 | 'radio_3' => 'Radio 3' 128 | ), 129 | 'description' => __('Field description.', 'wc_cpdf'), 130 | 'desc_tip' => true, 131 | ), 132 | 133 | array( 134 | 'id' => '_myhidden', 135 | 'type' => 'hidden', 136 | 'value' => 'Hidden Value', 137 | ), 138 | 139 | array( 140 | 'id' => '_mymultiselect', 141 | 'type' => 'multiselect', 142 | 'label' => __('Multiselect', 'wc_cpdf'), 143 | 'placeholder' => __('Multiselect maan!', 'wc_cpdf'), 144 | 'options' => array( 145 | 'option_1' => 'Option 1', 146 | 'option_2' => 'Option 2', 147 | 'option_3' => 'Option 3', 148 | 'option_4' => 'Option 4', 149 | 'option_5' => 'Option 5' 150 | ), 151 | 'description' => __('Field description.', 'wc_cpdf'), 152 | 'desc_tip' => true, 153 | 'class' => 'medium' 154 | ), 155 | 156 | // image 157 | array( 158 | 'id' => '_myimage', 159 | 'type' => 'image', 160 | 'label' => __('Image 1', 'wc_cpdf'), 161 | 'description' => __('Field description.', 'wc_cpdf'), 162 | 'desc_tip' => true, 163 | ), 164 | 165 | array( 166 | 'id' => '_mygallery', 167 | 'type' => 'gallery', 168 | 'label' => __('Gallery', 'wc_cpdf'), 169 | 'description' => __('Field description.', 'wc_cpdf'), 170 | 'desc_tip' => true, 171 | ), 172 | 173 | // Color 174 | array( 175 | 'id' => '_mycolor', 176 | 'type' => 'color', 177 | 'label' => __('Select color', 'wc_cpdf'), 178 | 'placeholder' => __('A placeholder text goes here.', 'wc_cpdf'), 179 | 'class' => 'large', 180 | 'description' => __('Field description.', 'wc_cpdf'), 181 | 'desc_tip' => true, 182 | ), 183 | 184 | // Datepicker 185 | 186 | array( 187 | 'id' => '_mydatepicker', 188 | 'type' => 'datepicker', 189 | 'label' => __('Select date', 'wc_cpdf'), 190 | 'placeholder' => __('A placeholder text goes here.', 'wc_cpdf'), 191 | 'class' => 'large', 192 | 'description' => __('Field description.', 'wc_cpdf'), 193 | 'desc_tip' => true, 194 | ), 195 | 196 | array( 197 | 'type' => 'divider' 198 | ) 199 | 200 | ); 201 | 202 | /** First product data tab ends **/ 203 | /** ===================================== */ 204 | 205 | 206 | /** Second product data tab starts **/ 207 | /** ===================================== */ 208 | 209 | $custom_product_data_fields['custom_data_2'] = array( 210 | 211 | array( 212 | 'tab_name' => __('Custom Data 2', 'wc_cpdf'), 213 | ), 214 | 215 | array( 216 | 'id' => '_mytext_2', 217 | 'type' => 'text', 218 | 'label' => __('Text ABCD', 'wc_cpdf'), 219 | 'placeholder' => __('A placeholder text goes here.', 'wc_cpdf'), 220 | 'class' => 'large', 221 | 'description' => __('Field description.', 'wc_cpdf'), 222 | 'desc_tip' => true, 223 | ) 224 | 225 | ); 226 | 227 | return $custom_product_data_fields; 228 | 229 | } 230 | 231 | endif; 232 | 233 | ``` 234 | 235 | 236 | ## Getting The Field Value 237 | 238 | ``` 239 | /** 240 | * 241 | * $wc_cpdf->get_value($post_id, $field_id); 242 | * $post_id = (integer) post ID 243 | * $field_id = (string) unique field ID 244 | * 245 | */ 246 | 247 | global $wc_cpdf; 248 | echo $wc_cpdf->get_value(get_the_ID(), '_mytext'); 249 | ``` 250 | 251 | **Getting multiselect value** 252 | 253 | ``` 254 | global $wc_cpdf; 255 | $multiselect = $wc_cpdf->get_value($post->ID, '_mymultiselect'); 256 | foreach ($multiselect as $value) { 257 | echo $value; 258 | } 259 | ``` 260 | 261 | **Getting image value** 262 | 263 | ``` 264 | global $wc_cpdf; 265 | $image_id = $wc_cpdf->get_value($post->ID, '_myimage'); 266 | $size = 'thumbnail'; 267 | $image_attachment = wp_get_attachment_image($image_id, $size); 268 | echo $image_attachment; 269 | ``` 270 | 271 | **Getting gallery value** 272 | 273 | ``` 274 | global $wc_cpdf; 275 | $gallery = $wc_cpdf->get_value($post->ID, '_mygallery'); 276 | foreach ($gallery as $image_id) { 277 | 278 | $image_id = $wc_cpdf->get_value($post->ID, '_mygallery'); 279 | $size = 'thumbnail'; 280 | $image_attachment = wp_get_attachment_image($image_id, $size); 281 | 282 | echo $image_attachment; 283 | 284 | } 285 | ``` 286 | 287 | ## License 288 | 289 | [GNU General Public License v3.0]: http://www.gnu.org/licenses/gpl-3.0.html 290 | 291 | [GNU General Public License v3.0][] 292 | 293 | 294 | ## More Info 295 | 296 | [kharisulistiyo(at)gmail(dot)com]: mailto:kharisulistiyo@gmail.com 297 | [@kharissulistiyo]: http://twitter.com/kharissulistiyo 298 | 299 | Contact me: 300 | 301 | * Mail: [kharisulistiyo(at)gmail(dot)com][] 302 | * Twitter: [@kharissulistiyo][] 303 | -------------------------------------------------------------------------------- /readme.txt: -------------------------------------------------------------------------------- 1 | === WooCommerce Custom Product Data Fields === 2 | Contributors: kharisblank 3 | Tags: ecommerce, e-commerce, commerce, woocommerce, extension, product, tab, framework 4 | Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=ACYNA5XNUGBUL 5 | Requires at least: 3.1 6 | Tested up to: 3.9.1 7 | Stable tag: 1.1 8 | License: GPLv3 9 | License URI: http://www.gnu.org/licenses/gpl-3.0.html 10 | 11 | WooCommerce extension which will help you to build extra product data fields easily. 12 | 13 | == Description == 14 | 15 | WooCommerce Custom Product Data Fields is a simple framework which will help you to build extra product data fields easily, e.g. secondary product title, vendor info, custom message for individual product, etc. 16 | 17 | You can use this plugin as a library of your ‘brand-new’ WooCommerce Extension. 18 | 19 | = Available Fields = 20 | 21 | * text 22 | * number 23 | * textarea 24 | * checkbox 25 | * select 26 | * radio 27 | * hidden 28 | * multiselect 29 | * image 30 | * gallery 31 | * colorpicker 32 | * datepicker 33 | * devider 34 | 35 | = Defining Your Fields = 36 | 37 | 38 | /** 39 | * WooCommerce product data tab definition 40 | * 41 | * @return array 42 | */ 43 | 44 | add_action('wc_cpdf_init', 'prefix_custom_product_data_tab_init', 10, 0); 45 | if(!function_exists('prefix_custom_product_data_tab_init')) : 46 | 47 | function prefix_custom_product_data_tab_init(){ 48 | 49 | 50 | $custom_product_data_fields = array(); 51 | 52 | 53 | /** First product data tab starts **/ 54 | /** ===================================== */ 55 | 56 | $custom_product_data_fields['custom_data_1'] = array( 57 | 58 | array( 59 | 'tab_name' => __('Custom Data', 'wc_cpdf'), 60 | ), 61 | 62 | array( 63 | 'id' => '_mytext', 64 | 'type' => 'text', 65 | 'label' => __('Text', 'wc_cpdf'), 66 | 'placeholder' => __('A placeholder text goes here.', 'wc_cpdf'), 67 | 'class' => 'large', 68 | 'description' => __('Field description.', 'wc_cpdf'), 69 | 'desc_tip' => true, 70 | ), 71 | 72 | array( 73 | 'id' => '_mynumber', 74 | 'type' => 'number', 75 | 'label' => __('Number', 'wc_cpdf'), 76 | 'placeholder' => __('Number.', 'wc_cpdf'), 77 | 'class' => 'short', 78 | 'description' => __('Field description.', 'wc_cpdf'), 79 | 'desc_tip' => true, 80 | ), 81 | 82 | array( 83 | 'id' => '_mytextarea', 84 | 'type' => 'textarea', 85 | 'label' => __('Textarea', 'wc_cpdf'), 86 | 'placeholder' => __('A placeholder text goes here.', 'wc_cpdf'), 87 | 'style' => 'width:70%;height:140px;', 88 | 'description' => __('Field description.', 'wc_cpdf'), 89 | 'desc_tip' => true, 90 | ), 91 | 92 | array( 93 | 'id' => '_mycheckbox', 94 | 'type' => 'checkbox', 95 | 'label' => __('Checkbox', 'wc_cpdf'), 96 | 'description' => __('Field description.', 'wc_cpdf'), 97 | 'desc_tip' => true, 98 | ), 99 | 100 | array( 101 | 'id' => '_myselect', 102 | 'type' => 'select', 103 | 'label' => __('Select', 'wc_cpdf'), 104 | 'options' => array( 105 | 'option_1' => 'Option 1', 106 | 'option_2' => 'Option 2', 107 | 'option_3' => 'Option 3' 108 | ), 109 | 'description' => __('Field description.', 'wc_cpdf'), 110 | 'desc_tip' => true, 111 | ), 112 | 113 | array( 114 | 'id' => '_myradio', 115 | 'type' => 'radio', 116 | 'label' => __('Radio', 'wc_cpdf'), 117 | 'options' => array( 118 | 'radio_1' => 'Radio 1', 119 | 'radio_2' => 'Radio 2', 120 | 'radio_3' => 'Radio 3' 121 | ), 122 | 'description' => __('Field description.', 'wc_cpdf'), 123 | 'desc_tip' => true, 124 | ), 125 | 126 | array( 127 | 'id' => '_myhidden', 128 | 'type' => 'hidden', 129 | 'value' => 'Hidden Value', 130 | ), 131 | 132 | array( 133 | 'id' => '_mymultiselect', 134 | 'type' => 'multiselect', 135 | 'label' => __('Multiselect', 'wc_cpdf'), 136 | 'placeholder' => __('Multiselect maan!', 'wc_cpdf'), 137 | 'options' => array( 138 | 'option_1' => 'Option 1', 139 | 'option_2' => 'Option 2', 140 | 'option_3' => 'Option 3', 141 | 'option_4' => 'Option 4', 142 | 'option_5' => 'Option 5' 143 | ), 144 | 'description' => __('Field description.', 'wc_cpdf'), 145 | 'desc_tip' => true, 146 | 'class' => 'medium' 147 | ), 148 | 149 | // image 150 | array( 151 | 'id' => '_myimage', 152 | 'type' => 'image', 153 | 'label' => __('Image 1', 'wc_cpdf'), 154 | 'description' => __('Field description.', 'wc_cpdf'), 155 | 'desc_tip' => true, 156 | ), 157 | 158 | array( 159 | 'id' => '_mygallery', 160 | 'type' => 'gallery', 161 | 'label' => __('Gallery', 'wc_cpdf'), 162 | 'description' => __('Field description.', 'wc_cpdf'), 163 | 'desc_tip' => true, 164 | ), 165 | 166 | // Color 167 | array( 168 | 'id' => '_mycolor', 169 | 'type' => 'color', 170 | 'label' => __('Select color', 'wc_cpdf'), 171 | 'placeholder' => __('A placeholder text goes here.', 'wc_cpdf'), 172 | 'class' => 'large', 173 | 'description' => __('Field description.', 'wc_cpdf'), 174 | 'desc_tip' => true, 175 | ), 176 | 177 | // Datepicker 178 | 179 | array( 180 | 'id' => '_mydatepicker', 181 | 'type' => 'datepicker', 182 | 'label' => __('Select date', 'wc_cpdf'), 183 | 'placeholder' => __('A placeholder text goes here.', 'wc_cpdf'), 184 | 'class' => 'large', 185 | 'description' => __('Field description.', 'wc_cpdf'), 186 | 'desc_tip' => true, 187 | ), 188 | 189 | array( 190 | 'type' => 'divider' 191 | ) 192 | 193 | ); 194 | 195 | /** First product data tab ends **/ 196 | /** ===================================== */ 197 | 198 | 199 | /** Second product data tab starts **/ 200 | /** ===================================== */ 201 | 202 | $custom_product_data_fields['custom_data_2'] = array( 203 | 204 | array( 205 | 'tab_name' => __('Custom Data 2', 'wc_cpdf'), 206 | ), 207 | 208 | array( 209 | 'id' => '_mytext_2', 210 | 'type' => 'text', 211 | 'label' => __('Text ABCD', 'wc_cpdf'), 212 | 'placeholder' => __('A placeholder text goes here.', 'wc_cpdf'), 213 | 'class' => 'large', 214 | 'description' => __('Field description.', 'wc_cpdf'), 215 | 'desc_tip' => true, 216 | ) 217 | 218 | ); 219 | 220 | return $custom_product_data_fields; 221 | 222 | } 223 | 224 | endif; 225 | 226 | 227 | 228 | 229 | = Getting The Field Value = 230 | 231 | 232 | /** 233 | * 234 | * $wc_cpdf->get_value($post_id, $field_id); 235 | * $post_id = (integer) post ID 236 | * $field_id = (string) unique field ID 237 | * 238 | */ 239 | 240 | global $wc_cpdf; 241 | echo $wc_cpdf->get_value(get_the_ID(), '_mytext'); 242 | 243 | 244 | 245 | Retrieving multiselect value 246 | 247 |
    248 | global $wc_cpdf;
    249 | $multiselect = $wc_cpdf->get_value($post->ID, '_mymultiselect');
    250 | foreach ($multiselect as $value) {
    251 |   echo $value;
    252 | }
    253 | 
    254 | 255 | Retrieving image value 256 | 257 |
    258 | global $wc_cpdf;
    259 | $image_id = $wc_cpdf->get_value($post->ID, '_myimage');
    260 | $size = 'thumbnail';
    261 | $image_attachment = wp_get_attachment_image($image_id, $size);
    262 | echo $image_attachment;
    263 | 
    264 | 265 | Retrieving gallery value 266 | 267 |
    268 | global $wc_cpdf;
    269 | $gallery = $wc_cpdf->get_value($post->ID, '_mygallery');
    270 | foreach ($gallery as $image_id) {
    271 | 
    272 |   $image_id = $wc_cpdf->get_value($post->ID, '_mygallery');
    273 |   $size = 'thumbnail';
    274 |   $image_attachment = wp_get_attachment_image($image_id, $size);
    275 | 
    276 |   echo $image_attachment;
    277 | 
    278 | }
    279 | 
    280 | 281 | 282 | This project is also available on [Github](https://github.com/kharissulistiyo/WooCommerce-Custom-Product-Data-Fields). 283 | 284 | = More info = 285 | 286 | Send me your question to my contacts below: 287 | 288 | Mail: [kharisulistiyo(at)gmail(dot)com](mailto:kharisulistiyo@gmail.com) 289 | Twitter: [@kharissulistiyo](http://twitter.com/kharissulistiyo) 290 | 291 | P.S: Don't be worry, I always reply. :) 292 | 293 | 294 | == Installation == 295 | 296 | 1. Upload this plugin to the /wp-content/plugins/ directory. 297 | 2. Activate the plugin through the Plugins menu in WordPress. 298 | 3. Define your custom product data fields in your theme `functions.php` file. See `fields-init.php` inside this plugin folder. 299 | 300 | == Screenshots == 301 | 302 | 1. WooCommerce Custom Product Data Fields 303 | 304 | == Changelog == 305 | 306 | = 1.0.2 = 307 | * Fix some bugs 308 | * Add multiselect field 309 | * Add image field 310 | * Add image field 311 | * Add gallery field 312 | * Add colorpicker field 313 | * Add datepicker field 314 | * Add devider 315 | * Allow multiple data tabs definition 316 | * Update fields definition function (via hook) 317 | 318 | = 1.0 = 319 | * Initial release 320 | -------------------------------------------------------------------------------- /class-wc-product-data-fields.php: -------------------------------------------------------------------------------- 1 | wc_cpdf_fields(); 101 | 102 | if($fields == null){ 103 | return; 104 | } 105 | 106 | foreach ($fields as $key => $fields_array){ 107 | 108 | foreach ($fields_array as $field) { 109 | if(isset($field['tab_name']) && $field['tab_name'] != ''){ 110 | $href = "#".$key; 111 | echo "
  • ".$field['tab_name']."
  • "; 112 | } 113 | } 114 | 115 | } 116 | 117 | 118 | } 119 | 120 | 121 | /** 122 | * Adds the panel to the Product Data postbox in the product interface 123 | * 124 | * @return string 125 | */ 126 | public function product_write_panel(){ 127 | 128 | global $post; 129 | 130 | // Pull the field data out of the database 131 | $available_fields = array(); 132 | $available_fields[] = maybe_unserialize(get_post_meta($post->ID, 'wc_productdata_options', true)); 133 | 134 | if($available_fields){ 135 | 136 | // Display fields panel 137 | foreach($available_fields as $available_field){ 138 | 139 | $fields = $this->wc_cpdf_fields(); 140 | 141 | if($fields == null){ 142 | return; 143 | } 144 | 145 | 146 | foreach ($fields as $key => $fields_array){ 147 | 148 | echo '
    '; 149 | 150 | foreach ($fields_array as $field) { 151 | 152 | if(!$field['tab_name']){ 153 | 154 | WC_Product_Data_Fields::wc_product_data_options_fields($field); 155 | 156 | } 157 | 158 | } 159 | 160 | echo '
    '; 161 | 162 | } 163 | 164 | 165 | 166 | 167 | } 168 | 169 | } 170 | 171 | 172 | } 173 | 174 | 175 | /** 176 | * Create Fields 177 | * 178 | * @param $field array 179 | * @return string 180 | */ 181 | public function wc_product_data_options_fields($field){ 182 | global $thepostid, $post, $woocommerce; 183 | 184 | $fieldtype = isset( $field['type'] ) ? $field['type'] : ''; 185 | $field_id = isset( $field['id'] ) ? $field['id'] : ''; 186 | 187 | $thepostid = empty( $thepostid ) ? $post->ID : $thepostid; 188 | 189 | 190 | $options_data = maybe_unserialize(get_post_meta($thepostid, 'wc_productdata_options', true)); 191 | 192 | switch($fieldtype){ 193 | 194 | case 'text': 195 | $thepostid = empty( $thepostid ) ? $post->ID : $thepostid; 196 | $field['placeholder'] = isset( $field['placeholder'] ) ? $field['placeholder'] : ''; 197 | $field['class'] = isset( $field['class'] ) ? $field['class'] : 'short'; 198 | $field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : ''; 199 | $field['value'] = isset( $field['value'] ) ? $field['value'] : get_post_meta( $thepostid, $field['id'], true ); 200 | $field['name'] = isset( $field['name'] ) ? $field['name'] : $field['id']; 201 | $field['type'] = isset( $field['type'] ) ? $field['type'] : 'text'; 202 | 203 | $inputval = isset( $options_data[0][$field_id] ) ? $options_data[0][$field_id] : ''; 204 | 205 | echo '

    '; 206 | 207 | if ( ! empty( $field['description'] ) ) { 208 | 209 | if ( isset( $field['desc_tip'] ) && false !== $field['desc_tip'] ) { 210 | echo ''; 211 | } else { 212 | echo '' . wp_kses_post( $field['description'] ) . ''; 213 | } 214 | 215 | } 216 | 217 | echo '

    '; 218 | break; 219 | 220 | case 'number': 221 | $thepostid = empty( $thepostid ) ? $post->ID : $thepostid; 222 | $field['placeholder'] = isset( $field['placeholder'] ) ? $field['placeholder'] : ''; 223 | $field['class'] = isset( $field['class'] ) ? $field['class'] : 'short'; 224 | $field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : ''; 225 | $field['value'] = isset( $field['value'] ) ? $field['value'] : get_post_meta( $thepostid, $field['id'], true ); 226 | $field['name'] = isset( $field['name'] ) ? $field['name'] : $field['id']; 227 | $field['type'] = isset( $field['type'] ) ? $field['type'] : 'text'; 228 | 229 | $inputval = isset( $options_data[0][$field_id] ) ? $options_data[0][$field_id] : ''; 230 | 231 | echo '

    '; 232 | 233 | if ( ! empty( $field['description'] ) ) { 234 | 235 | if ( isset( $field['desc_tip'] ) && false !== $field['desc_tip'] ) { 236 | echo ''; 237 | } else { 238 | echo '' . wp_kses_post( $field['description'] ) . ''; 239 | } 240 | 241 | } 242 | 243 | echo '

    '; 244 | break; 245 | 246 | case 'textarea': 247 | if(!$thepostid) $thepostid = $post->ID; 248 | if(!isset($field['placeholder'])) $field['placeholder'] = ''; 249 | if(!isset($field['class'])) $field['class'] = 'short'; 250 | if(!isset($field['value'])) $field['value'] = get_post_meta($thepostid, $field['id'], true); 251 | 252 | $inputval = isset( $options_data[0][$field_id] ) ? $options_data[0][$field_id] : ''; 253 | 254 | echo '

    '; 255 | 256 | if ( ! empty( $field['description'] ) ) { 257 | 258 | if ( isset( $field['desc_tip'] ) && false !== $field['desc_tip'] ) { 259 | echo ''; 260 | } else { 261 | echo '' . wp_kses_post( $field['description'] ) . ''; 262 | } 263 | 264 | } 265 | 266 | echo '

    '; 267 | break; 268 | 269 | 270 | case 'checkbox': 271 | $thepostid = empty( $thepostid ) ? $post->ID : $thepostid; 272 | $field['class'] = isset( $field['class'] ) ? $field['class'] : 'checkbox'; 273 | $field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : ''; 274 | $field['value'] = isset( $options_data[0][$field_id] ) ? $options_data[0][$field_id] : ''; 275 | $field['cbvalue'] = isset( $field['cbvalue'] ) ? $field['cbvalue'] : 'yes'; 276 | $field['name'] = isset( $field['name'] ) ? $field['name'] : $field['id']; 277 | 278 | echo '

    '; 279 | 280 | if ( ! empty( $field['description'] ) ) { 281 | 282 | if ( isset( $field['desc_tip'] ) && false !== $field['desc_tip'] ) { 283 | echo ''; 284 | } else { 285 | echo '' . wp_kses_post( $field['description'] ) . ''; 286 | } 287 | 288 | } 289 | 290 | echo '

    '; 291 | break; 292 | 293 | case 'select': 294 | $thepostid = empty( $thepostid ) ? $post->ID : $thepostid; 295 | $field['class'] = isset( $field['class'] ) ? $field['class'] : 'select short'; 296 | $field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : ''; 297 | $field['value'] = isset( $options_data[0][$field_id] ) ? $options_data[0][$field_id] : ''; 298 | 299 | echo '

    '; 308 | 309 | if ( ! empty( $field['description'] ) ) { 310 | 311 | if ( isset( $field['desc_tip'] ) && false !== $field['desc_tip'] ) { 312 | echo ''; 313 | } else { 314 | echo '' . wp_kses_post( $field['description'] ) . ''; 315 | } 316 | 317 | } 318 | echo '

    '; 319 | break; 320 | 321 | 322 | case 'radio': 323 | $thepostid = empty( $thepostid ) ? $post->ID : $thepostid; 324 | $field['class'] = isset( $field['class'] ) ? $field['class'] : 'select short'; 325 | $field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : ''; 326 | $field['value'] = isset( $options_data[0][$field_id] ) ? $options_data[0][$field_id] : ''; 327 | $field['name'] = isset( $field['name'] ) ? $field['name'] : $field['id']; 328 | 329 | echo '
    ' . wp_kses_post( $field['label'] ) . ''; 343 | 344 | if ( ! empty( $field['description'] ) ) { 345 | 346 | if ( isset( $field['desc_tip'] ) && false !== $field['desc_tip'] ) { 347 | echo ''; 348 | } else { 349 | echo '' . wp_kses_post( $field['description'] ) . ''; 350 | } 351 | 352 | } 353 | 354 | echo '
    '; 355 | break; 356 | 357 | 358 | case 'hidden': 359 | $thepostid = empty( $thepostid ) ? $post->ID : $thepostid; 360 | $field['value'] = isset( $field['value'] ) ? $field['value'] : $options_data[0][$field_id]; 361 | $field['class'] = isset( $field['class'] ) ? $field['class'] : ''; 362 | 363 | echo ' '; 364 | 365 | break; 366 | 367 | 368 | case 'multiselect': 369 | 370 | global $wc_cpdf; 371 | 372 | if(!$thepostid) $thepostid = $post->ID; 373 | if(!isset($field['placeholder'])) $field['placeholder'] = ''; 374 | if(!isset($field['class'])) $field['class'] = 'short'; 375 | if(!isset($field['value'])) $field['value'] = get_post_meta($thepostid, $field['id'], true); 376 | 377 | $inputval = isset( $options_data[0][$field_id] ) ? $options_data[0][$field_id] : ''; 378 | 379 | $html = '

    '; 380 | 381 | $html .= ''; 382 | 383 | $html .= ''; 394 | 395 | if ( ! empty( $field['description'] ) ) { 396 | 397 | if ( isset( $field['desc_tip'] ) && false !== $field['desc_tip'] ) { 398 | $html .= ''; 399 | } else { 400 | $html .= '' . wp_kses_post( $field['description'] ) . ''; 401 | } 402 | 403 | } 404 | 405 | $html .= '

    '; 406 | 407 | echo $html; 408 | 409 | break; 410 | 411 | 412 | case 'image': 413 | 414 | global $wc_cpdf; 415 | 416 | $saved_image = $wc_cpdf->get_value($thepostid, $field['id']); 417 | $saved_image_url = wp_get_attachment_image_src($saved_image); 418 | $saved_image_url_thumb = wp_get_attachment_image_src($saved_image, 'thumbnail', true); 419 | 420 | ?> 421 | 422 |
    423 | 424 |
    425 | 426 | '.$field['label'].''; ?> 427 | 428 |
    429 | 430 |
    431 | 432 |
    433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 |
    442 | 443 | 444 | 445 | 446 | 447 | plugin_url() ) . '/assets/images/help.png" height="16" width="16" />'; 452 | } else { 453 | echo '' . wp_kses_post( $field['description'] ) . ''; 454 | } 455 | 456 | } 457 | ?> 458 | 459 |
    460 | 461 |
    462 | 463 | get_value($thepostid, $field['id']); 472 | 473 | ?> 474 | 475 | 524 | 525 | ID : $thepostid; 532 | $field['placeholder'] = isset( $field['placeholder'] ) ? $field['placeholder'] : ''; 533 | $field['class'] = isset( $field['class'] ) ? $field['class'] : 'short'; 534 | $field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : ''; 535 | $field['value'] = isset( $field['value'] ) ? $field['value'] : get_post_meta( $thepostid, $field['id'], true ); 536 | $field['name'] = isset( $field['name'] ) ? $field['name'] : $field['id']; 537 | $field['type'] = isset( $field['type'] ) ? $field['type'] : 'text'; 538 | 539 | $inputval = isset( $options_data[0][$field_id] ) ? $options_data[0][$field_id] : ''; 540 | 541 | echo '

    '; 542 | 543 | if ( ! empty( $field['description'] ) ) { 544 | 545 | if ( isset( $field['desc_tip'] ) && false !== $field['desc_tip'] ) { 546 | echo ''; 547 | } else { 548 | echo '' . wp_kses_post( $field['description'] ) . ''; 549 | } 550 | 551 | } 552 | 553 | echo '

    '; 554 | 555 | break; 556 | 557 | 558 | case 'datepicker': 559 | 560 | $thepostid = empty( $thepostid ) ? $post->ID : $thepostid; 561 | $field['placeholder'] = isset( $field['placeholder'] ) ? $field['placeholder'] : ''; 562 | $field['class'] = isset( $field['class'] ) ? $field['class'] : 'short'; 563 | $field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : ''; 564 | $field['value'] = isset( $field['value'] ) ? $field['value'] : get_post_meta( $thepostid, $field['id'], true ); 565 | $field['name'] = isset( $field['name'] ) ? $field['name'] : $field['id']; 566 | $field['type'] = isset( $field['type'] ) ? $field['type'] : 'text'; 567 | 568 | $inputval = isset( $options_data[0][$field_id] ) ? $options_data[0][$field_id] : ''; 569 | 570 | echo '

    '; 571 | 572 | if ( ! empty( $field['description'] ) ) { 573 | 574 | if ( isset( $field['desc_tip'] ) && false !== $field['desc_tip'] ) { 575 | echo ''; 576 | } else { 577 | echo '' . wp_kses_post( $field['description'] ) . ''; 578 | } 579 | 580 | } 581 | 582 | echo '

    '; 583 | 584 | break; 585 | 586 | 587 | case 'divider': 588 | 589 | echo '
    '; 590 | 591 | break; 592 | 593 | 594 | } 595 | 596 | 597 | 598 | } 599 | 600 | 601 | /** 602 | * Saves the data inputed into the product boxes, as post meta data 603 | * identified by the name 'wc_productdata_options' 604 | * 605 | * @param int $post_id the post (product) identifier 606 | * @param stdClass $post the post (product) 607 | * @return void 608 | */ 609 | public function product_save_data($post_id, $post){ 610 | 611 | $options_value = array(); 612 | 613 | /** field name in pairs array **/ 614 | $data_args = array(); 615 | $fields = $this->wc_cpdf_fields(); 616 | 617 | if($fields == null){ 618 | return; 619 | } 620 | 621 | foreach ($fields as $key => $fields_array){ 622 | 623 | foreach ($fields_array as $data) { 624 | $name = $data['id']; 625 | $data_args[$data['id']] = maybe_unserialize($_POST[$data['id']]); 626 | } 627 | 628 | } 629 | 630 | $options_value[] = $data_args; 631 | 632 | // save the data to the database 633 | update_post_meta($post_id, 'wc_productdata_options', $options_value); 634 | 635 | } 636 | 637 | 638 | } 639 | 640 | } 641 | 642 | 643 | /** 644 | * Instantiate Class 645 | */ 646 | 647 | $wc_cpdf = new WC_Product_Data_Fields(); 648 | --------------------------------------------------------------------------------