我正在 Woocommerce 中制作一个插件,用于导入 json 文件并在产品页面上创建产品变体。除了为单个产品变体定义单个属性值之外,一切都有效。代码可以定义变量产品的属性及其所有属性值,但代码不能定义单个产品变体的特定属性值。代码 $variation->set_attributes($variation_attributes);似乎没有效果。相反,每个产品变体都具有由变量产品定义的所有属性值。为产品变体定义特定属性值的正确语法是什么?
<?php
/**
* Plugin Name: WooCommerce JSON Product Importer
* Description: Imports WooCommerce products with variations from a JSON file.
* Version: 1.0.0
* Author: Joe
*/
if (!defined('ABSPATH')) {
exit;
}
add_action('admin_menu', 'wcppi_register_menu');
function wcppi_register_menu() {
add_menu_page(
'Product JSON Importer',
'Product Importer',
'manage_options',
'wcppi-importer',
'wcppi_importer_page',
'dashicons-upload',
20
);
}
function wcppi_importer_page() {
?>
<div class="wrap">
<h1>WooCommerce Product Importer</h1>
<form method="post" enctype="multipart/form-data">
<input type="file" name="product_json" required><br><br>
<input type="submit" name="import_submit" value="Import Products">
</form>
</div>
<?php
if (isset($_POST['import_submit'])) {
wcppi_handle_import();
}
}
function wcppi_handle_import() {
if (isset($_FILES['product_json'])) {
$json_file = $_FILES['product_json']['tmp_name'];
$json_data = file_get_contents($json_file);
$products = json_decode($json_data, true);
if (!$products) {
echo '<p>Error: Invalid JSON format.</p>';
return;
}
$array = [
'pa_color' => array('Blue', 'Red', 'Green'),
'pa_size' => array('S', 'M','L'),
];
foreach($array as $attribute_slug => $terms){
if (!taxonomy_exists($attribute_slug)) {
$attribute_id[$attribute_slug] = wc_create_attribute(array(
'name' => ucfirst(str_replace('pa_', '', $attribute_slug)),
'slug' => $attribute_slug, // Attribute slug
'type' => 'select', // Type (dropdown)
'order_by' => 'menu_order', // Order by menu order
'has_archives' => false,
));
// Register the taxonomy for the attribute
register_taxonomy($attribute_slug, 'product', array(
'label' => ucfirst(str_replace('pa_', '', $attribute_slug)),
'public' => true,
'hierarchical' => false,
'show_ui' => false,
'query_var' => true,
'rewrite' => false,
));
}
}
foreach ($array as $attribute_slug => $terms) {
foreach ($terms as $term) {
if (!term_exists($term, $attribute_slug)) {
wp_insert_term($term, $attribute_slug);
}
}
}
foreach ($products as $product_data) {
wcppi_import_product($product_data);
}
echo '<p>Products imported successfully!</p>';
}
}
function wcppi_import_product($data) {
$product = new WC_Product_Variable();
$product->set_name($data['name']);
$product->set_description($data['description']);
$product->set_sku($data['sku']);
$product->set_status('publish');
$product->save();
$product_id = $product->get_id();
$attributes = array(); // Initializing
$position = $attr_id = 0; // Initializing
foreach ($data['attributes'] as $taxonomy => $terms ) {
$attr_id++;
$attribute = new WC_Product_Attribute();
$attribute->set_id($attr_id);
$attribute->set_name("pa_" . lcfirst($taxonomy));
$attribute->set_position($position);
$attribute->set_visible(1);
$attribute->set_variation(1);
$attributes["pa_" . lcfirst($taxonomy)] = $attribute;
$position++;
}
$product->set_attributes($attributes);
$product_id = $product->save(); // Save product that returns the product ID
foreach ($data['attributes'] as $taxonomy => $terms ) {
wp_set_object_terms( $product_id, $terms, "pa_" . lcfirst($taxonomy), true );
}
foreach ($data['variations'] as $variation_data) {
wcppi_import_variation($product_id, $variation_data);
}
}
function wcppi_import_variation($product_id, $data) {
$variation = new WC_Product_Variation();
$variation->set_parent_id($product_id);
$variation->set_regular_price($data['price']);
$variation->set_sku($data['sku']);
$variation_attributes = [];
foreach ($data['attributes'] as $attribute_name => $attribute_value) {
$taxonomy = 'pa_' . lcfirst($attribute_name);
$variation_attributes[$taxonomy] = $attribute_value;
}
$variation->set_attributes($variation_attributes);
$product_id = $variation->save();
foreach ($data['attributes'] as $taxonomy => $terms ) {
wp_set_object_terms( $product_id, $terms, "pa_" . lcfirst($taxonomy), true );
}
}
这是我导入的json文件:
[
{
"name": "T-shirt",
"sku": "TSHIRT001",
"description": "A stylish t-shirt",
"attributes": {
"Color": ["Red", "Blue", "Green"],
"Size": ["S", "M", "L"]
},
"variations": [
{
"sku": "TSHIRT001-RED-S",
"price": "19.99",
"attributes": {
"Color": "Red",
"Size": "S"
}
},
{
"sku": "TSHIRT001-RED-M",
"price": "19.99",
"attributes": {
"Color": "Red",
"Size": "M"
}
},
{
"sku": "TSHIRT001-BLUE-S",
"price": "19.99",
"attributes": {
"Color": "Blue",
"Size": "S"
}
}
]
},
{
"name": "Hoodie",
"sku": "HOODIE001",
"description": "A warm hoodie",
"attributes": {
"Color": ["Black", "White"],
"Size": ["M", "L", "XL"]
},
"variations": [
{
"sku": "HOODIE001-BLACK-M",
"price": "29.99",
"attributes": {
"Color": "Black",
"Size": "M"
}
},
{
"sku": "HOODIE001-BLACK-L",
"price": "29.99",
"attributes": {
"Color": "Black",
"Size": "L"
}
},
{
"sku": "HOODIE001-WHITE-XL",
"price": "29.99",
"attributes": {
"Color": "White",
"Size": "XL"
}
}
]
}
]
`
我找到了解决办法。对于变异属性分类法与 slug 配对(蓝色的 slug 是蓝色)
$variation_attributes = [];
foreach ($data['attributes'] as $attribute_name => $attribute_value) {
$taxonomy = 'pa_' . lcfirst($attribute_name);
$variation_attributes[$taxonomy] = lcfirst($attribute_value);
}