Skip to content

Instantly share code, notes, and snippets.

@PluginRepublicSupport
Created March 8, 2023 06:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PluginRepublicSupport/237055334c95a6e64ee8ad4e12b3638c to your computer and use it in GitHub Desktop.
Save PluginRepublicSupport/237055334c95a6e64ee8ad4e12b3638c to your computer and use it in GitHub Desktop.
For compatibility between Product Add-Ons Ultimate and Polylang
<?php
/**
* Duplicates groups and fields for translated products. Original product should have duplicated groups and fields by this time.
*/
function pewc_product_duplicate_polylang( $duplicate, $product ) {
// check that this function exists
if ( function_exists( 'pll_get_post_translations' ) ) {
$new_id = $duplicate->get_id();
$old_id = $product->get_id();
$new_trs = pll_get_post_translations( $new_id );
$old_trs = pll_get_post_translations( $old_id );
// Are we duplicating groups and fields, i.e. cloning them and assigning new IDs
$do_duplication = apply_filters( 'pewc_duplicate_fields', true, false );
if ( ! empty( $new_trs ) && $do_duplication ) {
foreach ( $new_trs as $lang => $id ) {
// we only duplicate fields for the translated products
if ( $id != $new_id ) {
// default source is the original parent product
$source = $product;
if ( ! empty( $old_trs[$lang] ) && 'product' == get_post_type( $old_trs[$lang] ) ) {
// the source of the add-ons would be the translated product
$source = wc_get_product( $old_trs[$lang] );
}
pewc_duplicate_groups_and_fields( wc_get_product( $id ), $source, true );
update_post_meta( $id, 'pewc_pll_duplicated_fields', 'yes' );
}
}
}
}
}
add_action( 'woocommerce_product_duplicate', 'pewc_product_duplicate_polylang', 11, 2 );
/**
* snippet for when creating a translation of a product:
*/
function pewc_duplicate_fields_on_new_translation( $from, $to, $lang ) {
if ( isset( $GLOBALS['pagenow'], $_GET['from_post'], $_GET['new_lang'] ) && 'post-new.php' === $GLOBALS['pagenow'] ) {
// Are we duplicating groups and fields, i.e. cloning them and assigning new IDs
$do_duplication = apply_filters( 'pewc_duplicate_fields', true, false );
if( $do_duplication ) {
pewc_duplicate_groups_and_fields( wc_get_product( $to ), wc_get_product( $from ), true );
update_post_meta( $to, 'pewc_pll_duplicated_fields', 'yes' );
}
update_option( 'pewc_duplication_notice', 1 );
}
}
add_action( 'pllwc_copy_product', 'pewc_duplicate_fields_on_new_translation', 11, 4 );
/**
* snippet for Global Add-On Fields. Must use Global Groups (edit group as post type)
*/
function pewc_pll_duplicate_global_group_fields( $post_ID, $post, $update ) {
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
return;
}
if ( $post->post_type == 'pewc_group' && $post->post_parent == 0 && $GLOBALS['pagenow'] == 'post-new.php' && isset( $_GET['post_type'], $_GET['from_post'], $_GET['new_lang'] ) && $_GET['post_type'] == 'pewc_group' ) {
// a new translation for a global group has been created, create new field IDs. Idea taken from pewc_duplicate_group()
$old_group_id = (int) $_GET['from_post'];
$old_group = get_post( $old_group_id );
if ( $old_group->post_type == 'pewc_group' && $old_group->post_parent == 0 ) {
// let's only do this for global groups
// Check if the duplicated group has fields
$duplicated_fields = get_post_meta( $old_group_id, 'field_ids', true );
$mapped_fields = array();
if( $duplicated_fields ) {
// Duplicate each field
foreach( $duplicated_fields as $old_field_id ) {
$new_field_id = pewc_duplicate_field_by_id( $old_field_id );
// Make an array to map old field IDs to their duplicated versions
$mapped_fields[$old_field_id] = $new_field_id;
}
}
$updated = update_post_meta( $post_ID, 'field_ids', array_values( $mapped_fields ) );
update_post_meta( $post_ID, 'pewc_pll_duplicated_fields', 'yes' );
if ( ! empty( $mapped_fields ) ) {
$new_field_ids = array_values( $mapped_fields );
// find calculation fields and update their formulas with new field IDs if needed
foreach ( $new_field_ids as $fid ) {
$ftype = get_post_meta( $fid, 'field_type', true );
if ( 'calculation' === $ftype ) {
// this is a calculation field, let's try to replace the fields in the formula if they exist
$formula = get_post_meta ( $fid, 'formula', true );
if ( false !== strpos( $formula, '{field_' ) ) {
// the formula has fields that we can replace
$new_formula = $formula;
foreach ( $mapped_fields as $ofid => $nfid ) {
$new_formula = str_replace( '{field_'.$ofid.'}', '{field_'.$nfid.'}', $new_formula );
$new_formula = str_replace( '{field_'.$ofid.'_', '{field_'.$nfid.'_', $new_formula );
}
update_post_meta( $fid, 'formula', sanitize_text_field( $new_formula ) );
}
}
}
}
}
}
}
add_action( 'wp_insert_post', 'pewc_pll_duplicate_global_group_fields', 100, 3 );
/**
* snippet for Global Groups, when copying an existing group
*/
function pewc_pll_prevent_post_metas( $keys, $sync, $from, $to, $lang ) {
$new_keys = array();
foreach ( $keys as $key ) {
if ( $key != 'field_ids' ) {
// don't copy field_ids because it overwrites our changes
$new_keys[] = $key;
}
}
return $new_keys;
}
add_filter( 'pll_copy_post_metas', 'pewc_pll_prevent_post_metas', 100, 5 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment