Attachments.php
1 year ago
Attributes.php
4 months ago
DownloadableFiles.php
4 months ago
Linked.php
10 months ago
Meta.php
1 year ago
Post.php
3 months ago
Stock.php
1 year ago
Synchronizer.php
1 year ago
SynchronizerForMeta.php
8 months ago
Taxonomies.php
4 months ago
VariationAttachments.php
1 year ago
VariationMeta.php
8 months ago
VariationTaxonomies.php
8 months ago
Variations.php
1 month ago
Stock.php
53 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WCML\Synchronization\Component; |
| 4 | |
| 5 | class Stock extends SynchronizerForMeta { |
| 6 | |
| 7 | const STOCK_META_KEY = '_stock'; |
| 8 | const STOCK_STATUS_META_KEY = '_stock_status'; |
| 9 | |
| 10 | /** |
| 11 | * @param \WP_Post $product |
| 12 | * @param int[] $translationsIds |
| 13 | * @param array<int,string> $translationsLanguages |
| 14 | */ |
| 15 | public function run( $product, $translationsIds, $translationsLanguages ) { |
| 16 | $productObject = wc_get_product( $product->ID ); |
| 17 | $productIdManagingStock = $productObject->get_stock_managed_by_id(); |
| 18 | |
| 19 | // Assume that the translation of the product managing stock is the product managing stock of the translations. |
| 20 | $translationsManagingStock = $translationsIds; |
| 21 | if ( $productIdManagingStock !== $product->ID ) { |
| 22 | $translationsManagingStock = $this->elementTranslations->get_element_translations( $productIdManagingStock, false, false ); |
| 23 | } |
| 24 | |
| 25 | $this->synchronizeMeta( $productIdManagingStock, $translationsManagingStock, self::STOCK_META_KEY ); |
| 26 | $this->synchronizeMeta( $product->ID, $translationsIds, self::STOCK_STATUS_META_KEY ); |
| 27 | |
| 28 | if ( $productIdManagingStock !== $product->ID ) { |
| 29 | $this->deleteCache( $productIdManagingStock ); |
| 30 | foreach ( $translationsManagingStock as $translationManagingStock ) { |
| 31 | $this->deleteCache( $translationManagingStock ); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | delete_transient( 'wc_low_stock_count' ); |
| 36 | delete_transient( 'wc_outofstock_count' ); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @param int $productId |
| 41 | * |
| 42 | * @todo Collect IDs that need cache deleting, and delete on shutdown. Note that variations might be firing this multiple times for their parent product. |
| 43 | */ |
| 44 | private function deleteCache( $productId ) { |
| 45 | wp_cache_delete( $productId, 'post_meta' ); |
| 46 | wp_cache_delete( 'product-' . $productId, 'products' ); |
| 47 | delete_transient( 'wc_product_children_' . $productId ); |
| 48 | $wcml_data_store = wcml_product_data_store_cpt(); |
| 49 | $wcml_data_store->update_lookup_table_data( $productId ); |
| 50 | } |
| 51 | |
| 52 | } |
| 53 |