WC_Brands::rest_api_add_brands_to_product
Add brands in product response.
Method of the class: WC_Brands{}
No Hooks.
Returns
null. Nothing (null).
Usage
$WC_Brands = new WC_Brands(); $WC_Brands->rest_api_add_brands_to_product( $product, $request, $creating );
- $product(WC_Data) (required)
- Inserted product object.
- $request(WP_REST_Request) (required)
- Request object.
- $creating(true|false)
- True when creating object, false when updating.
Default:true
WC_Brands::rest_api_add_brands_to_product() WC Brands::rest api add brands to product code WC 10.4.3
public function rest_api_add_brands_to_product( $product, $request, $creating = true ) {
$product_id = is_callable( array( $product, 'get_id' ) ) ? $product->get_id() : ( ! empty( $product->ID ) ? $product->ID : null );
$params = $request->get_params();
$brands = isset( $params['brands'] ) ? $params['brands'] : array();
if ( ! empty( $brands ) ) {
if ( is_array( $brands[0] ) && array_key_exists( 'id', $brands[0] ) ) {
$brands = array_map(
function ( $brand ) {
return absint( $brand['id'] );
},
$brands
);
} else {
$brands = array_map( 'absint', $brands );
}
wp_set_object_terms( $product_id, $brands, 'product_brand' );
}
}