I’m working on a Woocommerce site that requires some of its products to have extra information “Product Features” under the the short description and before the cart.
I’ve created a tab in the Woocommerce product data panel called “Features” and it includes 5 custom fields for “Key Features”. This is the code:
add_filter( 'woocommerce_product_data_tabs', 'ssafe_product_settings_tabs' );
function ssafe_product_settings_tabs( $tabs ){
$tabs[ 'ssafe' ] = array(
'label' => 'Features',
'target' => 'features_product_data',
'priority' => 81,
);
return $tabs;
}
add_action( 'woocommerce_product_data_panels', 'ssafe_product_panels' );
function ssafe_product_panels(){
echo '<div id="features_product_data" class="panel woocommerce_options_panel hidden">';
woocommerce_wp_text_input(
array(
'id' => 'feat_1',
'value' => get_post_meta( get_the_ID(), 'feat_1', true ),
'label' => 'Feature 1',
'description' => 'Describe a key product feature'
)
);
woocommerce_wp_text_input(
array(
'id' => 'feat_2',
'value' => get_post_meta( get_the_ID(), 'feat_2', true ),
'label' => 'Feature 2',
'description' => 'Describe a key product feature'
)
);
woocommerce_wp_text_input(
array(
'id' => 'feat_3',
'value' => get_post_meta( get_the_ID(), 'feat_3', true ),
'label' => 'Feature 3',
'description' => 'Describe a key product feature'
)
);
woocommerce_wp_text_input(
array(
'id' => 'feat_4',
'value' => get_post_meta( get_the_ID(), 'feat_4', true ),
'label' => 'Feature 4',
'description' => 'Describe a key product feature'
)
);
woocommerce_wp_text_input(
array(
'id' => 'feat_5',
'value' => get_post_meta( get_the_ID(), 'feat_5', true ),
'label' => 'Feature 5',
'description' => 'Describe a key product feature'
)
);
echo '</div>';
}
add_action( 'woocommerce_process_product_meta', 'ssafe_save_fields' );
function ssafe_save_fields( $id ){
update_post_meta( $id, 'feat_1', sanitize_text_field( $_POST[ 'feat_1' ] ) );
update_post_meta( $id, 'feat_2', sanitize_text_field( $_POST[ 'feat_2' ] ) );
update_post_meta( $id, 'feat_3', sanitize_text_field( $_POST[ 'feat_3' ] ) );
update_post_meta( $id, 'feat_4', sanitize_text_field( $_POST[ 'feat_4' ] ) );
update_post_meta( $id, 'feat_5', sanitize_text_field( $_POST[ 'feat_5' ] ) );
}
I’ve also added code to the functions.php to display these fields on the front-end of the website. This is the code:
add_action( 'woocommerce_before_add_to_cart_form', 'features', 11 );
function features() {
global $product;
$features1 = $product->get_meta( 'feat_1' );
$features2 = $product->get_meta( 'feat_2' );
$features3 = $product->get_meta( 'feat_3' );
$features4 = $product->get_meta( 'feat_4' );
$features5 = $product->get_meta( 'feat_5' );
if ( ! empty($features1) ) {
echo ("<h6>Key Features</h6>");
}
if ( ! empty($features1) ) {
echo '<p>' . sprintf( __( ' • %s', 'woocommerce' ), $features1 ) . '</p>';
}
if ( ! empty($features2) ) {
echo '<p>' . sprintf( __( ' • %s', 'woocommerce' ), $features2 ) . '</p>';
}
if ( ! empty($features3) ) {
echo '<p>' . sprintf( __( ' • %s', 'woocommerce' ), $features3 ) . '</p>';
}
if ( ! empty($features4) ) {
echo '<p>' . sprintf( __( ' • %s', 'woocommerce' ), $features4 ) . '</p>';
}
if ( ! empty($features5) ) {
echo '<p>' . sprintf( __( ' • %s', 'woocommerce' ), $features5 ) . '</p>';
}
}
The second block of code is where I’m struggling. It needs to be conditional so that Key Features only appear if the custom field contains some data. However, I’m worried that I’m using a lot of echo
statements to achieve something that can probably done with a bit more finesse.
Does anybody have any suggestions for making my code a bit more efficient?