Merge pull request #27038 from woocommerce/add/26192
Add existing meta keys to woocommerce_duplicate_product_exclude_meta filter
This commit is contained in:
commit
24bd19c517
|
@ -45,8 +45,8 @@
|
||||||
},
|
},
|
||||||
"autoload-dev": {
|
"autoload-dev": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"Automattic\\WooCommerce\\Tests\\": "tests/php/src",
|
"Automattic\\WooCommerce\\Tests\\": "tests/php/src/",
|
||||||
"Automattic\\WooCommerce\\Testing\\Tools\\": "tests/Tools"
|
"Automattic\\WooCommerce\\Testing\\Tools\\": "tests/Tools/"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|
|
@ -129,11 +129,24 @@ class WC_Admin_Duplicate_Product {
|
||||||
*/
|
*/
|
||||||
public function product_duplicate( $product ) {
|
public function product_duplicate( $product ) {
|
||||||
/**
|
/**
|
||||||
* Filter to allow us to unset/remove data we don't want to copy to the duplicate.
|
* Filter to allow us to exclude meta keys from product duplication..
|
||||||
*
|
*
|
||||||
|
* @param array $exclude_meta The keys to exclude from the duplicate.
|
||||||
|
* @param array $existing_meta_keys The meta keys that the product already has.
|
||||||
* @since 2.6
|
* @since 2.6
|
||||||
*/
|
*/
|
||||||
$meta_to_exclude = array_filter( apply_filters( 'woocommerce_duplicate_product_exclude_meta', array() ) );
|
$meta_to_exclude = array_filter(
|
||||||
|
apply_filters(
|
||||||
|
'woocommerce_duplicate_product_exclude_meta',
|
||||||
|
array(),
|
||||||
|
array_map(
|
||||||
|
function ( $datum ) {
|
||||||
|
return $datum->key;
|
||||||
|
},
|
||||||
|
$product->get_meta_data()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
$duplicate = clone $product;
|
$duplicate = clone $product;
|
||||||
$duplicate->set_id( 0 );
|
$duplicate->set_id( 0 );
|
||||||
|
|
|
@ -0,0 +1,32 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Unit tests for the WC_Admin_Duplicate_Product class.
|
||||||
|
*
|
||||||
|
* @package WooCommerce\Tests\Admin
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class WC_Admin_Duplicate_Product_Test
|
||||||
|
*/
|
||||||
|
class WC_Admin_Duplicate_Product_Test extends WC_Unit_Test_Case {
|
||||||
|
/**
|
||||||
|
* Tests that the filter will exclude metadata from the duplicate as-expected.
|
||||||
|
*/
|
||||||
|
public function test_filter_allows_excluding_metadata_from_duplicate() {
|
||||||
|
$product = WC_Helper_Product::create_simple_product();
|
||||||
|
$product->add_meta_data( 'test_data', 'test' );
|
||||||
|
|
||||||
|
$filter = function ( $exclude_meta, $existing_meta_keys ) {
|
||||||
|
$this->assertContains( 'test_data', $existing_meta_keys );
|
||||||
|
return array( 'test_data' );
|
||||||
|
};
|
||||||
|
add_filter( 'woocommerce_duplicate_product_exclude_meta', $filter, 10, 2 );
|
||||||
|
|
||||||
|
$duplicate = ( new WC_Admin_Duplicate_Product() )->product_duplicate( $product );
|
||||||
|
|
||||||
|
remove_filter( 'woocommerce_duplicate_product_exclude_meta', $filter );
|
||||||
|
|
||||||
|
$this->assertNotEquals( $product->get_id(), $duplicate->get_id() );
|
||||||
|
$this->assertEmpty( $duplicate->get_meta_data() );
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue