To add magento like new products to woocommerce, you have to copy the functionalities from them woocommerce featured produts and make it as a shortcode or use just function call
To add products new label simply add this to your functions.php file to cretae new label checkbox meta box from admin
<?php
/*Add new products label to woocommerce*/
add_action("admin_init", "wp4wordpress_add_product_meta_box");
function wp4wordpress_add_product_meta_box(){
if(class_exists('Woocommerce')){
$post_type = 'product';
}
add_meta_box("wp4wordpress-product-meta-box", __( "wp4wordpress Custom Settings", 'wp4wordpress_DOMAIN'), "wp4wordpress_product_meta_box", $post_type, "side", "default");
}
function wp4wordpress_product_meta_box() {
global $post;
?>
<div class="wp4wordpress-metaboxes">
<input type="hidden" name="wp4wordpress_product_meta_box_nonce" value="<?php echo wp_create_nonce(plugin_basename(__FILE__)); ?>" />
<p><input type="checkbox" name="wp4wordpress_product[_wp4wordpress_new_label]" id="wp4wordpress_product[_wp4wordpress_new_label]" value="1" <?php checked(1, wp4wordpress_get_custom_field('_wp4wordpress_new_label')); ?> />
<label for="wp4wordpress_product[_wp4wordpress_new_label]"><?php _e("Add this product to New Products List", 'wp4wordpress_DOMAIN'); ?></label>
</p>
</div>
<?php
}
add_action('save_post', 'wp4wordpress_product_meta_box_save', 1, 2);
function wp4wordpress_product_meta_box_save($post_id, $post) {
// verify the nonce
if ( !isset($_POST['wp4wordpress_product_meta_box_nonce']) || !wp_verify_nonce( $_POST['wp4wordpress_product_meta_box_nonce'], plugin_basename(__FILE__) ) )
return $post->ID;
// don't try to save the data under autosave, ajax, or future post.
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;
if ( defined('DOING_AJAX') && DOING_AJAX ) return;
if ( defined('DOING_CRON') && DOING_CRON ) return;
// is the user allowed to edit the post or page?
if ( ('page' == $_POST['post_type'] && !current_user_can('edit_page', $post->ID)) || !current_user_can('edit_post', $post->ID ) )
return $post->ID;
$product_defaults = array(
'_wp4wordpress_new_label' => '',
);
$product = wp_parse_args($_POST['wp4wordpress_product'], $product_defaults);
// store the custom fields
foreach ( (array)$product as $key => $value ) {
if ( $post->post_type == 'revision' ) return; // don't try to store data during revision save
// sanitize the url before storage
if ( $key == 'link' && $value ) $value = esc_url( $value );
if ( $value )
update_post_meta($post->ID, $key, $value);
else
delete_post_meta($post->ID, $key);
}
}
To retrive the new label is active for current product add this code
<?php
function wp4wordpress_product_is_new( $product_id = '' ) {
global $post, $wpdb;
$key = '_wp4wordpress_new_label';
if(!$product_id) $product_id = $post->ID;
if(!$product_id) return false;
$_wp4wordpress_new_label = get_post_meta($product_id, $key);
return $_wp4wordpress_new_label;
if($_wp4wordpress_new_label[0] == 1) {
return true;
}
return false;
}
?>
the following code is used To add new labels to products
<?php
if ( $data['new_icon'] ) : $count_labels++;
if(wp4wordpress_product_is_new($product_id)) :
if($count_labels > 1) $new_label = 'new_label';
$output .= '<span class="label-icon new-label '.$new_label.'">'.__( 'New').'</span>';
endif;
endif;
return $output;
?>
Responses
0 Respones to "How to add magento like new label to woocommerce produtcs"
Post a Comment