以编程方式添加的变体在单击更新按钮后才会显示

时间:2018-12-20 作者:Mike

这开始让我头痛,所以希望这里的人能帮助我,我们可以一起帮助其他有同样问题的人!

我构建了一个插件,可以自动为WooCommerce生成产品变体。这一切似乎都很好,除了一件事。新的变体在前端不可见,当我查看产品编辑页面时,它们如下所示:

Product variations wrong

当我点击更新按钮并保存产品时,它得到了神奇的修复。在前端和后端都可以看到变化,现在看起来是这样的:

Product variations correct

The code

// Get the Variable product object (parent)
$product = wc_get_product($parent_id);

$variation_post = array(
    \'post_title\'  => $product->get_title(),
    \'post_name\'   => \'product-\'.$parent_id.\'-variation\',
    \'post_status\' => ($enable_variations == "yes") ? \'publish\' : \'private\',
    \'post_parent\' => $parent_id,
    \'post_type\'   => \'product_variation\',
    \'guid\'        => $product->get_permalink()
);

// Creating the product variation
$variation_id = wp_insert_post( $variation_post, true );

$fields = array(
    \'_manage_stock\'                 => \'yes\',
    \'_backorders\'                   => $backorders,
    \'_stock_status\'                 => $stock_status,
    \'_stock\'                        => $stock,
    \'_regular_price\'                => $price,
    \'_price\'                        => $price,
);

foreach ( $fields as $meta_key => $meta_value ) {
    update_post_meta($variation_id, $meta_key, $meta_value);
}

// Add attribute 1 to variation
wp_set_post_terms( $variation_id, sanitize_title($attribute_value), "pa_attribute_1", true );
update_post_meta( $variation_id, \'attribute_pa_attribute_1\', sanitize_title($attribute_value));

// Add attribute 2 to variation
wp_set_post_terms( $variation_id, sanitize_title($attribute_value), "pa_attribute_2", true );
update_post_meta( $variation_id, \'attribute_pa_attribute_2\', sanitize_title($attribute_value));

// Add attribute 3 to variation
wp_set_post_terms( $variation_id, sanitize_title($attribute_value), "pa_attribute_3", true );
update_post_meta( $variation_id, \'attribute_pa_attribute_3\', sanitize_title($attribute_value));

wc_delete_product_transients($variation_id);
我错过了什么?在产品上单击“更新”时会发生什么情况?已经尝试使用$product->save()和删除过渡,就像web上大多数主题中回答的那样。

1 个回复
最合适的回答,由SO网友:Mike 整理而成

我发现这是因为WooCommerce中还没有这些属性。首先,您必须添加所有属性及其值,然后生成并添加变体。那么它应该会起作用:

// Create an array of the attributes we want to add
$attributes = array(
    "attribute_1" => array(
        "slug"  => "attribute_1",
        "label" => "Name of attribute 1",
    ),
    "attribute_2" => array(
        "slug"  => "attribute_2",
        "label" => "Name of attribute 2",
    ),
    "attribute_3" => array(
        "slug"  => "attribute_3",
        "label" => "Name of attribute 3",
    ),
);

// Add the attributes to WooCommerce
foreach($attributes as $attribute){

    if(!get_taxonomy("pa_" . $attribute["slug"])) {
        $result = wc_create_attribute(array(
            "name" => $attribute["label"],
            "slug" => $attribute["slug"], 
            "type" => "select",
        ));
    }
    
}

// Create an array with all the terms
$attributes_values["attribute_1"] = array("value 1", "value 2", "value 3");
$attributes_values["attribute_2"] = array("value 1", "value 2", "value 3");
$attributes_values["attribute_3"] = array("value 1", "value 2", "value 3");

// Iterate over our attributes map,containing attributes that we want to sync
foreach($attributes_values as $attribute => $values ){
    // Iterate over possible values for the attribute and insert them. 
    foreach($values as $value) {
        if(!term_exists($value, wc_attribute_taxonomy_name($attribute))){
            wp_insert_term($value, wc_attribute_taxonomy_name($attribute), array(\'slug\' => sanitize_title($term)));
        }
    }
}
Update march 2021上述内容在今年早些时候WooCommerce更新后停止工作,并发现通过WooCommerce类管理变量实际上是可能的,而且安全得多:

// Get the Variable product object (parent)
$product = wc_get_product($variableProductID);
   
$variation_post = array(
    \'post_title\'  => $product->get_title(),
    \'post_name\'   => \'product-\'.$variableProductID.\'-variation\',
    \'post_status\' => \'publish\',
    \'post_parent\' => $variableProductID,
    \'post_type\'   => \'product_variation\',
    \'guid\'        => $product->get_permalink()
);

// Creating the product variation
$variation_id = wp_insert_post($variation_post, true);

if (is_wp_error($variation_id)) {
    // Error handling
}

// Get an instance of the WC_Product_Variation object
$variation = new \\WC_Product_Variation($variation_id);

// Add attributes to variation
update_post_meta($variation_id, \'attribute_pa_device_brand\', sanitize_title($action["arguments"]["device_brand"]));
update_post_meta($variation_id, \'attribute_pa_device_model\', sanitize_title($action["arguments"]["device_model"]));
update_post_meta($variation_id, \'attribute_pa_case_type_colour\', sanitize_title($action["arguments"]["case_type_colour"]));

$variation->set_price($action[\'arguments\'][\'price\']);
$variation->set_regular_price($action[\'arguments\'][\'price\']);
$variation->set_stock_quantity($action[\'arguments\'][\'stock\']);
$variation->set_manage_stock(true);
$variation->set_stock_status();
$variation->set_backorders($action[\'arguments\'][\'backorders\']);

$variation->save();

相关推荐