WooCommerce将简单产品更改为变量,反之亦然,但如何在前端显示新的变化


WooCommerce Changing Simple Product to Variable and Vice Versa, but how to get new variations to show on the frontend?

我正在创建一个插件,连接到Runit系统显示手头的产品在WooCommerce。这需要通过Cron作业来更新产品,从简单产品到可变产品,反之亦然,这取决于库存数量和这些产品的变化。我有工作代码,在后端更新这些产品,显示正确的变化,但是,它不会在前端更新。新的变化,即使在编辑可变产品时,在查看实际产品本身时也不会显示在前端。我还能看到以前的变化。如何更新变体以自动使用新的变体,而不是在编辑产品时在后端管理中点击Update按钮?

是否有某种功能需要调用更新的变化在WooCommerce,我不知道?我使用这样的数组,并在更新wp_postmeta表中meta_key _product_attributes的meta_value之前对其进行序列化,如下所示:

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_key = _product_attributesmeta_valuewp_postmeta表中。每个变体也有一个meta_key为attribute_sizeattribute_extension,其中meta_value等于该帖子变体ID在wp_postmeta表中所需的特定变体的值。

我不知所措,试图更新变量产品的变化,或者当将简单产品转换为变量产品时,需要更新。它在产品的后端管理面板中显示得很好,但是当进入实际的产品页面时,它仍然显示旧产品的变化。显示新版本的唯一方法是进入产品的后端管理面板并点击Update按钮,这将显示所有的新版本。但是如何通过编程来实现呢?我以为这是可能的?一定是我忽略了什么?也许有些地方的术语需要更新?

我尝试过的函数,无法用新的变化更新产品,s表示小=变化id 181342, l表示大=变化id 181343,来自wp_posts表的ID列或wp_postmeta表的post_id列,用于该变化:

wp_set_object_terms(181342, 's', 'size');
wp_set_object_terms(181343, 'l', 'size');

do_action( 'woocommerce_create_product_variation', 181342 );
do_action( 'woocommerce_create_product_variation', 181343 );

这些都没有将产品更新为显示在前端的变体。它仍然只显示了前端的旧版本。如何获得尺寸sl显示在前端?

明白了…看起来你必须使用set_transient来更新wp_options表…这就是我所做的,它把它扼杀在萌芽中:

$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_id =实际产品ID值。

$allVariationIDs = wp_posts表中所有变量ID值的数组。$total_stock是该产品的新库存,计算所有变化的库存水平。

希望这有助于其他人在为您的产品和变体设置所有wp_postswp_postmeta表行后,在前端更新他们的变体。

除了更新产品属性,您还必须更新数据库中的产品变体。

例如,在删除产品变量时,必须将其从wp_posts表

中删除。

你可以参考这个链接了解更多细节Woocommerce -无法删除产品变体