Metabox Data Isn't Saving - wordpress

I created a metabox within a class. I needed it for a custom post type that I had. The metabox is displaying fine, but the values refuse to save. I've altered the code several times, but it still isn't working for me. Do you know what I'm doing wrong? Here is the full code:
<?php
class portfolio_metabox {
public function __construct() {
if ( is_admin() ) {
add_action( 'load-post.php', array( $this, 'init_metabox' ) );
add_action( 'load-post-new.php', array( $this, 'init_metabox' ) );
}
}
public function init_metabox() {
add_action( 'add_meta_boxes', array( $this, 'add_metabox' ) );
add_action( 'save_post', array( $this, 'save_metabox' ), 10, 2 );
}
public function add_metabox() {
add_meta_box(
'details',
__( 'Project Details', 'chic' ),
array( $this, 'render_project_metabox' ),
'portfolio',
'advanced',
'default'
);
}
public function render_project_metabox( $post ) {
// Retrieve an existing value from the database.
$portfolio_web_design = get_post_meta( $post->ID, 'portfolio_web_design ', true );
$portfolio_web_dev = get_post_meta( $post->ID, 'portfolio_web_dev ', true );
$portfolio_digital_art = get_post_meta( $post->ID, 'portfolio_digital_art ', true );
$portfolio_graphic_design = get_post_meta( $post->ID, 'portfolio_graphic_design ', true );
// Set default values.
// Form fields.
echo '<table class="form-table">';
echo '<tr>';
echo '<th><label for="portfolio_project" class="portfolio_project_label">' . __( 'Project Type', 'chic' ) . '</label></th>';
echo '<td>';
echo '<label><input type="checkbox" name="portfolio_web_design " class="portfolio_project_field" value="' . $portfolio_web_design . '" ' . checked( $portfolio_web_design , 'checked', false ) . '> ' . __( ' Web Design', 'chic' ) . '</label><br>';
echo '<label><input type="checkbox" name="portfolio_web_dev " class="portfolio_project_field" value="' . $portfolio_web_dev . '" ' . checked( $portfolio_web_dev , 'checked', false ) . '> ' . __( ' Web Development', 'chic' ) . '</label><br>';
echo '<label><input type="checkbox" name="portfolio_digital_art " class="portfolio_project_field" value="' . $portfolio_digital_art . '" ' . checked( $portfolio_digital_art , 'checked', false ) . '> ' . __( ' Digital Art', 'chic' ) . '</label><br>';
echo '<label><input type="checkbox" name="portfolio_graphic_design " class="portfolio_project_field" value="' . $portfolio_graphic_design . '" ' . checked( $portfolio_graphic_design , 'checked', false ) . '> ' . __( ' Graphic Design', 'chic' ) . '</label><br>';
echo '<p class="description">' . __( 'Project Type', 'chic' ) . '</p>';
echo '</td>';
echo '</tr>';
echo '</table>';
}
public function save_metabox( $post_id, $post ) {
// Sanitize user input.
$portfolio_new_web_design = isset( $_POST[ 'portfolio_web_design' ] ) ? 'checked' : '';
$portfolio_new_web_dev = isset( $_POST[ 'portfolio_web_dev' ] ) ? 'checked' : '';
$portfolio_new_digital_art = isset( $_POST[ 'portfolio_digital_art' ] ) ? 'checked' : '';
$portfolio_new_digital_art = isset( $_POST[ 'portfolio_graphic_design' ] ) ? 'checked' : '';
// Update the meta field in the database.
update_post_meta( $post_id, 'portfolio_web_design ', $portfolio_new_web_design );
update_post_meta( $post_id, 'portfolio_web_dev ', $portfolio_new_web_dev );
update_post_meta( $post_id, 'portfolio_digital_art ', $portfolio_new_digital_art );
update_post_meta( $post_id, 'portfolio_graphic_design ', $portfolio_new_digital_art );
}
}
new portfolio_metabox;
?>
UPDATE:
The code given by Mukesh Panchal worked great. It saves just like I wanted it to, but I still can't get the metadata to display on the template. Any ideas why that's going on?

There's a extra space in your input name="" attributes, like here:
name="portfolio_web_design "
This should be:
name="portfolio_web_design"
After that your isset should work properly ;)

Can you please try below updated code:
<?php
class portfolio_metabox {
public function __construct() {
if ( is_admin() ) {
add_action( 'load-post.php', array( $this, 'init_metabox' ) );
add_action( 'load-post-new.php', array( $this, 'init_metabox' ) );
}
}
public function init_metabox() {
add_action( 'add_meta_boxes', array( $this, 'add_metabox' ) );
add_action( 'save_post', array( $this, 'save_metabox' ), 10, 2 );
}
public function add_metabox() {
add_meta_box(
'details',
__( 'Project Details', 'chic' ),
array( $this, 'render_project_metabox' ),
'portfolio',
'advanced',
'default'
);
}
public function render_project_metabox( $post ) {
// Retrieve an existing value from the database.
$portfolio_web_design = get_post_meta( $post->ID, 'portfolio_web_design ', true );
$portfolio_web_dev = get_post_meta( $post->ID, 'portfolio_web_dev ', true );
$portfolio_digital_art = get_post_meta( $post->ID, 'portfolio_digital_art ', true );
$portfolio_graphic_design = get_post_meta( $post->ID, 'portfolio_graphic_design ', true );
// Set default values.
// Form fields.
echo '<table class="form-table">';
echo ' <tr>';
echo ' <th><label for="portfolio_project" class="portfolio_project_label">' . __( 'Project Type', 'chic' ) . '</label></th>';
echo ' <td>';
echo ' <label><input type="checkbox" name="portfolio_web_design" class="portfolio_project_field" value="' . $portfolio_web_design . '" ' . checked( $portfolio_web_design , 'checked', false ) . '> ' . __( ' Web Design', 'chic' ) . '</label><br>';
echo ' <label><input type="checkbox" name="portfolio_web_dev" class="portfolio_project_field" value="' . $portfolio_web_dev . '" ' . checked( $portfolio_web_dev , 'checked', false ) . '> ' . __( ' Web Development', 'chic' ) . '</label><br>';
echo ' <label><input type="checkbox" name="portfolio_digital_art" class="portfolio_project_field" value="' . $portfolio_digital_art . '" ' . checked( $portfolio_digital_art , 'checked', false ) . '> ' . __( ' Digital Art', 'chic' ) . '</label><br>';
echo ' <label><input type="checkbox" name="portfolio_graphic_design" class="portfolio_project_field" value="' . $portfolio_graphic_design . '" ' . checked( $portfolio_graphic_design , 'checked', false ) . '> ' . __( ' Graphic Design', 'chic' ) . '</label><br>';
echo ' <p class="description">' . __( 'Project Type', 'chic' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo '</table>';
}
public function save_metabox( $post_id, $post ) {
// Sanitize user input.
$portfolio_new_web_design = isset( $_POST[ 'portfolio_web_design' ] ) ? 'checked' : '';
$portfolio_new_web_dev = isset( $_POST[ 'portfolio_web_dev' ] ) ? 'checked' : '';
$portfolio_new_digital_art = isset( $_POST[ 'portfolio_digital_art' ] ) ? 'checked' : '';
$portfolio_new_digital_art = isset( $_POST[ 'portfolio_graphic_design' ] ) ? 'checked' : '';
// Update the meta field in the database.
update_post_meta( $post_id, 'portfolio_web_design', $portfolio_new_web_design );
update_post_meta( $post_id, 'portfolio_web_dev', $portfolio_new_web_dev );
update_post_meta( $post_id, 'portfolio_digital_art', $portfolio_new_digital_art );
update_post_meta( $post_id, 'portfolio_graphic_design', $portfolio_new_digital_art );
}
}
new portfolio_metabox;
?>

This should do it:
class portfolio_metabox {
public function __construct() {
if ( is_admin() ) {
add_action( 'load-post.php', array( $this, 'init_metabox' ) );
add_action( 'load-post-new.php', array( $this, 'init_metabox' ) );
}
}
public function init_metabox() {
add_action( 'add_meta_boxes', array( $this, 'add_metabox' ) );
add_action( 'save_post', array( $this, 'save_metabox' ), 10, 2 );
}
public function add_metabox() {
add_meta_box(
'details',
__( 'Project Details', 'chic' ),
array( $this, 'render_project_metabox' ),
'portfolio',
'advanced',
'default'
);
}
public function render_project_metabox( $post ) {
// Retrieve an existing value from the database.
$post_meta = get_post_meta( $post->ID );
$portfolio_web_design = ( isset($post_meta['portfolio_web_design'][0]) && $post_meta['portfolio_web_design'][0] != '' ) ? $post_meta['portfolio_web_design'][0] : 0;
$portfolio_web_dev = ( isset($post_meta['portfolio_web_dev'][0]) && $post_meta['portfolio_web_dev'][0]!= '' ) ? $post_meta['portfolio_web_dev'][0] : 0;
$portfolio_digital_art = ( isset($post_meta['portfolio_digital_art'][0]) && $post_meta['portfolio_digital_art'][0]!= '' ) ? $post_meta['portfolio_digital_art'][0] : 0;
$portfolio_graphic_design = ( isset($post_meta['portfolio_graphic_design'][0]) && $post_meta['portfolio_graphic_design'][0]!= '' ) ? $post_meta['portfolio_graphic_design'][0] : 0;
// Set default values.
// Form fields.
?>
<table class="form-table">
<tr>
<th><label for="portfolio_project" class="portfolio_project_label"><?php esc_html_e( 'Project Type', 'chic' ) ?></label;></th>
<td>
<label><input type="checkbox" name="portfolio_web_design" class="portfolio_project_field" value="1" <?php checked( $portfolio_web_design , 1 ) ?>> <?php esc_html_e( ' Web Design', 'chic' ); ?></label><br>
<label><input type="checkbox" name="portfolio_web_dev" class="portfolio_project_field" value="1" <?php checked( $portfolio_web_dev , 1 ) ?>> <?php esc_html_e( ' Web Development', 'chic' ); ?></label><br>
<label><input type="checkbox" name="portfolio_digital_art" class="portfolio_project_field" value="1" <?php checked( $portfolio_digital_art , 1 ) ?>> <?php esc_html_e( ' Digital Art', 'chic' ); ?></label><br>
<label><input type="checkbox" name="portfolio_graphic_design" class="portfolio_project_field" value="1" <?php checked( $portfolio_graphic_design , 1 ) ?>> <?php esc_html_e( ' Graphic Design', 'chic' ); ?></label><br>
<p class="description"><?php esc_html_e( 'Project Type', 'chic' ) ?></p;>
</td>
</tr>
</table>
<?php
}
public function save_metabox( ) {
global $post;
// Sanitize user input.
$portfolio_new_web_design = ( isset( $_POST['portfolio_web_design'] ) && $_POST['portfolio_web_design'] == 1 ) ? 1 : 0;
$portfolio_new_web_dev = ( isset( $_POST['portfolio_web_dev'] ) && $_POST['portfolio_web_dev'] == 1 ) ? 1 : 0;
$portfolio_new_digital_art = ( isset( $_POST['portfolio_digital_art'] ) && $_POST['portfolio_digital_art'] == 1 ) ? 1 : 0;
$portfolio_new_digital_art = ( isset( $_POST['portfolio_graphic_design'] ) && $_POST['portfolio_graphic_design'] == 1 ) ? 1 : 0;
// Update the meta field in the database.
update_post_meta( $post->ID, 'portfolio_web_design', $portfolio_new_web_design );
update_post_meta( $post->ID, 'portfolio_web_dev', $portfolio_new_web_dev );
update_post_meta( $post->ID, 'portfolio_digital_art', $portfolio_new_digital_art );
update_post_meta( $post->ID, 'portfolio_graphic_design', $portfolio_new_digital_art );
}
}
new portfolio_metabox;
I cleaned up your code a bit. Try not to put space after everything, it's not necessary. The problem was in the way you're pulling the post meta out. The variables like $portfolio_web_design were empty.
I tested this and it worked.
Hope this helps :)

Related

Woocommerce - How to add one link "add to cart" to the loop product thumbnail?

I've tried several things adding the code to functions.php.
I think that the time I've been closer was with this code:
printf('<a class="button" href=" ..... "?>');
echo woocommerce_get_product_thumbnail();
printf('</a>');
}
But I don't know what to href on there.
previously, I removed the other liks with:
/*remove links to loop, add link to title*/
remove_action('woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10);
remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close', 5);
add_action('woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_link_open', 5);
add_action('woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_link_close', 20);
function woocommerce_template_loop_product_title() {
echo sprintf('<h2 class="woocommerce-loop-product__title"><a title="%2$s" href="%1$s">%2$s</a></h2>',
get_the_permalink(),
get_the_title()
);
}
Thank you very much
There may be some modifications that need to be made for it to fit your scenario.
Add the following code to your functions.php file.
This will only add the link to the thumbnail if you are on the products page.
function add_link_woocommerce_single_product_image_thumbnail_html( $html, $post_thumbnail_id ) {
/**
* This will only add the link on a single product page.
*/
if ( is_product() ) :
global $product;
$product_id = $product->get_id();
$product_url = get_permalink( $product_id );
$main_image = true;
$flexslider = (bool) apply_filters( 'woocommerce_single_product_flexslider_enabled', get_theme_support( 'wc-product-gallery-slider' ) );
$gallery_thumbnail = wc_get_image_size( 'gallery_thumbnail' );
$thumbnail_size = apply_filters( 'woocommerce_gallery_thumbnail_size', array( $gallery_thumbnail['width'], $gallery_thumbnail['height'] ) );
$image_size = apply_filters( 'woocommerce_gallery_image_size', $flexslider || $main_image ? 'woocommerce_single' : $thumbnail_size );
$full_size = apply_filters( 'woocommerce_gallery_full_size', apply_filters( 'woocommerce_product_thumbnails_large_size', 'full' ) );
$thumbnail_src = wp_get_attachment_image_src( $post_thumbnail_id, $thumbnail_size );
$full_src = wp_get_attachment_image_src( $post_thumbnail_id, $full_size );
$alt_text = trim( wp_strip_all_tags( get_post_meta( $post_thumbnail_id, '_wp_attachment_image_alt', true ) ) );
$image = wp_get_attachment_image(
$post_thumbnail_id,
$image_size,
false,
apply_filters(
'woocommerce_gallery_image_html_attachment_image_params',
array(
'title' => _wp_specialchars( get_post_field( 'post_title', $post_thumbnail_id ), ENT_QUOTES, 'UTF-8', true ),
'data-caption' => _wp_specialchars( get_post_field( 'post_excerpt', $post_thumbnail_id ), ENT_QUOTES, 'UTF-8', true ),
'data-src' => esc_url( $full_src[0] ),
'data-large_image' => esc_url( $full_src[0] ),
'data-large_image_width' => esc_attr( $full_src[1] ),
'data-large_image_height' => esc_attr( $full_src[2] ),
'class' => esc_attr( $main_image ? 'wp-post-image' : '' ),
),
$post_thumbnail_id,
$image_size,
$main_image
)
);
$html = '<div data-thumb="' . esc_url( $thumbnail_src[0] ) . '" data-thumb-alt="' . esc_attr( $alt_text ) . '" class="woocommerce-product-gallery__image">' . $image . '</div>';
return $html;
endif;
return $html;
}
add_filter( 'woocommerce_single_product_image_thumbnail_html', 'add_link_woocommerce_single_product_image_thumbnail_html', 10, 2 );

Use image as radio button on Contact Form 7 but not receiving image in mail

I used this code below which is very useful in wordpress function.php for contact form 7 to select images as checkbox but its not receiving in email, please help me in this matter, thanks
Here is the code
function add_shortcode_imageradio() {
wpcf7_add_shortcode( 'imageradio', 'imageradio_handler', true );
}
add_action( 'wpcf7_init', 'add_shortcode_imageradio' );
function imageradio_handler( $tag ){
$tag = new WPCF7_FormTag( $tag );
$atts = array(
'type' => 'radio',
'name' => $tag->name,
'list' => $tag->name . '-options' );
$input = sprintf(
'<input %s />',
wpcf7_format_atts( $atts ) );
$datalist = '';
$datalist .= '<div class="imgradio">';
foreach ( $tag->values as $val ) {
list($radiovalue,$imagepath) = explode("!", $val
);
$datalist .= sprintf(
'<label><input type="radio" name="%s" value="%s" class="hideradio" /><img src="%s"></label>', $tag->name, $radiovalue, $imagepath
);
}
$datalist .= '</div>';
return $datalist;
}
Here is the page I am working on:
https://www.commforceintl.com/lost-and-found-english/

Add Class Variation Woocommerce Out Stock

I need to add a css style class for only "label" products that are not in stock.
Example Original Code:
<label class="radio-label sw-radio-variation sw-radio-variation-2" title="l" for="l_2">
The code I want:
<label class="out-stock radio-label sw-radio-variation sw-radio-variation-2" title="l" for="l_2">
As you see add to the CSS code out-stock
Thanks
Edit: My product-variation.php is
<?php
/*
** Product variation hook
*/
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'sw_woocommerce_custom_variation', 10, 2 );
function sw_woocommerce_custom_variation( $html, $args ){
$options = $args['options'];
$product = $args['product'];
$attribute = $args['attribute'];
$name = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title( $attribute );
$id = $args['id'] ? $args['id'] : sanitize_title( $attribute );
$class = $args['class'];
if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
$attributes = $product->get_variation_attributes();
$options = $attributes[ $attribute ];
}
$html = '';
if ( ! empty( $options ) ) {
$html .= '<div class="sw-custom-variation">';
if ( $product && taxonomy_exists( $attribute ) ) {
// Get terms if this is a taxonomy - ordered. We need the names too.
$terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) );
foreach ( $terms as $key => $term ) {
$color = get_term_meta( $term->term_id, 'sw_variation_color', true );
$active = ( checked( sanitize_title( $args['selected'] ), $term->slug, false ) ) ? ' selected' : '';
$attributes = ( preg_match( '/color|colors/', $attribute, $match ) && $color != '' ) ? 'class="variation-color" style="background: '. esc_attr( $color ) .'"' : '';
if ( in_array( $term->slug, $options ) ) {
$html .= '<label class="radio-label sw-radio-variation sw-radio-variation-'. esc_attr( $key .' '. $active ) .'" title="'. esc_attr( $term->slug ) .'" for="'. esc_attr( $term->slug . '_' . $key ) . '">';
$html .= '<input type="radio" id="'. esc_attr( $term->slug . '_' . $key ) .'" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" value="' . esc_attr( $term->slug ) . '" '. checked( sanitize_title( $args['selected'] ), $term->slug, false ) .'/>';
$html .= '<span '. $attributes .'>'. $term->name .'</span>';
$html .= '</label>';
}
}
}else {
foreach ( $options as $key => $option ) {
// This handles < 2.4.0 bw compatibility where text attributes were not sanitized.
$checked = sanitize_title( $args['selected'] ) === $args['selected'] ? checked( $args['selected'], sanitize_title( $option ), false ) : checked( $args['selected'], $option, false );
$active = ( $checked ) ? 'selected' : '';
$html .= '<label class="radio-label sw-radio-variation sw-radio-variation-'. esc_attr( $key .' '. $active ) .'" title="'. esc_attr( $option ) .'" for="'. esc_attr( $option . '_' . $key ) . '">';
$html .= '<input type="radio" id="'. esc_attr( $option . '_' . $key ) .'" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" value="' . esc_attr( $option ) . '" '. $checked .'/>';
$html .= '<span>'. $option .'</span>';
$html .= '</label>';
}
}
$html .= '</div>';
}
return $html;
}
/*
** Action hook to color
*/
$sw_attribute_taxonomies = wc_get_attribute_taxonomies();
if ( ! empty( $sw_attribute_taxonomies ) ) {
foreach( $sw_attribute_taxonomies as $attr ){
if( preg_match( '/color|colors/', $attr->attribute_name, $match ) ){
add_action( 'pa_'. $attr->attribute_name .'_add_form_fields', 'sw_woocommerce_variation_fields', 200 );
add_action( 'pa_'. $attr->attribute_name .'_edit_form_fields', 'sw_woocommerce_edit_variation_fields', 200 );
add_action( 'created_term', 'sw_woocommerce_save_variation_fields', 10, 3 );
add_action( 'edit_terms', 'sw_woocommerce_save_variation_fields', 10, 3 );
/* Enqueue Admin js */
add_action( 'admin_enqueue_scripts', 'sw_woocommerce_variation_color_script' );
}
}
}
/*
** Create color
*/
function sw_woocommerce_variation_fields() {
?>
<div class="form-field custom-picker">
<label for="sw_variation_color"><?php _e( 'Color', 'sw_woocommerce' ); ?></label>
<input name="sw_variation_color" id="sw_variation_color" type="text" value="" size="40" class="category-colorpicker"/>
</div>
<?php
}
function sw_woocommerce_edit_variation_fields( $term ) {
$sw_variation_color = get_term_meta( $term->term_id, 'sw_variation_color', true );
?>
<tr class="form-field custom-picker custom-picker-edit">
<th scope="row" valign="top"><label for="sw_variation_color"><?php _e( 'Color', 'sw_woocommerce' ); ?></label></th>
<td>
<input name="sw_variation_color" id="sw_variation_color" type="text" value="<?php echo esc_attr( $sw_variation_color ) ?>" size="40" class="category-colorpicker"/>
</td>
</tr>
<?php
}
/** Save Custom Field Of Category Form */
function sw_woocommerce_save_variation_fields( $term_id, $tt_id = '', $taxonomy = '', $prev_value = '' ) {
if ( isset( $_POST['sw_variation_color'] ) ) {
$term_value = esc_attr( $_POST['sw_variation_color'] );
update_term_meta( $term_id, 'sw_variation_color', $term_value, $prev_value );
}
}
function sw_woocommerce_variation_color_script(){
wp_enqueue_style( 'wp-color-picker' );
wp_enqueue_script('category_color_picker_js', WCURL . '/js/admin/category_color_picker.js', array( 'wp-color-picker' ), false, true);
}
As you can see on line 60 you will find the labels
Will there be any code to add a custom CSS to these labels that create the variables?
Thanks
************** SOLUTION ***********
<?php
// Adiccional Class For Label Variations
function get_variation_stock_text( $product, $name, $term_slug ){
foreach ( $product->get_available_variations() as $variation ){
if($variation['attributes'][$name] == $term_slug ){
$is_in_stock = $variation['is_in_stock'];
$stock_qty = get_post_meta($variation['variation_id'], '_stock', true);
}
}
$in_stock = ' '.__("katayainstock", "woocommerce").'';
$out_of_stock = ' '.__("katayaoutstock", "woocommerce").'';
return $is_in_stock == 1 ? $in_stock : $out_of_stock;
}
/**
* Product variation hook
*/
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'sw_woocommerce_custom_variation', 10, 2 );
function sw_woocommerce_custom_variation( $html, $args ) {
$options = $args['options'];
$product = $args['product'];
$attribute = $args['attribute'];
$name = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title( $attribute );
$id = $args['id'] ? $args['id'] : sanitize_title( $attribute );
$class = $args['class'];
if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
$attributes = $product->get_variation_attributes();
$options = $attributes[ $attribute ];
}
$html = '';
if ( ! empty( $options ) ) {
$html .= '<div class="sw-custom-variation">';
if ( $product && taxonomy_exists( $attribute ) ) {
// Get terms if this is a taxonomy - ordered. We need the names too.
$terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) );
foreach ( $terms as $key => $term ) {
$color = get_term_meta( $term->term_id, 'sw_variation_color', true );
$active = checked( sanitize_title( $args['selected'] ), $term->slug, false ) ? ' selected' : '';
$attributes = ( preg_match( '/color|colors/', $attribute, $match ) && $color !== '' ) ? 'class="variation-color" style="background: ' . esc_attr( $color ) . '"' : '';
if ( in_array( $term->slug, $options, true ) ) {
$stock_text = get_variation_stock_text( $product, $name, $term->slug );
$html .= '<label class="' . $stock_text . ' radio-label sw-radio-variation sw-radio-variation-' . esc_attr( $key . ' ' . $active ) . '" title="' . esc_attr( $term->slug ) . '" for="' . esc_attr( $term->slug . '_' . $key ) . '">';
$html .= '<input type="radio" id="' . esc_attr( $term->slug . '_' . $key ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" value="' . esc_attr( $term->slug ) . '" ' . checked( sanitize_title( $args['selected'] ), $term->slug, false ) . '/>';
$html .= '<span ' . $attributes . '>' . $term->name . '</span>';
$html .= '</label>';
}
}
} else {
foreach ( $options as $key => $option ) {
// This handles < 2.4.0 bw compatibility where text attributes were not sanitized.
$checked = sanitize_title( $args['selected'] ) === $args['selected'] ? checked( $args['selected'], sanitize_title( $option ), false ) : checked( $args['selected'], $option, false );
$active = ( $checked ) ? 'selected' : '';
$html .= '<label class="radio-label sw-radio-variation sw-radio-variation-' . esc_attr( $key . ' ' . $active ) . '" title="' . esc_attr( $option ) . '" for="' . esc_attr( $option . '_' . $key ) . '">';
$html .= '<input type="radio" id="' . esc_attr( $option . '_' . $key ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" value="' . esc_attr( $option ) . '" ' . $checked . '/>';
$html .= '<span>' . $option . '</span>';
$html .= '</label>';
}
}
$html .= '</div>';
}
return $html;
}
/**
* Action hook color
*/
$sw_attribute_taxonomies = wc_get_attribute_taxonomies();
if ( ! empty( $sw_attribute_taxonomies ) ) {
foreach ( $sw_attribute_taxonomies as $attr ) {
if ( preg_match( '/color|colors/', $attr->attribute_name, $match ) ) {
add_action( 'pa_' . $attr->attribute_name . '_add_form_fields', 'sw_woocommerce_variation_fields', 200 );
add_action( 'pa_' . $attr->attribute_name . '_edit_form_fields', 'sw_woocommerce_edit_variation_fields', 200 );
add_action( 'created_term', 'sw_woocommerce_save_variation_fields', 10, 3 );
add_action( 'edit_terms', 'sw_woocommerce_save_variation_fields', 10, 3 );
add_action( 'admin_enqueue_scripts', 'sw_woocommerce_variation_color_script' );
}
}
}
/**
* Create color
*/
function sw_woocommerce_variation_fields() { ?>
<div class="form-field custom-picker">
<label for="sw_variation_color"><?php _e( 'Color', 'sw_woocommerce' ); ?></label>
<input name="sw_variation_color" id="sw_variation_color" type="text" value="" size="40"
class="category-colorpicker"/>
</div>
<?php
}
/**
* #param $term
*/
function sw_woocommerce_edit_variation_fields( $term ) {
$sw_variation_color = get_term_meta( $term->term_id, 'sw_variation_color', true ); ?>
<tr class="form-field custom-picker custom-picker-edit">
<th scope="row" valign="top"><label
for="sw_variation_color"><?php _e( 'Color', 'sw_woocommerce' ); ?></label></th>
<td>
<input name="sw_variation_color" id="sw_variation_color" type="text"
value="<?php echo esc_attr( $sw_variation_color ) ?>" size="40" class="category-colorpicker"/>
</td>
</tr>
<?php
}
/**
* Save custom field of category form
*
* #param $term_id
* #param string $tt_id
* #param string $taxonomy
* #param string $prev_value
*/
function sw_woocommerce_save_variation_fields( $term_id, $tt_id = '', $taxonomy = '', $prev_value = '' ) {
if ( isset( $_POST['sw_variation_color'] ) ) {
$term_value = esc_attr( $_POST['sw_variation_color'] );
update_term_meta( $term_id, 'sw_variation_color', $term_value, $prev_value );
}
}
function sw_woocommerce_variation_color_script() {
wp_enqueue_style( 'wp-color-picker' );
wp_enqueue_script( 'category_color_picker_js', WCURL . '/js/admin/category_color_picker.js', array( 'wp-color-picker' ), false, true );
}

Add to cart button with quentity filed under image woocommerce

I want to add add to cart button with quantity filed under image in single product page. i have tried many code but not luck. one code which work but issue is that quantity not adding properly. i add below code my-theme/woocommerce/single-product/product-image.php before do_action( 'woocommerce_product_thumbnails' ); hook.
if ( ! is_shop() && ! is_product_taxonomy() ) {
$quantity_field = woocommerce_quantity_input( array(
'input_name' => 'product_id',
'input_value' => ! empty( $product->cart_item['quantity'] ) ? $product->cart_item['quantity'] : 1,
'max_value' => $product->backorders_allowed() ? '' : $product->get_stock_quantity(),
'min_value' => 0,
), $product, false );
$quantity_field = str_replace( array( '<div class="quantity">', "</div>" ), '', $quantity_field );
echo str_replace( '<input ', '<input style="max-width: 70px" ', $quantity_field );
}
echo apply_filters( 'woocommerce_loop_add_to_cart_link',
sprintf( '<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>',
esc_url( $product->add_to_cart_url() ),
esc_attr( isset( $quantity ) ? $quantity : 1 ),
esc_attr( $product->id ),
esc_attr( $product->get_sku() ),
esc_attr( isset( $class ) ? $class : 'button' ),
esc_html( $product->add_to_cart_text() )
),
$product );
Hi add this hook in your function file.
remove_action( 'woocommerce_single_product_summary','woocommerce_template_single_add_to_cart',30 );
add_action( 'woocommerce_product_thumbnails','woocommerce_template_single_add_to_cart',30 );
display as like default WC
http://screencast.com/t/u8giinLfKmzt

Undefined index error in custom post metabox

I'm getting this error with my custom post metabox. The custom post and metabox works fine, but if I have debug on and when I try to save any other page or post, I will get an error:
Notice: Undefined index: iam_video_select in C:\xampp\htdocs\Iam\wp-content\themes\Iam\inc\classes\Custom_Meta_Boxes.class.php on line 149
Notice: Undefined index: iam_video_url in C:\xampp\htdocs\Iam\wp-content\themes\Iam\inc\classes\Custom_Meta_Boxes.class.php on line 149
Notice: Undefined index: iam_link in C:\xampp\htdocs\Iam\wp-content\themes\Iam\inc\classes\Custom_Meta_Boxes.class.php on line 149
My code:
<?php
$meta_boxes = array();
//Video meta box
$meta_boxes[] = array(
'id' => 'video-meta-box',
'title' => __( 'Video', 'iamtheme' ),
'pages' => 'post',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'Select Video Host',
'id' => PTHEME . '_video_select',
'type' => 'select',
'options' => array( 'Youtube', 'Vimeo', 'Self hosted' )
),
array(
'name' => 'URL',
'desc' => 'Enter video url here.',
'id' => PTHEME . '_video_url',
'std' => 'Default value here.',
'type' => 'text'
)
)
);
//Link meta box
$meta_boxes[] = array(
'id' => 'link-meta-box',
'title' => __( 'Link', 'iamtheme' ),
'pages' => 'post',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'Link',
'desc' => 'Enter your url here.',
'id' => PTHEME . '_link',
'std' => 'Default value here.',
'type' => 'text'
)
)
);
/**
*
*/
class Custom_Meta_Boxes{
public $_meta_box;
public function __construct( $meta_box ){
$this->_meta_box = $meta_box;
add_action( 'add_meta_boxes', array( $this, 'iam_add_meta_box' ) );
add_action( 'save_post', array( $this, 'iam_save_meta_box_data' ) );
}
/**
* Adds a meta box to the post editing screen
*/
public function iam_add_meta_box(){
add_meta_box(
$this->_meta_box['id'],
$this->_meta_box['title'],
array( &$this, 'iam_display_custom_meta_box' ),
$this->_meta_box['pages'],
$this->_meta_box['context'],
$this->_meta_box['priority']
);
}
/**
* Render Meta Box content.
*/
public function iam_display_custom_meta_box() {
global $post;
// Add an nonce field so we can check for it later.
wp_nonce_field( 'iam_nonce_check', 'iam_nonce_check_value' );
echo '<div class="metabox-wrapper">';
foreach ( $this->_meta_box['fields'] as $field) {
// get current post meta data
$meta = get_post_meta( $post->ID, $field['id'], true );
echo '<div class="metabox-fields metabox_' , $field['type'] , '">';
echo '<label for="', $field['id'] , '">', $field['name'] , '</label>';
switch ( $field['type'] ) {
case 'text':
echo '<input type="text" name="', $field['id'] , '" id="', $field['id'] , '" value="', $meta , '" />';
echo '<p class="meta-desc">' , $field['desc'] , '</p>';
break;
case 'textarea':
echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta , '</textarea>';
echo '<p class="meta-desc">' , $field['desc'] , '</p>';
break;
case 'select':
echo '<select name="', $field['id'], '" id="', $field['id'], '">';
foreach ( $field['options'] as $option ) {
echo '<option', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
}
echo '</select>';
break;
case 'radio':
foreach ( $field['options'] as $option ) {
echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
}
break;
case 'checkbox':
echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
break;
}
echo '</div>';
}
echo '</div>';
}
/**
* Save the meta when the post is saved.
*/
public function iam_save_meta_box_data( $post_id ){
if( $this->iam_user_can_save( $post_id, 'iam_nonce_check_value' ) ) {
// Checks for input and sanitizes/saves if needed
foreach ( $this->_meta_box['fields'] as $field ) {
$old = get_post_meta( $post_id, $field['id'], true );
$new = sanitize_text_field( $_POST[ $field['id'] ] );
if ( $new && $new != $old ) {
update_post_meta( $post_id, $field['id'], $new );
} elseif ( '' == $new && $old ) {
delete_post_meta( $post_id, $field['id'], $old );
}
}
}
}
/**
* Determines whether or not the current user has the ability to save meta
* data associated with this post.
*
* #param int $post_id The ID of the post being save
* #param bool Whether or not the user has the ability to save this post.
*/
public function iam_user_can_save( $post_id, $nonce ){
// Checks save status
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ $nonce ] ) && wp_verify_nonce( $_POST[ $nonce ], 'iam_nonce_check' ) ) ? 'true' : 'false';
// Return true if the user is able to save; otherwise, false.
return ! ( $is_autosave || $is_revision ) && $is_valid_nonce;
}
}
// Instantiate theme
if ( class_exists( 'Custom_Meta_Boxes' ) ){
foreach ( $meta_boxes as $meta_box ) {
$my_box = new Custom_Meta_Boxes( $meta_box );
}
}
?>
Thanks for any helps.

Resources