Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php WooCommerce将简单产品更改为可变产品,反之亦然,但如何在前端显示新的变化?_Php_Wordpress_Woocommerce - Fatal编程技术网

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

Php WooCommerce将简单产品更改为可变产品,反之亦然,但如何在前端显示新的变化?,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我正在创建一个插件,该插件连接到Runit系统,用于在WooCommerce中显示现有产品。这需要通过Cron作业更改产品,该作业将更新产品,从简单产品更改为可变产品,反之亦然,具体取决于库存量和这些产品的变化(这些变化会发生变化)。我有一个工作代码,它在后端更新这些产品,显示正确的变体,但是,它在前端不更新。即使在编辑可变产品时,新的变体在查看实际产品时也不会显示在前端。我仍然在那里看到旧的变化。如何更新变体以自动使用新变体,而不必在编辑产品时点击后端管理中的update按钮 是否需要调用某种

我正在创建一个插件,该插件连接到Runit系统,用于在WooCommerce中显示现有产品。这需要通过Cron作业更改产品,该作业将更新产品,从简单产品更改为可变产品,反之亦然,具体取决于库存量和这些产品的变化(这些变化会发生变化)。我有一个工作代码,它在后端更新这些产品,显示正确的变体,但是,它在前端不更新。即使在编辑可变产品时,新的变体在查看实际产品时也不会显示在前端。我仍然在那里看到旧的变化。如何更新变体以自动使用新变体,而不必在编辑产品时点击后端管理中的
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代表小=变体id
181342
和l代表大=变体id
181343
来自
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]);
}