CogsAwareRestControllerTrait.php
1 year ago
CogsAwareTrait.php
1 year ago
CogsAwareUnitTestSuiteTrait.php
1 year ago
CostOfGoodsSoldController.php
9 months ago
CogsAwareRestControllerTrait.php
122 lines
| 1 | <?php |
| 2 | declare( strict_types=1 ); |
| 3 | |
| 4 | namespace Automattic\WooCommerce\Internal\CostOfGoodsSold; |
| 5 | |
| 6 | /** |
| 7 | * Trait with Cost of Goods Sold related functionality shared by the REST products and variations controllers. |
| 8 | */ |
| 9 | trait CogsAwareRestControllerTrait { |
| 10 | |
| 11 | use CogsAwareTrait; |
| 12 | |
| 13 | /** |
| 14 | * Add Cost of Goods Sold related information for a given product to the array of data that will become the REST response. |
| 15 | * |
| 16 | * @param array $data Array of response data. |
| 17 | * @param WC_Product $product Product to get the information from. |
| 18 | */ |
| 19 | private function add_cogs_info_to_returned_product_data( array &$data, $product ): void { |
| 20 | if ( ! $this->cogs_is_enabled() ) { |
| 21 | return; |
| 22 | } |
| 23 | |
| 24 | $data['cost_of_goods_sold'] = array( |
| 25 | 'values' => array( |
| 26 | array( |
| 27 | 'defined_value' => $product->get_cogs_value(), |
| 28 | 'effective_value' => $product->get_cogs_effective_value(), |
| 29 | ), |
| 30 | ), |
| 31 | 'total_value' => $product->get_cogs_total_value(), |
| 32 | ); |
| 33 | |
| 34 | if ( $product instanceof \WC_Product_Variation ) { |
| 35 | $data['cost_of_goods_sold']['defined_value_is_additive'] = $product->get_cogs_value_is_additive(); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Apply Cost of Goods Sold related information received in the request body to a product object. |
| 41 | * |
| 42 | * @param WP_Rest_Request $request Request data. |
| 43 | * @param WC_Product $product The product to apply the data to. |
| 44 | */ |
| 45 | private function set_cogs_info_in_product_object( $request, $product ): void { |
| 46 | $values = $request['cost_of_goods_sold']['values'] ?? null; |
| 47 | if ( ! is_null( $values ) ) { |
| 48 | $value = 0; |
| 49 | foreach ( $values as $value_info ) { |
| 50 | $value += (float) ( $value_info['defined_value'] ?? 0 ); |
| 51 | } |
| 52 | |
| 53 | $product->set_cogs_value( $value ); |
| 54 | } |
| 55 | |
| 56 | if ( $product instanceof \WC_Product_Variation ) { |
| 57 | $is_additive = $request['cost_of_goods_sold']['defined_value_is_additive'] ?? null; |
| 58 | if ( ! is_null( $is_additive ) ) { |
| 59 | $product->set_cogs_value_is_additive( $is_additive ); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Add Cost of Goods Sold related schema information to a given REST endpoint schema. |
| 66 | * |
| 67 | * @param array $schema The schema data set to add the information to. |
| 68 | * @param bool $for_variations_controller True if the information is for an endpoint in the variations controller. |
| 69 | * @return array Updated schema information. |
| 70 | */ |
| 71 | private function add_cogs_related_product_schema( array $schema, bool $for_variations_controller ): array { |
| 72 | $schema['properties']['cost_of_goods_sold'] = array( |
| 73 | 'description' => __( 'Cost of Goods Sold data.', 'woocommerce' ), |
| 74 | 'type' => 'object', |
| 75 | 'context' => array( 'view', 'edit' ), |
| 76 | 'properties' => array( |
| 77 | 'values' => array( |
| 78 | 'description' => __( 'Cost of Goods Sold values for the product.', 'woocommerce' ), |
| 79 | 'type' => 'array', |
| 80 | 'context' => array( 'view', 'edit' ), |
| 81 | 'items' => array( |
| 82 | 'type' => 'object', |
| 83 | 'properties' => array( |
| 84 | 'defined_value' => array( |
| 85 | 'description' => __( 'Defined cost value.', 'woocommerce' ), |
| 86 | 'type' => 'number', |
| 87 | 'context' => array( 'view', 'edit' ), |
| 88 | ), |
| 89 | 'effective_value' => array( |
| 90 | 'description' => __( 'Effective monetary cost value.', 'woocommerce' ), |
| 91 | 'type' => 'number', |
| 92 | 'context' => array( 'view', 'edit' ), |
| 93 | 'readonly' => true, |
| 94 | ), |
| 95 | ), |
| 96 | ), |
| 97 | |
| 98 | ), |
| 99 | 'defined_value_is_additive' => array( |
| 100 | 'description' => __( 'Applies to variations only. If true, the effective value is the base value from the parent product plus the defined value; if false, the defined value is the final effective value.', 'woocommerce' ), |
| 101 | 'type' => 'boolean', |
| 102 | 'default' => false, |
| 103 | 'context' => array( 'view', 'edit' ), |
| 104 | ), |
| 105 | 'total_value' => array( |
| 106 | 'description' => __( 'Total monetary value of the Cost of Goods Sold for the product (sum of all the effective values).', 'woocommerce' ), |
| 107 | 'type' => 'number', |
| 108 | 'context' => array( 'view', 'edit' ), |
| 109 | 'readonly' => true, |
| 110 | ), |
| 111 | ), |
| 112 | ); |
| 113 | |
| 114 | if ( $for_variations_controller ) { |
| 115 | $schema['properties']['cost_of_goods_sold']['properties']['defined_value_is_additive']['description'] = |
| 116 | __( 'If true, the effective value is the base value from the parent product plus the defined value; if false, the defined value is the final effective value.', 'woocommerce' ); |
| 117 | } |
| 118 | |
| 119 | return $schema; |
| 120 | } |
| 121 | } |
| 122 |