user3559298
user3559298

Reputation: 77

Wordpress - Woocommerce

I found some custom fields turorial for WP and Woocommerce. So I played a bit with that. It all works ok, but I tried to customize part that saves values during checkbox checking, and unfortunately I'm not able to finish that. This is what I'm trying, piece of code in functions.php:

// Custom field for price labels
// Display Fields
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;

    // Checkbox
    woocommerce_wp_checkbox( 
        array( 
            'id'            => '_primer', 
            'wrapper_class' => '', 
            'label'         => __('Primer', 'woocommerce' ), 
            'description'   => __( 'Check me!', 'woocommerce' ) 
        )
    );
}

function woo_add_custom_general_fields_save( $post_id ){

    // Checkbox_1
    $woocommerce_checkbox_primer = isset( $_POST['_primer'] ) ? 'yes' : '';
    update_post_meta( $post_id, '_primer', $woocommerce_checkbox_primer );
}  

...and then this peace of code in single-product.php

<span><?php echo get_post_meta( get_the_ID(), '_primer', true ); ?></span> 

So when this is implemented I have one big "yes" on single product page, but what I want to get is image instead of that "yes", so if checkbox is checked to show image on single product page, and to save that selection in database. I hope I was clear enough, and thanks in advance.

Upvotes: 2

Views: 386

Answers (1)

helgatheviking
helgatheviking

Reputation: 26329

I'm not 100% sure that I understand but it sounded like you wanted to show an image conditionally if the checkbox was true in the backend

if( 'yes' == get_post_meta( get_the_ID(), '_primer', true ) ) { ?>
  <img src="your-image.png"/>
<?php } 

Upvotes: 2

Related Questions