44. Field Forge mit WooCommerce | Field Forge - Benutzerdefinierte Felder, gebaut für Geschwindigkeit
Herunterladen Anmelden

44. Field Forge mit WooCommerce

Integrieren Sie benutzerdefinierte Felder von Field Forge mit WooCommerce-Produkten.

Hinzufügen von Feldern zu Produkten

Erstellen Sie eine Feldgruppe, die den product Beitragstyp anvisiert:

php
FIELDFORGE_Field_Groups::instance()->create( [
    'title'  => 'Product Extra Info',
    'fields' => [
        [ 'key' => 'field_wc_material', 'label' => 'Material', 'name' => 'material', 'type' => 'text' ],
        [ 'key' => 'field_wc_care', 'label' => 'Care Instructions', 'name' => 'care_instructions', 'type' => 'textarea' ],
        [ 'key' => 'field_wc_warranty', 'label' => 'Warranty (months)', 'name' => 'warranty_months', 'type' => 'number' ],
    ],
    'location_rules' => [
        [ [ 'param' => 'post_type', 'operator' => '==', 'value' => 'product' ] ],
    ],
] );

Anzeigen von Feldern auf Produktseiten

php
// Display after the product summary
add_action( 'woocommerce_single_product_summary', function() {
    $material = get_field( 'material' );
    $warranty = get_field( 'warranty_months' );

    if ( $material ) {
        echo '<div class="product-material"><strong>Material:</strong> ' . esc_html( $material ) . '</div>';
    }
    if ( $warranty ) {
        echo '<div class="product-warranty"><strong>Warranty:</strong> ' . esc_html( $warranty ) . ' months</div>';
    }
}, 25 );

// Display care instructions in a product tab
add_filter( 'woocommerce_product_tabs', function( $tabs ) {
    $care = get_field( 'care_instructions' );
    if ( $care ) {
        $tabs['care'] = [
            'title'    => 'Care Instructions',
            'callback' => function() {
                echo '<div class="care-tab">' . wp_kses_post( get_field( 'care_instructions' ) ) . '</div>';
            },
            'priority' => 20,
        ];
    }
    return $tabs;
} );

Synchronisieren von Preisfeldern

WooCommerce liest Preise aus _price und _regular_price Postmeta. Synchronisieren Sie Field Forge-Werte:

php
add_action( 'fieldforge/value_updated', function( $post_id, $field_name ) {
    if ( get_post_type( $post_id ) !== 'product' ) {
        return;
    }

    $sync_map = [
        'price'      => '_regular_price',
        'sale_price' => '_sale_price',
        'sku'        => '_sku',
    ];

    if ( isset( $sync_map[ $field_name ] ) ) {
        $value = FIELDFORGE_Field_Values::instance()->get_value( $field_name, $post_id );
        update_post_meta( $post_id, $sync_map[ $field_name ], $value );

        // Update WooCommerce _price (used for sorting/filtering)
        if ( $field_name === 'sale_price' && $value ) {
            update_post_meta( $post_id, '_price', $value );
        } elseif ( $field_name === 'price' ) {
            $sale = get_post_meta( $post_id, '_sale_price', true );
            if ( ! $sale ) {
                update_post_meta( $post_id, '_price', $value );
            }
        }
    }
}, 10, 2 );

Forge KI-Assistent Online

Hallo! Ich bin der Field Forge KI-Assistent. Fragen Sie mich alles über das Plugin — Einrichtung, Funktionen, Fehlerbehebung oder Entwicklung.

Gerade eben
Unterstützt von Forge KI · Dokumentation durchsuchen