In this post I was able to see the code that made a custom field with a link work very well.
Display a selected variation custom field in WooCommerce as a pdf linked file
My problem is that I need to have three fields in each variation with different links each downloading a different datasheet.
I've tried this code, but I can't get it to save the custom fields when saving the variation:
// 1. Add custom field input # Product Data > Variations > Single Variation
add_action( 'woocommerce_variation_options_pricing', 'add_custom_field_to_variations', 10, 3 );
function add_custom_field_to_variations( $loop, $variation_data, $variation ) {
woocommerce_wp_text_input( array(
'id' => 'ficha[' . $loop . ']',
'class' => 'short',
'label' => __( 'ficha', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, 'ficha', true )
) );
woocommerce_wp_text_input( array(
'id' => 'diagrama[' . $loop . ']',
'class' => 'short',
'label' => __( 'diagrama', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, 'diagrama', true )
) );
woocommerce_wp_text_input( array(
'id' => 'esquema[' . $loop . ']',
'class' => 'short',
'label' => __( 'esquema', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, 'custom_field', true )
) );
}
// -----------------------------------------
// 2. Save custom field on product variation save
add_action( 'woocommerce_save_product_variation', 'save_custom_field_variations', 10, 2 );
function save_custom_field_variations( $variation_id, $i )
{
$custom_field = $_POST['ficha'][$i];
if ( isset( $custom_field ) ) update_post_meta( $variation_id, 'ficha', esc_attr( $custom_field ) );
}
{
$custom_field = $_POST['diagrama'][$i];
if ( isset( $custom_field ) ) update_post_meta( $variation_id, 'diagrama', esc_attr( $custom_field ) );
}
{
$custom_field = $_POST['custom_field'][$i];
if ( isset( $custom_field ) ) update_post_meta( $variation_id, 'esquema', esc_attr( $custom_field ) );
}
// -----------------------------------------
// 3. Store custom field value into variation data
add_filter( 'woocommerce_available_variation', 'add_custom_field_value_to_variation_data', 10, 3 );
function add_custom_field_value_to_variation_data( $variation_data, $product, $variation ) {
$variation_data['custom_field'] = $variation->get_meta('custom_field');
if( ! empty($variation_data['custom_field']) ) {
$variation_data['custom_field_html'] = '<div class="woocommerce-custom_field"><img src="https://www.tualuce.eu/wp-content/uploads/2022/07/pdfimage-e1657626977128.png" width="50" height="50" alt="pdf_logo.png" title="' . __("External product link", "woocommerce") . '" />' . __(" Ficha tecnica", "woocommerce") . '</div>';
}
if( ! empty($variation_data['custom_field']) ) {
$variation_data['custom_field_html'] = '<div class="woocommerce-custom_field"><img src="https://www.tualuce.eu/wp-content/uploads/2022/07/pdfimage-e1657626977128.png" width="50" height="50" alt="pdf_logo.png" title="' . __("External product link", "woocommerce") . '" />' . __(" Diagrama", "woocommerce") . '</div>';
}
if( ! empty($variation_data['custom_field']) ) {
$variation_data['custom_field_html'] = '<div class="woocommerce-custom_field"><img src="https://www.tualuce.eu/wp-content/uploads/2022/05/visibility-e1653592380924.png" width="50" height="50" alt="pdf_logo.png" title="' . __("External product link", "woocommerce") . '" />' . __(" Esquema", "woocommerce") . '</div>';
}
return $variation_data;
}
I have this code, based on Display custom field values of product variations to custom product tab in WooCommerce to display 3 custom field on WooCommerce variations.
In that fields I want to insert text with HTML tags and show it in shortcode with all HTML styling (tables etc...) but current code doesn't output styled text but raw text with printed HTML tags:
<div>some text etc...</div><a>some link</a>
Any suggestions what I did wrong here?
/* Add custom field input # Product Data > Variations > Single Variation */
add_action( 'woocommerce_variation_options', 'add_custom_field_cage_code_to_variations', 10, 3 );
function add_custom_field_cage_code_to_variations( $loop, $variation_data, $variation ) {
echo '<div class="cage_code_options_group options_group">';
woocommerce_wp_text_input( array(
'id' => 'cage_code[' . $loop . ']',
'label' => __( 'One-line description', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, 'cage_code', true )
));
woocommerce_wp_textarea_input( array(
'id' => 'cage_code_part_number[' . $loop . ']',
'label' => __( 'Short Description', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, 'cage_code_part_number', true )
));
woocommerce_wp_textarea_input( array(
'id' => 'cage_code_niin_nsn_number[' . $loop . ']',
'label' => __( 'Long description', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, 'cage_code_niin_nsn_number', true )
));
echo '</div>';
}
/* Save custom field on product variation save */
add_action( 'woocommerce_save_product_variation', 'magazine_save_custom_field_variations', 10, 2 );
function magazine_save_custom_field_variations( $variation_id, $i ) {
$cage_code = $_POST['cage_code'][$i];
if ( isset( $cage_code ) ) update_post_meta( $variation_id, 'cage_code', esc_attr(
$cage_code ) );
$cage_code_part_number = $_POST['cage_code_part_number'][$i];
if ( isset( $cage_code ) ) update_post_meta( $variation_id, 'cage_code_part_number',
esc_attr( $cage_code_part_number ) );
$cage_code_niin_nsn_number = $_POST['cage_code_niin_nsn_number'][$i];
if ( isset( $cage_code_niin_nsn_number ) ) update_post_meta( $variation_id,
'cage_code_niin_nsn_number', esc_attr( $cage_code_niin_nsn_number ) );
}
function woo_cage_code_info_tab_content() {
global $product;
if ( $product->is_type( 'variable' ) ) {
// Loop through the variation IDs
foreach( $product->get_children() as $key => $variation_id ) {
// Get an instance of the WC_Product_Variation Object
$variation = wc_get_product( $variation_id );
// Get meta
$cage_code = $variation->get_meta( 'cage_code' );
$cage_code_part_number = $variation->get_meta( 'cage_code_part_number' );
$cage_code_niin_nsn_number = $variation->get_meta(
'cage_code_niin_nsn_number' );
// Output
echo '<div class="woo_cage_code_info_tab_content
woo_cage_code_info_tab_content-' . $variation_id .'">';
if ( $cage_code ) {
echo '<p>' . $cage_code . '</p>';
}
echo '</div>';
}
?>
<script>
jQuery(document).ready(function($) {
// Hide all
$( '.woo_cage_code_info_tab_content' ).css( 'display', 'none' );
// Change
$( 'input.variation_id' ).change( function() {
// Hide all
$( '.woo_cage_code_info_tab_content' ).css( 'display', 'none' );
if( $( 'input.variation_id' ).val() != '' ) {
var var_id = $( 'input.variation_id' ).val();
// Display current
$( '.woo_cage_code_info_tab_content-' + var_id ).css( 'display', 'block' );
}
});
});
</script>
<?php
}
}
add_shortcode('woo_cage_code_info_tab_content', 'woo_cage_code_info_tab_content');
Your code contains some minor mistakes. You can use woocommerce_wp_text_input() or woocommerce_wp_textarea_input() without issues.
However, do not use esc_attr() when saving because it's for escaping HTML attributes (I assume that you as admin only have access to the backend fields) OR if you do use it, convert HTML entities back to characters at the output. In this answer I have used wp_kses_post() and wp_unslash()
The woocommerce_admin_process_variation_object replaces the outdated woocommerce_save_product_variation hook
So you get:
// Add field(s)
function action_woocommerce_variation_options( $loop, $variation_data, $variation ) {
echo '<div class="cage_code_options_group options_group">';
woocommerce_wp_text_input(
array(
'id' => 'cage_code[' . $loop . ']',
'label' => __( 'One-line description', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, 'cage_code', true )
)
);
woocommerce_wp_textarea_input(
array(
'id' => 'cage_code_part_number[' . $loop . ']',
'label' => __( 'Short Description', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, 'cage_code_part_number', true )
)
);
woocommerce_wp_textarea_input(
array(
'id' => 'cage_code_niin_nsn_number[' . $loop . ']',
'label' => __( 'Long description', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, 'cage_code_niin_nsn_number', true )
)
);
echo '</div>';
}
add_action( 'woocommerce_variation_options', 'action_woocommerce_variation_options', 10, 3 );
// Save
function action_woocommerce_admin_process_variation_object( $variation, $i ) {
// Isset
if ( isset( $_POST['cage_code'][$i] ) ) {
// Store
$cage_code = wp_kses_post( wp_unslash( $_POST['cage_code'][$i] ) );
// Update
$variation->update_meta_data( 'cage_code', $cage_code );
}
// Isset
if ( isset( $_POST['cage_code_part_number'][$i] ) ) {
// Store
$cage_code_part_number = wp_kses_post( wp_unslash( $_POST['cage_code_part_number'][$i] ) );
// Update
$variation->update_meta_data( 'cage_code_part_number', $cage_code_part_number );
}
// Isset
if ( isset( $_POST['cage_code_niin_nsn_number'][$i] ) ) {
// Store
$cage_code_niin_nsn_number = wp_kses_post( wp_unslash( $_POST['cage_code_niin_nsn_number'][$i] ) );
// Update
$variation->update_meta_data( 'cage_code_niin_nsn_number', $cage_code_niin_nsn_number );
}
}
add_action( 'woocommerce_admin_process_variation_object', 'action_woocommerce_admin_process_variation_object', 10, 2 );
Then I have no idea why you would use a shortcode versus a hook to render, like I did this in my answer. This can be any hook as long as it applies to the single product page:
// Display
function filter_woocommerce_product_tabs( $tabs ) {
// Adds the new tab
$tabs['cage_code_information_tab'] = array(
'title' => __( 'Cage Code Information', 'woocommerce' ),
'priority' => 50,
'callback' => 'woo_cage_code_info_tab_content'
);
return $tabs;
}
add_filter( 'woocommerce_product_tabs', 'filter_woocommerce_product_tabs', 100, 1 );
function woo_cage_code_info_tab_content() {
global $product;
if ( $product->is_type( 'variable' ) ) {
// Loop through the variation IDs
foreach( $product->get_children() as $key => $variation_id ) {
// Get an instance of the WC_Product_Variation Object
$variation = wc_get_product( $variation_id );
// Get meta
$cage_code = $variation->get_meta( 'cage_code' );
$cage_code_part_number = $variation->get_meta( 'cage_code_part_number' );
$cage_code_niin_nsn_number = $variation->get_meta( 'cage_code_niin_nsn_number' );
// Output
echo '<div class="woo_cage_code_info_tab_content woo_cage_code_info_tab_content-' . $variation_id .'">';
if ( $cage_code ) {
echo '<p>Cage code: ' . $cage_code . '</p>';
}
if ( $cage_code_part_number ) {
echo '<p>Cage code part number: ' . $cage_code_part_number . '</p>';
}
if ( $cage_code_niin_nsn_number ) {
echo '<p>Cage code niin nsn_number: ' . $cage_code_niin_nsn_number . '</p>';
}
echo '</div>';
}
?>
<script>
jQuery(document).ready(function($) {
// Hide all
$( '.woo_cage_code_info_tab_content' ).css( 'display', 'none' );
// Change
$( 'input.variation_id' ).change( function() {
// Hide all
$( '.woo_cage_code_info_tab_content' ).css( 'display', 'none' );
if( $( 'input.variation_id' ).val() != '' ) {
var var_id = $( 'input.variation_id' ).val();
// Display current
$( '.woo_cage_code_info_tab_content-' + var_id ).css( 'display', 'block' );
}
});
});
</script>
<?php
}
}
I have a theme for a blog in which im trying to modify, so that it always shows the "published date" instead for "Edited on" every time I edit it...
I have nailed it down to the file -> functions.php where it displays the dates... Section "Posted On Function"
But everytime i try to modify the if/else i get a error 500 on the page :-(
Some help would be highly appreciated here...
Thanks in advance!
-Best regards
Part that I suspect is the reason :
-------------------------------------------------------------------------------------------------------
Posted On Function
-------------------------------------------------------------------------------------------------------
*/
function swell_posted_on() {
if ( get_the_modified_time() != get_the_time() ) {
printf( __( '<span class="%1$s">Last Updated:</span> %2$s', 'organic-swell' ),
'meta-prep meta-prep-author',
sprintf( '<span class="entry-date">%3$s</span>',
esc_url( get_permalink() ),
esc_attr( get_the_modified_time() ),
get_the_modified_date()
)
);
} else {
printf( __( '<span class="%1$s">Posted:</span> %2$s', 'organic-swell' ),
'meta-prep meta-prep-author',
sprintf( '<span class="entry-date">%3$s</span>',
esc_url( get_permalink() ),
esc_attr( get_the_time() ),
get_the_date()
)
);
}
}
/*
This is the entire functions.php file :
/*
-------------------------------------------------------------------------------------------------------
Theme Setup
-------------------------------------------------------------------------------------------------------
*/
if ( ! function_exists( 'swell_setup' ) ) :
function swell_setup() {
// Make theme available for translation.
load_theme_textdomain( 'organic-swell', get_template_directory() . '/languages' );
// Add default posts and comments RSS feed links to head.
add_theme_support( 'automatic-feed-links' );
// Enable support for Post Thumbnails.
add_theme_support( 'post-thumbnails' );
// Enable support for site title tag.
add_theme_support( 'title-tag' );
add_image_size( 'swell-featured-large', 1800, 1200, true ); // Large Featured Image.
add_image_size( 'swell-featured-medium', 1200, 800, true ); // Medium Featured Image.
add_image_size( 'swell-featured-small', 640, 640, true ); // Small Featured Image.
// Post Formats.
add_theme_support( 'post-formats', array(
'gallery',
'link',
'image',
'audio',
'status',
'quote',
'video',
)
);
// Create Menus.
register_nav_menus( array(
'fixed-menu' => esc_html__( 'Fixed Menu', 'organic-swell' ),
'main-menu' => esc_html__( 'Main Menu', 'organic-swell' ),
'social-menu' => esc_html__( 'Social Menu', 'organic-swell' ),
));
// Custom Header.
register_default_headers( array(
'default' => array(
'url' => get_template_directory_uri() . '/images/default-header.jpg',
'thumbnail_url' => get_template_directory_uri() . '/images/default-header.jpg',
'description' => esc_html__( 'Default Custom Header', 'organic-swell' ),
),
));
$defaults = array(
'width' => 1800,
'height' => 480,
'flex-height' => true,
'flex-width' => true,
'default-text-color' => 'ffffff',
'default-image' => get_template_directory_uri() . '/images/default-header.jpg',
'header-text' => false,
'uploads' => true,
);
add_theme_support( 'custom-header', $defaults );
// Custom Background.
$defaults = array(
'default-color' => 'eeeeee',
);
add_theme_support( 'custom-background', $defaults );
}
endif; // swell_setup
add_action( 'after_setup_theme', 'swell_setup' );
/*
-------------------------------------------------------------------------------------------------------
Theme Updater
-------------------------------------------------------------------------------------------------------
*/
function swell_theme_updater() {
require( get_template_directory() . '/updater/theme-updater.php' );
}
add_action( 'after_setup_theme', 'swell_theme_updater' );
/*
-------------------------------------------------------------------------------------------------------
Category ID to Name
-------------------------------------------------------------------------------------------------------
*/
function swell_cat_id_to_name( $id ) {
$cat = get_category( $id );
if ( is_wp_error( $cat ) ) {
return false; }
return $cat->cat_name;
}
/*
-------------------------------------------------------------------------------------------------------
Register Scripts
-------------------------------------------------------------------------------------------------------
*/
if ( ! function_exists( 'swell_enqueue_scripts' ) ) {
function swell_enqueue_scripts() {
// Enqueue Styles.
wp_enqueue_style( 'swell-style', get_stylesheet_uri() );
wp_enqueue_style( 'swell-style-mobile', get_template_directory_uri() . '/css/style-mobile.css', array( 'swell-style' ), '1.0' );
// Resgister Scripts.
wp_register_script( 'swell-fitvids', get_template_directory_uri() . '/js/jquery.fitvids.js', array( 'jquery' ), '20130729' );
wp_register_script( 'swell-hover', get_template_directory_uri() . '/js/hoverIntent.js', array( 'jquery' ), '20130729' );
wp_register_script( 'swell-superfish', get_template_directory_uri() . '/js/superfish.js', array( 'jquery', 'swell-hover' ), '20130729' );
// Enqueue Scripts.
wp_enqueue_script( 'swell-custom', get_template_directory_uri() . '/js/jquery.custom.js', array( 'jquery', 'swell-superfish', 'swell-fitvids', 'masonry' ), '20130729', true );
wp_enqueue_script( 'swell-navigation', get_template_directory_uri() . '/js/navigation.js', array(), '20130729', true );
// Load Flexslider on front page and slideshow page template.
if ( is_home() || is_front_page() || is_single() || is_page_template( 'template-slideshow.php' ) || is_page_template( 'template-featured-content.php' ) ) {
wp_enqueue_script( 'swell-flexslider', get_template_directory_uri() . '/js/jquery.flexslider.js', array( 'jquery' ), '20130729' );
}
// Load single scripts only on single pages.
if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
wp_enqueue_script( 'comment-reply' );
}
}
}
add_action( 'wp_enqueue_scripts', 'swell_enqueue_scripts' );
/*
-------------------------------------------------------------------------------------------------------
Register Sidebars
-------------------------------------------------------------------------------------------------------
*/
function swell_widgets_init() {
register_sidebar(array(
'name' => esc_html__( 'Default Sidebar', 'organic-swell' ),
'id' => 'default-sidebar',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h6 class="title">',
'after_title' => '</h6>',
));
register_sidebar(array(
'name' => esc_html__( 'Blog Sidebar', 'organic-swell' ),
'id' => 'blog-sidebar',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h6 class="title">',
'after_title' => '</h6>',
));
register_sidebar(array(
'name' => esc_html__( 'Left Sidebar', 'organic-swell' ),
'id' => 'left-sidebar',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h6 class="title">',
'after_title' => '</h6>',
));
register_sidebar(array(
'name' => esc_html__( 'Footer Widgets', 'organic-swell' ),
'id' => 'footer',
'before_widget' => '<div id="%1$s" class="widget %2$s"><div class="footer-widget">',
'after_widget' => '</div></div>',
'before_title' => '<h6 class="title">',
'after_title' => '</h6>',
));
}
add_action( 'widgets_init', 'swell_widgets_init' );
/*
-------------------------------------------------------------------------------------------------------
Add Stylesheet To Visual Editor
-------------------------------------------------------------------------------------------------------
*/
add_action( 'widgets_init', 'swell_add_editor_styles' );
/**
* Apply theme's stylesheet to the visual editor.
*
* #uses add_editor_style() Links a stylesheet to visual editor
* #uses get_stylesheet_uri() Returns URI of theme stylesheet
*/
function swell_add_editor_styles() {
add_editor_style( 'css/style-editor.css' );
}
/*
-------------------------------------------------------------------------------------------------------
Posted On Function
-------------------------------------------------------------------------------------------------------
*/
function swell_posted_on() {
if ( get_the_modified_time() != get_the_time() ) {
printf( __( '<span class="%1$s">Last Updated:</span> %2$s', 'organic-swell' ),
'meta-prep meta-prep-author',
sprintf( '<span class="entry-date">%3$s</span>',
esc_url( get_permalink() ),
esc_attr( get_the_modified_time() ),
get_the_modified_date()
)
);
} else {
printf( __( '<span class="%1$s">Posted:</span> %2$s', 'organic-swell' ),
'meta-prep meta-prep-author',
sprintf( '<span class="entry-date">%3$s</span>',
esc_url( get_permalink() ),
esc_attr( get_the_time() ),
get_the_date()
)
);
}
}
/*
-------------------------------------------------------------------------------------------------------
Post Format Meta Boxes
-------------------------------------------------------------------------------------------------------
*/
add_action( 'admin_init', 'create_metaboxes' );
add_action( 'save_post', 'save_metaboxes' );
$metaboxes = array(
'link_url' => array(
'title' => esc_html__( 'Link Information', 'organic-swell' ),
'applicableto' => 'post',
'location' => 'side',
'display_condition' => 'post-format-link',
'priority' => 'default',
'fields' => array(
'l_url' => array(
'title' => esc_html__( 'Link URL: ', 'organic-swell' ),
'type' => 'text',
'description' => '',
'size' => 20,
),
),
),
'quote_author' => array(
'title' => esc_html__( 'Quote Author', 'organic-swell' ),
'applicableto' => 'post',
'location' => 'side',
'display_condition' => 'post-format-quote',
'priority' => 'default',
'fields' => array(
'q_author' => array(
'title' => esc_html__( 'Author: ', 'organic-swell' ),
'type' => 'text',
'description' => '',
'size' => 20,
),
),
),
);
function create_metaboxes() {
global $metaboxes;
if ( ! empty( $metaboxes ) ) {
foreach ( $metaboxes as $id => $metabox ) {
add_meta_box( $id, $metabox['title'], 'show_metaboxes', $metabox['applicableto'], $metabox['location'], $metabox['priority'], $id );
}
}
}
function show_metaboxes( $post, $args ) {
global $metaboxes;
$custom = get_post_custom( $post->ID );
$fields = $tabs = $metaboxes[ $args['id'] ]['fields'];
/** Nonce */
$output = '<input type="hidden" name="post_format_meta_box_nonce" value="' . wp_create_nonce( basename( __FILE__ ) ) . '" />';
if ( sizeof( $fields ) ) {
foreach ( $fields as $id => $field ) {
switch ( $field['type'] ) {
default:
case 'text':
if ( isset( $custom[ $id ] ) ) {
$output .= '<label for="' . esc_attr( $id ) . '">' . $field['title'] . '</label><input id="' . esc_attr( $id ) . '" type="text" name="' . esc_attr( $id ) . '" value="' . $custom[ $id ][0] . '" size="' . $field['size'] . '" />';
} else {
$output .= '<label for="' . esc_attr( $id ) . '">' . $field['title'] . '</label><input id="' . esc_attr( $id ) . '" type="text" name="' . esc_attr( $id ) . '" value="" size="' . $field['size'] . '" />';
}
break;
}
}
}
echo $output;
}
function save_metaboxes( $post_id ) {
global $metaboxes;
// Verify nonce.
if ( isset( $_POST['post_format_meta_box_nonce'] ) && ! wp_verify_nonce( $_POST['post_format_meta_box_nonce'], basename( __FILE__ ) ) ) {
return $post_id; }
// Check autosave.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id; }
// Check permissions.
if ( 'page' == isset( $_POST['post_type'] ) ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return $post_id; }
} elseif ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
$post_type = get_post_type();
// Loop through fields and save the data.
foreach ( $metaboxes as $id => $metabox ) {
// Check if metabox is applicable for current post type.
if ( $metabox['applicableto'] == $post_type ) {
$fields = $metaboxes[ $id ]['fields'];
foreach ( $fields as $id => $field ) {
$old = get_post_meta( $post_id, $id, true );
$new = $_POST[ $id ];
if ( $new && $new != $old ) {
update_post_meta( $post_id, $id, $new );
} elseif ( '' == $new && $old || ! isset( $_POST[ $id ] ) ) {
delete_post_meta( $post_id, $id, $old );
}
}
}
}
}
add_action( 'admin_print_scripts', 'display_metaboxes', 1000 );
function display_metaboxes() {
global $metaboxes;
if ( get_post_type() == 'post' ) :
?>
<script type="text/javascript"> // <![CDATA[
$ = jQuery;
<?php
$formats = $ids = array();
foreach ( $metaboxes as $id => $metabox ) {
array_push( $formats, "'" . $metabox['display_condition'] . "': '" . $id . "'" );
array_push( $ids, '#' . $id );
}
?>
var formats = { <?php echo implode( ',', $formats );?> };
var ids = "<?php echo implode( ',', $ids ); ?>";
function displayMetaboxes() {
// Hide all post format metaboxes.
$(ids).hide();
// Get current post format.
var selectedElt = $("input[name='post_format']:checked").attr("id");
// If exists, fade in current post format metabox.
if ( formats[selectedElt] )
$("#" + formats[selectedElt]).fadeIn();
}
$(function() {
// Show/hide metaboxes on page load
displayMetaboxes();
// Show/hide metaboxes on change event
$("input[name='post_format']").change(function() {
displayMetaboxes();
});
});
// ]]></script>
<?php
endif;
}
/*
-------------------------------------------------------------------------------------------------------
Content Width
-------------------------------------------------------------------------------------------------------
*/
if ( ! isset( $content_width ) ) {
$content_width = 640; }
/**
* Adjust content_width value based on the presence of widgets
*/
function swell_content_width() {
if ( ! is_active_sidebar( 'post-sidebar' ) || is_active_sidebar( 'page-sidebar' ) || is_active_sidebar( 'blog-sidebar' ) ) {
global $content_width;
$content_width = 960;
}
}
add_action( 'template_redirect', 'swell_content_width' );
/*
-------------------------------------------------------------------------------------------------------
Comments Function
-------------------------------------------------------------------------------------------------------
*/
if ( ! function_exists( 'swell_comment' ) ) :
function swell_comment( $comment, $args, $depth ) {
$GLOBALS['comment'] = $comment;
switch ( $comment->comment_type ) :
case 'pingback' :
case 'trackback' :
?>
<li class="post pingback">
<p><?php esc_html_e( 'Pingback:', 'organic-swell' ); ?> <?php comment_author_link(); ?><?php edit_comment_link( esc_html__( 'Edit', 'organic-swell' ), '<span class="edit-link">', '</span>' ); ?></p>
<?php
break;
default :
?>
<li <?php comment_class(); ?> id="<?php echo esc_attr( 'li-comment-' . get_comment_ID() ); ?>">
<article id="<?php echo esc_attr( 'comment-' . get_comment_ID() ); ?>" class="comment">
<footer class="comment-meta">
<div class="comment-author vcard">
<?php
$avatar_size = 72;
if ( '0' != $comment->comment_parent ) {
$avatar_size = 48; }
echo get_avatar( $comment, $avatar_size );
/* translators: 1: comment author, 2: date and time */
printf( __( '%1$s <br/> %2$s <br/>', 'organic-swell' ),
sprintf( '<span class="fn">%s</span>', wp_kses_post( get_comment_author_link() ) ),
sprintf( '<time pubdate datetime="%2$s">%3$s</time>',
esc_url( get_comment_link( $comment->comment_ID ) ),
get_comment_time( 'c' ),
/* translators: 1: date, 2: time */
sprintf( __( '%1$s', 'organic-swell' ), get_comment_date(), get_comment_time() )
)
);
?>
</div><!-- .comment-author .vcard -->
</footer>
<div class="comment-content">
<?php if ( $comment->comment_approved == '0' ) : ?>
<em class="comment-awaiting-moderation"><?php esc_html_e( 'Your comment is awaiting moderation.', 'organic-swell' ); ?></em>
<br />
<?php endif; ?>
<?php comment_text(); ?>
<div class="reply">
<?php comment_reply_link( array_merge( $args, array( 'reply_text' => esc_html__( 'Reply', 'organic-swell' ), 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
</div><!-- .reply -->
<?php edit_comment_link( esc_html__( 'Edit', 'organic-swell' ), '<span class="edit-link">', '</span>' ); ?>
</div>
</article><!-- #comment-## -->
<?php
break;
endswitch;
}
endif; // Ends check for swell_comment().
/*
-------------------------------------------------------------------------------------------------------
Disable Comments On Pages Default
-------------------------------------------------------------------------------------------------------
*/
function swell_default_comments_off( $data ) {
if ( $data['post_type'] == 'page' && $data['post_status'] == 'auto-draft' ) {
$data['comment_status'] = 0;
}
return $data;
}
add_filter( 'wp_insert_post_data', 'swell_default_comments_off' );
/*
-------------------------------------------------------------------------------------------------------
Custom Excerpt Length
-------------------------------------------------------------------------------------------------------
*/
function swell_excerpt_length( $length ) {
return 38;
}
add_filter( 'excerpt_length', 'swell_excerpt_length', 999 );
function swell_excerpt_more( $more ) {
return '... <a class="read-more" href="'. get_permalink( get_the_ID() ) . '">'. esc_html__( 'Read More', 'organic-swell' ) .'</a>';
}
add_filter( 'excerpt_more', 'swell_excerpt_more' );
/*
-------------------------------------------------------------------------------------------------------
Add Excerpt To Pages
-------------------------------------------------------------------------------------------------------
*/
add_action( 'widgets_init', 'swell_add_excerpts_to_pages' );
function swell_add_excerpts_to_pages() {
add_post_type_support( 'page', 'excerpt' );
}
/*
-------------------------------------------------------------------------------------------------------
Custom Page Links
-------------------------------------------------------------------------------------------------------
*/
function swell_wp_link_pages_args_prevnext_add( $args ) {
global $page, $numpages, $more, $pagenow;
if ( ! $args['next_or_number'] == 'next_and_number' ) {
return $args; }
$args['next_or_number'] = 'number'; // Keep numbering for the main part
if ( ! $more ) {
return $args; }
if ( $page -1 ) { // There is a previous page
$args['before'] .= _wp_link_page( $page -1 )
. $args['link_before']. $args['previouspagelink'] . $args['link_after'] . '</a>'; }
if ( $page < $numpages ) { // There is a next page
$args['after'] = _wp_link_page( $page + 1 )
. $args['link_before'] . $args['nextpagelink'] . $args['link_after'] . '</a>'
. $args['after']; }
return $args;
}
add_filter( 'wp_link_pages_args', 'swell_wp_link_pages_args_prevnext_add' );
/*
-------------------------------------------------------------------------------------------------------
Featured Video Meta Box
-------------------------------------------------------------------------------------------------------
*/
add_action( 'admin_init', 'admin_init_featurevid' );
add_action( 'save_post', 'save_featurevid' );
function admin_init_featurevid() {
add_meta_box( 'featurevid-meta', esc_html__( 'Featured Video Embed Code', 'organic-swell' ), 'meta_options_featurevid', 'post', 'normal', 'high' );
}
function meta_options_featurevid() {
global $post;
$custom = get_post_custom( $post->ID );
$featurevid = isset( $custom['featurevid'] ) ? esc_attr( $custom['featurevid'][0] ) : '';
echo '<textarea name="featurevid" cols="60" rows="4" style="width:97.6%" />'.$featurevid.'</textarea>';
}
function save_featurevid( $post_id ) {
global $post;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
if ( isset( $_POST['featurevid'] ) ) {
update_post_meta( $post->ID, 'featurevid', $_POST['featurevid'] );
}
}
/*
-------------------------------------------------------------------------------------------------------
Remove First Gallery
-------------------------------------------------------------------------------------------------------
*/
function swell_remove_gallery( $content ) {
if ( is_page_template( 'template-slideshow.php' ) || has_post_format( 'gallery' ) || ( is_singular( 'jetpack-portfolio' ) && get_theme_mod( 'display_project_slideshow', false ) ) ) {
$content = preg_replace( '/\[gallery(.*?)ids=[^\]]+\]/', '', $content, 1 );
}
return $content;
}
add_filter( 'the_content', 'swell_remove_gallery' );
/*
-------------------------------------------------------------------------------------------------------
Home Link In Custom Menu
-------------------------------------------------------------------------------------------------------
*/
function home_page_menu_args( $args ) {
$args['show_home'] = true;
return $args;
}
add_filter( 'wp_page_menu_args', 'home_page_menu_args' );
/*
-------------------------------------------------------------------------------------------------------
Body Class
-------------------------------------------------------------------------------------------------------
*/
function swell_body_class( $classes ) {
if ( is_singular() ) {
$classes[] = 'swell-singular'; }
if ( is_active_sidebar( 'right-sidebar' ) ) {
$classes[] = 'swell-right-sidebar'; }
if ( '' != get_theme_mod( 'background_image' ) ) {
// This class will render when a background image is set
// regardless of whether the user has set a color as well.
$classes[] = 'swell-background-image';
} else if ( ! in_array( get_background_color(), array( '', get_theme_support( 'custom-background', 'default-color' ) ) ) ) {
// This class will render when a background color is set
// but no image is set. In the case the content text will
// Adjust relative to the background color.
$classes[] = 'swell-relative-text';
}
return $classes;
}
add_action( 'body_class', 'swell_body_class' );
/*
-------------------------------------------------------------------------------------------------------
Includes
-------------------------------------------------------------------------------------------------------
*/
require_once( get_template_directory() . '/includes/jetpack.php' );
require_once( get_template_directory() . '/includes/customizer.php' );
require_once( get_template_directory() . '/includes/typefaces.php' );
require_once( get_template_directory() . '/includes/woocommerce-setup.php' );
require_once( get_template_directory() . '/includes/plugin-activation.php' );
require_once( get_template_directory() . '/includes/plugin-activation-class.php' );
Change this line
if ( get_the_modified_time() != get_the_time() ) {
to
if ( get_the_modified_time() != get_the_time() && false ) {
I am trying to add a custom metabox in woocommerce. It has been added perfectly. My ultimate goal is to call that custom field in a function and show it in the cart.php. So I coded :
For Custom field: [I would refer http://www.remicorson.com/mastering-woocommerce-products-custom-fields/] in this regard
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
woocommerce_wp_text_input(
array(
'id' => 'number_field',
'label' => __( '<strong style="color:#239804">Your Free Products</strong>', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Please enter a number', 'woocommerce' ),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
echo '</div>';
}//woo_add_custom_general_fields
function woo_add_custom_general_fields_save( $post_id ){
$woocommerce_number_field = $_POST['number_field'];
if( !empty( $woocommerce_number_field ) )
update_post_meta( $post_id, 'number_field', esc_attr( $woocommerce_number_field ) );
}//woo_add_custom_general_fields_save( $post_id )
It has perfectly fitted in the Product Admin Page. Now I am creating another function where I am creating a counter for cart.php
function free_products(){
global $woocommerce ,$product, $post;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$free_number = get_post_meta( $post->ID, 'number_field', true );
$free_product = $cart_item['quantity'] * $free_number;
echo apply_filters( 'woocommerce_cart_item_quantity', $free_product, $cart_item_key );
}
}
In My cart.php when I add
<td class="product-quantity">
<?php
echo free_products();
?>
</td>
The output become zero in front end. Can anyone please help me what I am going wrong. Thanks in advance.
Try below code :
function free_products(){
global $woocommerce ,$product, $post;
foreach ( WC()->cart->get_cart() as $cart_item ) {
$my_var = $cart_item['product_id'];
$free_number = get_post_meta( $my_var, 'number_field', true );
$free_product = $cart_item['quantity'] * $free_number;
echo apply_filters( 'woocommerce_cart_item_quantity', $free_product);
}
}
Let me know if It is working for you or not.Its working for me.