Untitled
webwizards
plain_text
3 months ago
2.9 kB
6
Indexable
$b2bking_extra_currencies = [ 'PLN', 'EUR']; // configure currencies here
// ── 1. Frontend: swap b2bking group price meta with currency-specific value ──
add_filter( 'get_post_metadata', function ( $value, $post_id, $meta_key, $single ) use ( $b2bking_extra_currencies ) {
if ( ! preg_match( '/^b2bking_(regular|sale)_product_price_group_\d+$/', $meta_key ) ) {
return $value;
}
$currency = get_woocommerce_currency();
if ( ! in_array( $currency, $b2bking_extra_currencies, true ) ) {
return $value;
}
$price = get_post_meta( $post_id, $meta_key . '_' . strtolower( $currency ), true );
return $price !== '' ? ( $single ? $price : [ $price ] ) : $value;
}, 10, 4 );
// ── 2. Admin: add currency fields — helpers ──────────────────────────────────
function b2bking_currency_fields( $post_id, $currencies ) {
$groups = get_posts( [ 'post_type' => 'b2bking_group', 'numberposts' => -1, 'post_status' => 'publish' ] );
foreach ( $groups as $g ) {
echo '<p class="form-field"><strong>' . esc_html( $g->post_title ) . ' — Multi-Currency</strong></p>';
foreach ( $currencies as $cur ) {
foreach ( [ 'regular' => 'Regular', 'sale' => 'Sale' ] as $type => $label ) {
$key = "b2bking_{$type}_product_price_group_{$g->ID}_" . strtolower( $cur );
woocommerce_wp_text_input( [
'id' => $key,
'label' => "{$g->post_title} {$label} ({$cur})",
'class' => 'short wc_input_price',
'value' => get_post_meta( $post_id, $key, true ),
] );
}
}
}
}
function b2bking_currency_save( $post_id, $currencies ) {
$groups = get_posts( [ 'post_type' => 'b2bking_group', 'numberposts' => -1, 'post_status' => 'publish' ] );
foreach ( $groups as $g ) {
foreach ( $currencies as $cur ) {
foreach ( [ 'regular', 'sale' ] as $type ) {
$key = "b2bking_{$type}_product_price_group_{$g->ID}_" . strtolower( $cur );
if ( isset( $_POST[ $key ] ) ) {
update_post_meta( $post_id, $key, wc_clean( wp_unslash( $_POST[ $key ] ) ) );
}
}
}
}
}
// Simple products
add_action( 'woocommerce_product_options_pricing', function () use ( $b2bking_extra_currencies ) {
global $post;
b2bking_currency_fields( $post->ID, $b2bking_extra_currencies );
}, 15 );
add_action( 'woocommerce_process_product_meta', function ( $post_id ) use ( $b2bking_extra_currencies ) {
b2bking_currency_save( $post_id, $b2bking_extra_currencies );
} );
// Variations
add_action( 'woocommerce_variation_options_pricing', function ( $loop, $variation_data, $variation ) use ( $b2bking_extra_currencies ) {
b2bking_currency_fields( $variation->ID, $b2bking_extra_currencies );
}, 15, 3 );
add_action( 'woocommerce_save_product_variation', function ( $variation_id ) use ( $b2bking_extra_currencies ) {
b2bking_currency_save( $variation_id, $b2bking_extra_currencies );
} );Editor is loading...
Leave a Comment