Інтегруйте користувацькі поля Field Forge з продуктами WooCommerce.
Додавання полів до продуктів
Створіть групу полів, націлену на тип запису 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' ] ],
],
] );Відображення полів на сторінках продуктів
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;
} );Синхронізація полів ціни
WooCommerce читає ціни з _price та _regular_price postmeta. Синхронізуйте значення 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 );—