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rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Internal / CostOfGoodsSold / CostOfGoodsSoldController.php
woocommerce / src / Internal / CostOfGoodsSold Last commit date
CogsAwareRestControllerTrait.php 1 year ago CogsAwareTrait.php 1 year ago CogsAwareUnitTestSuiteTrait.php 1 year ago CostOfGoodsSoldController.php 9 months ago
CostOfGoodsSoldController.php
163 lines
1 <?php
2 declare( strict_types=1 );
3
4 namespace Automattic\WooCommerce\Internal\CostOfGoodsSold;
5
6 use Automattic\WooCommerce\Enums\FeaturePluginCompatibility;
7 use Automattic\WooCommerce\Internal\Features\FeaturesController;
8 use Automattic\WooCommerce\Internal\RegisterHooksInterface;
9
10 /**
11 * Main controller for the Cost of Goods Sold feature.
12 */
13 class CostOfGoodsSoldController implements RegisterHooksInterface {
14
15 /**
16 * The instance of FeaturesController to use.
17 *
18 * @var FeaturesController
19 */
20 private FeaturesController $features_controller;
21
22 /**
23 * Register hooks.
24 */
25 public function register() {
26 add_filter( 'woocommerce_debug_tools', array( $this, 'add_debug_tools_entry' ), 999, 1 );
27 }
28
29 /**
30 * Initialize the instance, runs when the instance is created by the dependency injection container.
31 *
32 * @internal
33 * @param FeaturesController $features_controller The instance of FeaturesController to use.
34 */
35 final public function init( FeaturesController $features_controller ) {
36 $this->features_controller = $features_controller;
37 }
38
39 /**
40 * Is the Cost of Goods Sold engine enabled?
41 *
42 * @return bool True if the engine is enabled, false otherwise.
43 */
44 public function feature_is_enabled(): bool {
45 return $this->features_controller->feature_is_enabled( 'cost_of_goods_sold' );
46 }
47
48 /**
49 * Add the feature information for the features settings page.
50 *
51 * @param FeaturesController $features_controller The instance of FeaturesController to use.
52 *
53 * @internal For exclusive usage of WooCommerce core, backwards compatibility not guaranteed.
54 */
55 public function add_feature_definition( $features_controller ) {
56 $definition = array(
57 'description' => __( 'Allows entering cost of goods sold information for products.', 'woocommerce' ),
58 'is_experimental' => false,
59 'enabled_by_default' => false,
60 'default_plugin_compatibility' => FeaturePluginCompatibility::COMPATIBLE,
61 );
62
63 $features_controller->add_feature_definition(
64 'cost_of_goods_sold',
65 __( 'Cost of Goods Sold', 'woocommerce' ),
66 $definition
67 );
68 }
69
70 /**
71 * Add the entry for "add/remove COGS value column to/from the product meta lookup table" to the WooCommerce admin tools.
72 *
73 * @internal Hook handler, not to be explicitly used from outside the class.
74 *
75 * @param array $tools_array Array to add the tool to.
76 * @return array Updated tools array.
77 */
78 public function add_debug_tools_entry( array $tools_array ): array {
79 // If the feature is disabled we show the tool for removing the column, but not for adding it.
80 $column_exists = $this->product_meta_lookup_table_cogs_value_columns_exist();
81 if ( ! $this->feature_is_enabled() && ! $column_exists ) {
82 return $tools_array;
83 }
84
85 $tools_array['generate_cogs_value_meta_column'] = array(
86 'name' => $column_exists ?
87 __( 'Remove COGS columns from the product meta lookup table', 'woocommerce' ) :
88 __( 'Create COGS columns in the product meta lookup table', 'woocommerce' ),
89 'button' => $column_exists ?
90 __( 'Remove columns', 'woocommerce' ) :
91 __( 'Create columns', 'woocommerce' ),
92 'desc' =>
93 $column_exists ?
94 __( 'This tool will remove the Cost of Goods Sold (COGS) related columns from the product meta lookup table. COGS will continue working (if the feature is enabled) but some functionality will not be available.', 'woocommerce' ) :
95 __( 'This tool will generate the necessary Cost of Goods Sold (COGS) related columns in the product meta lookup table, and populate them from existing product data.', 'woocommerce' ),
96 'callback' =>
97 $column_exists ? array( $this, 'remove_lookup_cogs_columns' ) : array( $this, 'generate_lookup_cogs_columns' ),
98 );
99
100 return $tools_array;
101 }
102
103 /**
104 * Handler for the "add COGS value column to the product meta lookup table" admin tool.
105 *
106 * @internal Tool callback, not to be explicitly used from outside the class.
107 */
108 public function generate_lookup_cogs_columns() {
109 global $wpdb;
110
111 if ( $this->feature_is_enabled() && ! $this->product_meta_lookup_table_cogs_value_columns_exist() ) {
112 $wpdb->query( "ALTER TABLE {$wpdb->prefix}wc_product_meta_lookup ADD COLUMN cogs_total_value DECIMAL(19,4)" );
113 $wpdb->query(
114 "UPDATE {$wpdb->prefix}wc_product_meta_lookup AS lookup
115 JOIN {$wpdb->prefix}postmeta AS pm ON lookup.product_id = pm.post_id
116 SET lookup.cogs_total_value = CAST(pm.meta_value AS DECIMAL(19, 4))
117 WHERE pm.meta_key = '_cogs_total_value';"
118 );
119 }
120 }
121
122 /**
123 * Handler for the "remove COGS value column to the product meta lookup table" admin tool.
124 *
125 * @internal Tool callback, not to be explicitly used from outside the class.
126 */
127 public function remove_lookup_cogs_columns() {
128 global $wpdb;
129
130 if ( $this->product_meta_lookup_table_cogs_value_columns_exist() ) {
131 $wpdb->query( "ALTER TABLE {$wpdb->prefix}wc_product_meta_lookup DROP COLUMN cogs_total_value" );
132 }
133 }
134
135 /**
136 * Tells if the COGS value column exists in the product meta lookup table.
137 *
138 * @return bool True if the column exists, false otherwise.
139 */
140 public function product_meta_lookup_table_cogs_value_columns_exist(): bool {
141 global $wpdb;
142
143 return (bool) $wpdb->get_var(
144 $wpdb->prepare(
145 "SHOW COLUMNS FROM {$wpdb->prefix}wc_product_meta_lookup LIKE %s",
146 'cogs_total_value'
147 )
148 );
149 }
150
151 /**
152 * Get the tooltip text for the COGS value field in the product editor.
153 *
154 * @param bool $for_variable_products True to get the value for variable products, false for other types of products.
155 * @return string The string to use as tooltip (translated but not escaped).
156 */
157 public function get_general_cost_edit_field_tooltip( bool $for_variable_products ) {
158 return $for_variable_products ?
159 __( 'Add the amount it costs you to buy or make this product. This will be applied as the default value for variations.', 'woocommerce' ) :
160 __( 'Add the amount it costs you to buy or make this product.', 'woocommerce' );
161 }
162 }
163