Created
January 17, 2020 01:47
-
-
Save yousan/aefb4b5388fdc60b5c35c2739cb01113 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: TM WooCommerce Product API Link Disable Duplicate | |
* Plugin URI: | |
* Description: Disable product syncing when WooCommerce duplicate. When duplicating a product with WooCommerce basic function, it copies synced ID. Then original and duplicated product has same destination ID. This plugin removes the product ID then disable syncing for new duplcated product. | |
* Version: 1.0.0 | |
* Author: yousan | |
*/ | |
// @see ../woocommerce/includes/admin/class-wc-admin-duplicate-product.php | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( class_exists( 'TWPALDD', false ) ) { | |
TWPALDD::register_hook(); | |
} | |
class TWPALDD { | |
public static function register_hook() { | |
add_action( 'woocommerce_product_duplicate_before_save', array( self::class, 'disable' ), 10, 2 ); | |
} | |
/** | |
* Delete synced product_id meta key from duplicated product. | |
* The $duplicate variable is passed as reference, so this hook do action without returns. | |
* | |
* @param WC_Product $duplicate | |
* @param WC_Product $product | |
*/ | |
public static function disable( $duplicate, $product ) { | |
/** @var WC_Meta_Data $meta */ | |
foreach ( $duplicate->get_meta_data() as $array_key => $meta ){ | |
if ( FALSE !== strpos($meta->key, 'tm_woo_api_dest_product_id_' ) ) { | |
$duplicate->delete_meta_data($meta->key); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment