44. Field Forge avec WooCommerce | Field Forge - Champs personnalisés, conçus pour la vitesse
Télécharger Se connecter

44. Field Forge avec WooCommerce

Intégrez les champs personnalisés de Field Forge avec les produits WooCommerce.

Ajout de Champs aux Produits

Créez un groupe de champs ciblant le type de publication product :

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' ] ],
    ],
] );

Affichage des Champs sur les Pages de Produits

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;
} );

Synchronisation des Champs de Prix

WooCommerce lit les prix à partir de _price et _regular_price postmeta. Synchronisez les valeurs Field Forge :

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 );

Assistant IA Forge En ligne

Bonjour ! Je suis l'assistant IA Field Forge. Posez-moi n'importe quelle question sur le plugin — configuration, fonctionnalités, dépannage ou développement.

À l'instant
Propulsé par Forge IA · Parcourir la documentation