Changeset 3373662
- Timestamp:
- 10/06/2025 12:02:41 PM (6 months ago)
- Location:
- storecontrl-wp-connection/trunk
- Files:
-
- 4 edited
-
includes/api/class-storecontrl-web-api-functions.php (modified) (1 diff)
-
includes/cronjob/class-storecontrl-cronjob-functions.php (modified) (1 diff)
-
readme.txt (modified) (2 diffs)
-
storecontrl-wp-connection.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
storecontrl-wp-connection/trunk/includes/api/class-storecontrl-web-api-functions.php
r3370412 r3373662 1769 1769 { 1770 1770 global $wpdb; 1771 1771 1772 $logging = new StoreContrl_WP_Connection_Logging(); 1772 1773 1773 $existing_product_variation = $wpdb->get_results($wpdb->prepare(" 1774 SELECT * 1775 FROM $wpdb->postmeta 1776 WHERE meta_key = 'storecontrl_size_id' AND meta_value = %s 1777 ORDER BY meta_id DESC 1778 ", $product_variation['sku_id']), ARRAY_A); 1774 $existing_product_variation = $wpdb->get_results( 1775 $wpdb->prepare(" 1776 SELECT * 1777 FROM {$wpdb->postmeta} 1778 WHERE meta_key = 'storecontrl_size_id' AND meta_value = %s 1779 ORDER BY meta_id DESC 1780 ", $product_variation['sku_id']), 1781 ARRAY_A 1782 ); 1783 1784 if (empty($existing_product_variation)) { 1785 $logging->log_file_write('StoreContrl | No variations found for size_id: ' . $product_variation['sku_id']); 1786 return; 1787 } 1788 1789 $stock = isset($product_variation['stock_total']) ? (int) max(0, (int)$product_variation['stock_total']) : 0; 1790 $touched_parents = []; 1779 1791 1780 1792 foreach ($existing_product_variation as $variation) { 1781 1782 if (!empty($variation['post_id'])) { 1783 $variation_post_id = (int) $variation['post_id']; 1784 $product_id = (int) wp_get_post_parent_id($variation_post_id); 1785 1786 if ($product_id <= 0) { 1787 $logging->log_file_write('ERROR | Unknown parent product for variation: ' . $variation_post_id ); 1788 // let op: correcte DELETE syntaxis + prepared helper: 1789 $wpdb->delete($wpdb->postmeta, ['meta_id' => (int)$variation['meta_id']], ['%d']); 1790 continue; 1791 } 1792 1793 $stock = (int) max(0, (int)$product_variation['stock_total']); 1794 1795 // Dit wijzigt alléén voorraad; geen attributen of andere meta. 1796 wc_update_product_stock($variation_post_id, $stock, 'set'); 1797 wc_update_product_stock_status($variation_post_id, $stock > 0 ? 'instock' : 'outofstock'); 1798 1799 $logging->log_file_write( 1800 sprintf('StoreContrl | Update stock of variation: %d / %s to: %d', 1801 $variation_post_id, 1802 $product_variation['sku_id'], 1803 $stock 1804 ) 1805 ); 1806 1807 // Optioneel: backorders en sale-datums alleen via meta bij 0 voorraad 1808 if ($stock === 0) { 1809 $_backorders = get_post_meta($variation_post_id, '_backorders', true); 1810 if ($_backorders !== 'notify' && $_backorders !== 'yes') { 1811 delete_post_meta($variation_post_id, '_sale_price_dates_from'); 1812 delete_post_meta($variation_post_id, '_sale_price_dates_to'); 1813 update_post_meta($variation_post_id, '_backorders', 'no'); 1814 } 1815 } 1816 1817 $this->functions->custom_update_post_meta($variation_post_id, 'latest_update', current_time('Y-m-d H:i')); 1793 if (empty($variation['post_id'])) { 1794 $logging->log_file_write('StoreContrl | Variation row has no post_id for size_id: ' . $product_variation['sku_id']); 1795 continue; 1796 } 1797 1798 $variation_id = (int)$variation['post_id']; 1799 $parent_id = (int)wp_get_post_parent_id($variation_id); 1800 if ($parent_id <= 0) { 1801 $logging->log_file_write('ERROR | Unknown parent product for variation: ' . $variation_id); 1802 continue; 1803 } 1804 1805 // 0) Respecteer bestaande backorders-instelling (variatie, anders parent) 1806 $var_backorders = get_post_meta($variation_id, '_backorders', true); // 'no' | 'notify' | 'yes' | '' 1807 $parent_backorders = get_post_meta($parent_id, '_backorders', true); 1808 $effective_backorders = $var_backorders !== '' ? $var_backorders : ($parent_backorders !== '' ? $parent_backorders : 'no'); 1809 1810 // 1) Manage stock aan op variatie, maar backorders NIET overschrijven 1811 update_post_meta($variation_id, '_manage_stock', 'yes'); 1812 1813 // 2) Voorraad & status via CRUD 1814 $v = wc_get_product($variation_id); 1815 if (!$v || !($v instanceof WC_Product_Variation)) { 1816 $logging->log_file_write('ERROR | Could not load variation object: ' . $variation_id); 1817 continue; 1818 } 1819 1820 $v->set_manage_stock(true); 1821 $v->set_backorders($effective_backorders); // respecteer huidige instelling 1822 $v->set_stock_quantity($stock); 1823 1824 // Bepaal status op basis van qty + backorders 1825 if ($stock > 0) { 1826 $stock_status = 'instock'; 1818 1827 } else { 1819 $logging->log_file_write('StoreContrl | Unable to find variation: ' . $product_variation['sku_id'] . ' to update a new stock total: ' . $product_variation['stock_total']); 1820 } 1821 } 1828 $stock_status = in_array($effective_backorders, ['yes','notify'], true) ? 'onbackorder' : 'outofstock'; 1829 } 1830 $v->set_stock_status($stock_status); 1831 $v->save(); 1832 1833 // 2b) Meta alignen (sommige setups lezen meta direct) 1834 update_post_meta($variation_id, '_stock', $stock); 1835 update_post_meta($variation_id, '_stock_status', $stock_status); 1836 1837 // 3) Alleen bij 0 voorraad EN wanneer backorders NIET toegestaan zijn: sale-datums weg 1838 if ($stock === 0 && !in_array($effective_backorders, ['yes','notify'], true)) { 1839 delete_post_meta($variation_id, '_sale_price_dates_from'); 1840 delete_post_meta($variation_id, '_sale_price_dates_to'); 1841 } 1842 1843 // 4) Lookup bijwerken (variatie + parent) 1844 $datastore = WC_Data_Store::load('product'); 1845 if ($datastore && method_exists($datastore, 'update_lookup_table')) { 1846 $datastore->update_lookup_table($variation_id, 'wc_product_meta_lookup'); 1847 $datastore->update_lookup_table($parent_id, 'wc_product_meta_lookup'); 1848 } 1849 1850 // 5) Transients/caches wissen (variatie + parent) 1851 wc_delete_product_transients($variation_id); 1852 wc_delete_product_transients($parent_id); 1853 clean_post_cache($variation_id); 1854 clean_post_cache($parent_id); 1855 1856 // 6) Laatste update (site-tijd, DST-proof) 1857 $this->functions->custom_update_post_meta($variation_id, 'latest_update', wp_date('Y-m-d H:i')); 1858 1859 // 7) Voor batch-gewijze parent-sync 1860 $touched_parents[$parent_id] = true; 1861 1862 $logging->log_file_write(sprintf( 1863 'StoreContrl | Update stock of variation: %d / %s to %d and set status to %s', 1864 $variation_id, 1865 $product_variation['sku_id'], 1866 $stock, 1867 $stock_status 1868 )); 1869 } 1870 1871 // 8) Parents 1x syncen (aggregaatstatus correct) + caches 1872 if (!empty($touched_parents) && class_exists('WC_Product_Variable')) { 1873 $datastore = WC_Data_Store::load('product'); 1874 foreach (array_keys($touched_parents) as $pid) { 1875 WC_Product_Variable::sync($pid); 1876 if (method_exists('WC_Product_Variable', 'sync_stock_status')) { 1877 WC_Product_Variable::sync_stock_status($pid); 1878 } 1879 if ($datastore && method_exists($datastore, 'update_lookup_table')) { 1880 $datastore->update_lookup_table($pid, 'wc_product_meta_lookup'); 1881 } 1882 wc_delete_product_transients($pid); 1883 clean_post_cache($pid); 1884 } 1885 } 1886 1822 1887 } 1823 1888 -
storecontrl-wp-connection/trunk/includes/cronjob/class-storecontrl-cronjob-functions.php
r3370412 r3373662 1536 1536 1537 1537 // Draai alleen om 01:00 en 12:00 én als er geen lock staat 1538 $hour = (int) current_time('H');1538 $hour = (int)wp_date('H', time(), wp_timezone()); 1539 1539 if (in_array($hour, [1, 12], true) && ! get_transient('update_sale_products_lock')) { 1540 1540 1541 1541 // TTL tot nét voorbij het volgende uur (voorkomt dubbele runs in hetzelfde uur) 1542 $minutes = (int) current_time('i');1542 $minutes = (int)wp_date('i', time(), wp_timezone()); 1543 1543 $ttl = ((60 - $minutes) + 5) * MINUTE_IN_SECONDS; 1544 1544 set_transient('update_sale_products_lock', 1, $ttl); -
storecontrl-wp-connection/trunk/readme.txt
r3370412 r3373662 5 5 Requires at least: 6.6.0 6 6 Tested up to: 6.8.2 7 Stable tag: 4.2. 07 Stable tag: 4.2.1 8 8 Requires PHP: 8.0 9 9 License: GPLv2 or later … … 93 93 == Changelog == 94 94 95 = 4.2.1 = 96 * Bugfix met stock updates en algemene product status 97 95 98 = 4.2.0 = 96 99 * Nieuwe add_action na het verwerken van de afbeelingen: storecontrl_after_gallery_save -
storecontrl-wp-connection/trunk/storecontrl-wp-connection.php
r3370412 r3373662 4 4 Plugin URI: http://www.arture.nl/storecontrl 5 5 Description: The Wordpress plugin for connecting Woocommerce with StoreContrl Cloud. With the synchronizing cronjobs your products will be automatically processed, images added, and the categories set. Every 5 minutes all stock changes are processed. We provide a up-to-date plugin, easy setup and always the best support. 6 Version: 4.2. 06 Version: 4.2.1 7 7 Requires Plugins: woocommerce 8 8 Author: Arture
Note: See TracChangeset
for help on using the changeset viewer.