我想用售价价格替换我的WooCommerce商店的所有常规价格,并删除 wp_postmeta
表格下的促销价格值。
post_id
列对于每种产品的价格都相同,而要复制的列 _sale_price
为 {{1 <}> 并删除 _price
。
我可以运行什么查询来实现此目的?
答案 0 :(得分:3)
这是您正在寻找的纯MySQL查询:
首先,您必须使用自我加入来更新 _price
和 _regular_price
字段
UPDATE `wp_postmeta` AS a,
`wp_postmeta` AS b
SET a.`meta_value` = b.meta_value
WHERE a.post_id = b.post_id
AND a.meta_key IN ('_price', '_regular_price')
AND b.meta_key = '_sale_price'
AND b.meta_value != ''
AND a.`post_id` IN
(SELECT `ID`
FROM `wp_posts`
WHERE `post_type` = 'product'
AND `post_status` = 'publish');
现在要重置销售价格和日期,您必须将其设置为空白
UPDATE `wp_postmeta`
SET `meta_value` = ''
WHERE `meta_key` IN ('_sale_price', '_sale_price_dates_from', '_sale_price_dates_to')
AND `post_id` IN
(SELECT `ID`
FROM `wp_posts`
WHERE `post_type` = 'product'
AND `post_status` = 'publish' );
如果要删除此字段,请使用带有WHERE子句的DELETE语句,它将解决您的目的。
此外,您还必须删除存储在wp_options
表格中的 _transient_timeout_wc_var_prices_{{post_id}}
和_transient_wc_var_prices_{{post_id}}
下的WooCommerce产品价格缓存{1}}
option_name
特别感谢@LoicTheAztec指出产品价格缓存
以上查询已经过测试并为我工作。
在运行此查询之前,请先进行数据库备份
希望这有帮助!
答案 1 :(得分:2)
这是一个自定义功能,将销售价格复制到正常价格,并将重置销售价格。在WooCommerce中有价格:
Traceback (most recent call last):
File "C:\Program Files\Python36\lib\site-packages\pandas\core\indexes\base.py", line 2442, in get_loc
return self._engine.get_loc(key)
File "pandas\_libs\index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5280)
File "pandas\_libs\index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5126)
File "pandas\_libs\hashtable_class_helper.pxi", line 1210, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20523)
File "pandas\_libs\hashtable_class_helper.pxi", line 1218, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20477)
KeyError: 'C'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Development\workspace\TestPython\TestPython.py", line 6, in <module>
df['C'] = np.where(df['A'] > df['C'].shift(), df['B'].shift(), df['A'])
File "C:\Program Files\Python36\lib\site-packages\pandas\core\frame.py", line 1964, in __getitem__
return self._getitem_column(key)
File "C:\Program Files\Python36\lib\site-packages\pandas\core\frame.py", line 1971, in _getitem_column
return self._get_item_cache(key)
File "C:\Program Files\Python36\lib\site-packages\pandas\core\generic.py", line 1645, in _get_item_cache
values = self._data.get(item)
File "C:\Program Files\Python36\lib\site-packages\pandas\core\internals.py", line 3590, in get
loc = self.items.get_loc(item)
File "C:\Program Files\Python36\lib\site-packages\pandas\core\indexes\base.py", line 2444, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas\_libs\index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5280)
File "pandas\_libs\index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5126)
File "pandas\_libs\hashtable_class_helper.pxi", line 1210, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20523)
File "pandas\_libs\hashtable_class_helper.pxi", line 1218, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20477)
KeyError: 'C'
'_price'
'_regular_price'
因此您需要更新常规价格并重置销售价格的所有相关数据。您还需要刷新价格 此功能的用户界面将显示在商店页面或单个产品页面仅适用于管理员和商店经理。
如果您有超过1000种产品,该流程将分多步自动拆分(1000种产品),以避免产品过多时出错。
SQL查询只会选择具有销售价格而非所有产品的产品ID。要更新/重置价格,我使用update_post_meta()函数而不是查询来刷新每个产品缓存......
看起来像:
重要提示:在开始此类流程之前始终进行数据库备份
这是钩住的功能代码:
'_sale_price'
代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。
此代码经过测试并有效。