Eigene Produkt-Felder in WooCommerce anlegen

Grade zusammengesucht und bereits umgesetzt. Das Script ermöglicht ein eigenes Feld im Produkt anzulegen. Z.B. für Produkt-Lager oder zusätzliche Informationen wie Grundpreis oder ähnlichem. Bitte in die functions.php des Themes einfügen.

add_action( 'woocommerce_product_options_pricing', 'cg_custom_product_field' );
function cg_custom_product_field() {
    woocommerce_wp_text_input( array( 'id' => 'cg_custom', 'class' => 'wc_input_custom short', 'label' => __( 'Eigenes Feld', 'woocommerce' ) . '' ) );
}

Der erste Abschnitt stellt das Feld im Backend beim Preis da.

add_action( 'save_post', 'cg_custom_save_product' );
 function cg_custom_save_product( $product_id ) {
 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
 return;
 if ( isset( $_POST['cg_custom'] ) ) {
 update_post_meta( $product_id, 'cg_custom', $_POST['cg_custom'] );
 } else delete_post_meta( $product_id, 'cg_custom' );
 }

Beim Absenden oder autom. Speichern des Formulars wird nun das Feld gespeichert. Oder wenn leer wird das Feld in der Datenbank gelöscht.

add_action( 'woocommerce_single_product_summary', 'cg_custom_show', 6 );
function cg_custom_show() {
    global $product;
    if ( $product->product_type <> 'variable' ) {
        $cg_custom = get_post_meta( $product->id, 'cg_custom', true );
        if($cg_custom) {
            echo '<p class="cg_custom cg">';
            echo '<span class="woocommerce-cg_custom">' . $cg_custom . '</span>';
            echo '</p>';
        }
    }
}

Dieser Abschnitt stellt das Feld im Produkt dar. Das ganze passiert über dem Preis.

function cg_shop_display_custom() {
    global $product;
    $cg_custom = get_post_meta( get_the_ID(), 'cg_custom', true );
    if($cg_baseprice){
        echo '<small class="custom">';
        echo '<span class="woocommerce-cg_custom">' . woocommerce_price( $cg_custom ) . ' </span>';
        _e( 'Eigenes Feld', 'woocommerce' );
        echo '</small>';
    }
}
add_action( 'woocommerce_after_shop_loop_item_title', 'cg_shop_display_custom', 10 );

Mit diesem Abschnitt wird das Feld auch in der Shop-Ansicht ausgegeben.

add_filter( 'woocommerce_product_tabs', 'cg_fakten_product_tab' );
function cg_fakten_product_tab( $tabs ) {
    
    // Adds the new tab
    
    $tabs['test_tab'] = array(
        'title'     => __( 'Fakten', 'woocommerce' ),
        'priority'  => 10,
        'callback'  => 'cg_fakten_product_tab_content'
    );

    return $tabs;

}
function cg_fakten_product_tab_content() {

    $markup = '<h2>Fakten</h2>';

    global $post;
    $product = wc_get_product( $post->ID );

    $markup .= '<div class="table-responsive"><table><tbody><tr>';

    $cg_custom = get_post_meta( $product->id, 'cg_custom', true );
    if($cg_custom) {
        $markup .= '<tr><td class="label">Eigenes Feld: ';
        $markup .= '</td><td class="woocommerce-cg_custom">' . $cg_custom . '%</td></tr>';
    }

    $markup .= '</tbody></table></div>';

    if( $cg_custom ) {
        echo $markup;
    } else {
        echo "Fehlende Angaben werden schnellst möglich ergänzt.";
    }
}

Der letzte Abschnitt stellt alle Angaben in einem eigenen Tab auf der Produkt-Seite als Tabelle dar.

Quellen: 1,2

Kommentar verfassen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Nach oben scrollen