Php WooCommerce将简单产品更改为可变产品,反之亦然,但如何在前端显示新的变化?
我正在创建一个插件,该插件连接到Runit系统,用于在WooCommerce中显示现有产品。这需要通过Cron作业更改产品,该作业将更新产品,从简单产品更改为可变产品,反之亦然,具体取决于库存量和这些产品的变化(这些变化会发生变化)。我有一个工作代码,它在后端更新这些产品,显示正确的变体,但是,它在前端不更新。即使在编辑可变产品时,新的变体在查看实际产品时也不会显示在前端。我仍然在那里看到旧的变化。如何更新变体以自动使用新变体,而不必在编辑产品时点击后端管理中的Php WooCommerce将简单产品更改为可变产品,反之亦然,但如何在前端显示新的变化?,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我正在创建一个插件,该插件连接到Runit系统,用于在WooCommerce中显示现有产品。这需要通过Cron作业更改产品,该作业将更新产品,从简单产品更改为可变产品,反之亦然,具体取决于库存量和这些产品的变化(这些变化会发生变化)。我有一个工作代码,它在后端更新这些产品,显示正确的变体,但是,它在前端不更新。即使在编辑可变产品时,新的变体在查看实际产品时也不会显示在前端。我仍然在那里看到旧的变化。如何更新变体以自动使用新变体,而不必在编辑产品时点击后端管理中的update按钮 是否需要调用某种
update
按钮
是否需要调用某种函数来更新WooCommerce中我不知道的变体?我像这样使用数组,并在更新wp_postETA表中meta_key\u product_attributes
的meta_值之前对其进行序列化,如下所示:
function ProcessBasicProperties(Array $properties)
{
$return = array();
$position = 0;
if (!empty($properties))
{
if (!empty($properties['siz']))
{
++$position;
$size = !is_array($properties['siz']) ? array($properties['siz']) : array_unique($properties['siz']);
$return['size'] = array(
'name' => 'Size',
'value' => count($size) > 1 ? implode(' | ', $size) : $size[0],
'is_visible' => count($size) > 1 ? 0 : 1,
'is_variation' => count($size) > 1 ? 1 : 0,
'is_taxonomy' => 0,
'position' => $position
);
}
if (!empty($properties['ext']))
{
++$position;
$extension = !is_array($properties['ext']) ? array($properties['ext']) : array_unique($properties['ext']);
$return['extension'] = array(
'name' => 'Extension',
'value' => count($extension) > 1 ? implode(' | ', $extension) : $extension[0],
'is_visible' => count($extension) > 1 ? 0 : 1,
'is_variation' => count($extension) > 1 ? 1 : 0,
'is_taxonomy' => 0,
'position' => $position
);
}
// styles do not get added to attributes for variable products, instead, with variable products, the style goes into the overall sku of the product (General Properties)
// So, in short, variable products should not have this key set.
if (!empty($properties['style']))
{
++$position;
$return['style'] = array(
'name' => 'Style',
'value' => htmlspecialchars($properties['style'], ENT_QUOTES),
'is_visible' => 1,
'is_variation' => 0,
'is_taxonomy' => 0,
'position' => $position
);
}
if (!empty($properties['color']))
{
++$position;
$colors = !is_array($properties['color']) ? array($properties['color']) : array_unique($properties['color']);
$return['color'] = array(
'name' => 'Color',
'value' => count($colors) > 1 ? htmlspecialchars(implode(' | ', $colors), ENT_QUOTES) : htmlspecialchars($colors[0], ENT_QUOTES),
'is_visible' => 1,
'is_variation' => 0,
'is_taxonomy' => 0,
'position' => $position
);
}
if (!empty($properties['gender']))
{
++$position;
$return['gender'] = array(
'name' => 'Gender',
'value' => htmlspecialchars($properties['gender'], ENT_QUOTES),
'is_visible' => 1,
'is_variation' => 0,
'is_taxonomy' => 0,
'position' => $position
);
}
if (!empty($properties['season']))
{
++$position;
$return['season'] = array(
'name' => 'Season',
'value' => htmlspecialchars($properties['season'], ENT_QUOTES),
'is_visible' => 1,
'is_variation' => 0,
'is_taxonomy' => 0,
'position' => $position
);
}
}
return $return;
}
这是一个函数,用于返回正确的数组,该数组被序列化并输入到meta\u值的wp\u postemta
表中,其中meta\u键
=\u产品属性
。每个变体还有一个meta_键attribute_size
和meta_键attribute_extension
,其中meta_值
等于该变体ID的wp_postETA
表中要求的特定变体的值
我不知所措,试图更新可变产品的变体,或者在将简单产品转换为可变产品时,需要更新。它在产品的后端管理面板中显示良好,但当转到实际的产品页面时,它仍然显示旧的产品变体。显示新版本的唯一方法是进入产品的后端管理面板,点击Update
按钮,然后显示所有新版本。但如何以编程方式实现这一点呢?我以为这是可能的?一定是我在这里忽略了什么?也许有些术语需要更新
我尝试过的功能,未能使用新的变体更新产品,s代表小=变体id181342
和l代表大=变体id181343
来自id
wp_posts
表的列或wp_postmeta
表的post_id
列,用于该变体:
wp_set_object_terms(181342, 's', 'size');
wp_set_object_terms(181343, 'l', 'size');
及
所有这些都不会将产品更新为前端显示的变体。它仍然只显示前端的旧版本。如何获取前端显示的尺寸
s
和l
?除了更新产品属性外,还必须从数据库中更新产品变体
例如,在删除产品变体时,必须将其从wp_posts表中删除
有关更多详细信息,请参阅此链接
找到了答案。。。看起来您必须使用
set\u transient
更新wp\u选项表。。。这就是我所做的,它把它扼杀在萌芽状态:
$transients = array(
'name' => array(
'wc_product_total_stock_' . $product_id,
'wc_product_children_ids_' . $product_id
),
'values' => array(
$total_stock,
$allVariationIDs
),
'filters' => array(
'woocommerce_stock_amount'
)
);
// Set it up so that the variations show in the front end also...
foreach($transients['name'] as $k => $transient_name)
{
set_transient($transient_name, $transients['values'][$k],YEAR_IN_SECONDS);
if (isset($transients['filters'][$k]))
apply_filters($transients['filters'][$k], $transients['values'][$k]);
}
$product\u id
=实际的产品id值
$allVariationIDs
=在wp\u posts
表中变量的所有ID值的数组<代码>$total_stock
是产品的新库存,在所有变化库存水平中计算
希望这有助于其他人在为您的产品和变体设置所有
wp_posts
和wp_postmeta
表格行后,更新前端的变体。是的,我知道这一点,已从wp_posts
表及其相应的wp_postmeta
表中删除所有产品变体,其中wp_postmeta中的post id
=wp_posts中的post父项
,所有新产品变体都已添加到wp_posts
表以及每个变体的wp_postmeta
表中。在wp_posts
和wp_postmeta
中的所有编辑都是以与WooCommerce插件完全相同的方式执行的,我已经研究了这两个表好几个星期了。是否还有其他表可能需要为产品变化进行编辑??除了wp_posts
和wp_postmeta
??可能是wp\u术语
??我正在将产品设置为wp\u set\u object\u terms
中的一个变量,但这是在产品上。是否必须为每个变量ID
设置某些内容?像wp_set_object_术语($variation_id,'somethine here','somethine other')代码>??
$transients = array(
'name' => array(
'wc_product_total_stock_' . $product_id,
'wc_product_children_ids_' . $product_id
),
'values' => array(
$total_stock,
$allVariationIDs
),
'filters' => array(
'woocommerce_stock_amount'
)
);
// Set it up so that the variations show in the front end also...
foreach($transients['name'] as $k => $transient_name)
{
set_transient($transient_name, $transients['values'][$k],YEAR_IN_SECONDS);
if (isset($transients['filters'][$k]))
apply_filters($transients['filters'][$k], $transients['values'][$k]);
}