Hooks.php
38 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WCML\Products; |
| 4 | |
| 5 | use WPML\FP\Obj; |
| 6 | use WPML\LIB\WP\Hooks as WpHooks; |
| 7 | use function WPML\FP\spreadArgs; |
| 8 | |
| 9 | class Hooks implements \IWPML_Frontend_Action, \IWPML_Backend_Action { |
| 10 | public function add_hooks() { |
| 11 | WpHooks::onFilter( 'woocommerce_variable_children_args' ) |
| 12 | ->then( spreadArgs( self::forceProductLanguageInQuery() ) ); |
| 13 | |
| 14 | WpHooks::onAction( 'woocommerce_after_product_object_save' ) |
| 15 | ->then( spreadArgs( [ $this, 'flushPostVariationCache' ] ) ); |
| 16 | } |
| 17 | |
| 18 | private static function forceProductLanguageInQuery() { |
| 19 | return function( $args ) { |
| 20 | return Obj::assoc( |
| 21 | 'wpml_lang', |
| 22 | apply_filters( 'wpml_element_language_code', '', [ 'element_id' => Obj::prop( 'post_parent', $args ), 'element_type' => 'product_variation' ] ), |
| 23 | $args |
| 24 | ); |
| 25 | }; |
| 26 | } |
| 27 | |
| 28 | public function flushPostVariationCache( $product ) { |
| 29 | if ( ! $product instanceof \WC_Product_Variable ) { |
| 30 | return; |
| 31 | } |
| 32 | |
| 33 | foreach ( (array) $product->get_children() as $variationId ) { |
| 34 | wp_cache_delete( $variationId, 'posts' ); |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 |