WC_REST_Orders_V1_Controller::get_product_id()
Gets the product ID from the SKU or posted ID.
Method of the class: WC_REST_Orders_V1_Controller{}
No Hooks.
Return
Int
.
Usage
// protected - for code of main (parent) or child class $result = $this->get_product_id( $posted, $action );
- $posted(array) (required)
- Request data.
- $action(string)
- 'create' to add line item or 'update' to update it.
Default: 'create'
WC_REST_Orders_V1_Controller::get_product_id() WC REST Orders V1 Controller::get product id code WC 9.8.1
protected function get_product_id( $posted, $action = 'create' ) { if ( ! empty( $posted['sku'] ) ) { $product_id = (int) wc_get_product_id_by_sku( $posted['sku'] ); } elseif ( ! empty( $posted['product_id'] ) && empty( $posted['variation_id'] ) ) { $product_id = (int) $posted['product_id']; } elseif ( ! empty( $posted['variation_id'] ) ) { $product_id = (int) $posted['variation_id']; } elseif ( 'update' === $action ) { $product_id = 0; } else { throw new WC_REST_Exception( 'woocommerce_rest_required_product_reference', __( 'Product ID or SKU is required.', 'woocommerce' ), 400 ); } return $product_id; }