woogc/ps/synchronize_product/child_product
Name: woogc/ps/synchronize_product/child_product
Type: Filter
Arguments:
(object) $child_product
(array) $main_product_data
(int) $origin_product_blog_ID
The woogc/ps/synchronize_product/child_product filter allows developers to modify the child product object immediately before it is returned to the WooCommerce Global Cart product synchronization process and saved on the destination shop.
This provides an opportunity to customize the synchronized product after the child product has been created or updated, but before the synchronization process completes.
The filter can be used to change standard WooCommerce product properties, add or modify custom metadata, synchronize additional product information, or implement custom business logic for individual child shops.
Arguments
$child_product
A WC_Product object representing the product on the destination/child shop. This is the object that can be modified using the standard WooCommerce product methods, such as:
$child_product>set_price( $price ); $child_product>set_stock_quantity( $quantity ); $child_product>set_status( 'publish' ); $child_product>update_meta_data( '_custom_field', $value );
$main_product_data
An array containing the synchronized data of the origin/main product. This can be used to inspect information received from the source product and apply additional logic to the child product.
$origin_product_blog_ID
The WordPress Multisite blog ID of the shop where the origin/main product is located.
Example
The following example increases the child product price by 10%:
add_filter( 'woogc/ps/synchronize_product/child_product', 'custom_modify_child_product', 10, 3 );
function custom_modify_child_product( $child_product, $main_product_data, $origin_product_blog_ID ) {
$regular_price = $child_product->get_regular_price( 'edit' );
if ( '' !== $regular_price ) {
$child_product->set_regular_price( wc_format_decimal( (float) $regular_price * 1.10 ) );
}
$sale_price = $child_product->get_sale_price( 'edit' );
if ( '' !== $sale_price ) {
$child_product->set_sale_price( wc_format_decimal( (float) $sale_price * 1.10 ) );
}
return $child_product;
}
The modified $child_product object must always be returned so that WooCommerce Global Cart can continue the synchronization process.
Modify Different Product Properties
Because the filter provides the actual WC_Product object, standard WooCommerce product methods can be used to modify its properties.
For example, the following changes the product status and adds a custom metadata field:
add_filter( 'woogc/ps/synchronize_product/child_product', 'customize_child_product', 10, 3 );
function customize_child_product( $child_product, $main_product_data, $origin_product_blog_ID ) {
$child_product->set_status( 'draft' );
$child_product->update_meta_data(
'_my_custom_value',
'This value is specific to the child shop'
);
return $child_product;
}
This approach is useful when a destination shop needs to have slightly different product information from the origin shop.
Working With Custom Taxonomies or Metadata
The filter can also be used when additional data needs to be assigned to the child product that is not handled by the standard synchronization process.
For example:
add_filter( 'woogc/ps/synchronize_product/child_product', 'custom_child_product_data', 10, 3 );
function custom_child_product_data( $child_product, $main_product_data, $origin_product_blog_ID ) {
$child_product->update_meta_data(
'_my_custom_field',
'Custom value'
);
return $child_product;
}
For custom taxonomy synchronization, the filter can also be used to retrieve the required taxonomy information from the origin shop and assign it to the synchronized child product.
Important
This filter operates on the child product object. It should therefore be used when the objective is to modify the product being created or updated on the destination shop.
When you need to prevent a particular metadata field or property from being synchronized altogether, use the woogc/ps/synchronize_product/ignore_meta_key filter instead.
When you need to execute custom logic for individual synchronized product properties, the woogc/ps/synchronize_product/synchronize_product_data action may be more appropriate.
The $child_product object should be returned from the filter even when no changes are made:
return $child_product;
This filter is particularly useful for networks where the same product is shared between multiple WooCommerce shops but certain product properties, metadata, pricing rules, or other destination-specific information need to be customized.
Custom code using this filter can be placed in the active theme’s functions.php file or, preferably, in a custom file within:
/wp-content/mu-plugins/

No Comments