Home / eCommerce / WWPP & WPML: bulk sync wholesale prices to translated products
Duplicate Snippet

Embed Snippet on Your Site

WWPP & WPML: bulk sync wholesale prices to translated products

Please review the following before using this solution:
1. This is a custom snippet provided as-is, please test it on a staging copy of your site first.
2. It will overwrite any wholesale prices that have been manually set on translated products with the values from the primary-language products.
3. If you intentionally use different wholesale prices per language, do not use this snippet.
4. Remove the snippet from functions.php immediately after running it.

Code Preview
php
<?php
// WooCommerce Wholesale Prices Premium × WPML — bulk sync wholesale prices to translated products.
// Add to functions.php. Visit: /wp-admin/?wwp_wpml_sync=1 as admin. Remove after use.
add_action( 'init', function () {
    if ( ! current_user_can( 'manage_options' ) || empty( $_GET['wwp_wpml_sync'] ) ) {
        return;
    }
    global $wpdb, $wc_wholesale_prices;
    if ( ! function_exists( 'icl_object_id' ) ) {
        wp_die( 'WPML is not installed or not active.' );
    }
    $icl_table = $wpdb->prefix . 'icl_translations';
    if ( ! $wpdb->get_var( "SHOW TABLES LIKE '{$icl_table}'" ) ) {
        wp_die( "WPML table {$icl_table} not found." );
    }
    @set_time_limit( 0 );
    $roles     = array_keys( $wc_wholesale_prices->wwp_wholesale_roles->getAllRegisteredWholesaleRoles() );
    $meta_keys = [ 'wwpp_product_wholesale_visibility_filter' ];
    foreach ( $roles as $role ) {
        $meta_keys[] = $role . '_wholesale_price';
        $meta_keys[] = $role . '_have_wholesale_price';
        $meta_keys[] = $role . '_have_wholesale_price_set_by_product_cat';
        $meta_keys[] = $role . '_wholesale_minimum_order_quantity';
        $meta_keys[] = $role . '_wholesale_order_quantity_step';
    }
    $primary_ids = $wpdb->get_col(
        "SELECT element_id
         FROM {$icl_table}
         WHERE element_type IN ('post_product','post_product_variation')
         AND source_language_code IS NULL"
    );
    if ( empty( $primary_ids ) ) {
        wp_die( 'No primary-language products found in icl_translations.' );
    }
    $synced_products = 0;
    $synced_entries  = 0;
    foreach ( $primary_ids as $primary_id ) {
        $trid = $wpdb->get_var(
            $wpdb->prepare(
                "SELECT trid FROM {$icl_table} WHERE element_id = %d",
                $primary_id
            )
        );
        if ( ! $trid ) {
            continue;
        }
        $translated_ids = $wpdb->get_col(
            $wpdb->prepare(
                "SELECT element_id
                 FROM {$icl_table}
                 WHERE trid = %d
                 AND element_id != %d",
                $trid,
                $primary_id
            )
        );
        if ( empty( $translated_ids ) ) {
            continue;
        }
        foreach ( $meta_keys as $key ) {
            $value = get_post_meta( (int) $primary_id, $key, true );
            foreach ( $translated_ids as $t_id ) {
                if ( $value !== '' && $value !== false ) {
                    update_post_meta( (int) $t_id, $key, $value );
                } else {
                    delete_post_meta( (int) $t_id, $key );
                }
                $synced_entries++;
            }
        }
        $synced_products++;
    }
    wp_die(
        "Done. {$synced_products} products/variations processed, {$synced_entries} meta entries synced. Remove this snippet from functions.php."
    );
} );

Comments

Add a Comment