Created
April 18, 2024 05:13
-
-
Save FileSubmit/69b35ee428b8f399cfb94657782b4a1c to your computer and use it in GitHub Desktop.
wpallimport - bulk attributes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action('pmxi_saved_post', 'wp_programator_automatically_import_wpai_params', 10, 2); | |
/** | |
* @param $post_id | |
* @param $xml | |
*/ | |
function wp_programator_automatically_import_wpai_params($post_id, $xml) | |
{ | |
$product_attributes = array(); | |
$atts = array(); // Initialize the array to avoid undefined variable warnings. | |
// The $xml is a SimpleXML object | |
// Scrub through the PARAM attributes and add these to product | |
foreach ($xml->params->item_3 as $param) { | |
// Get attribute name | |
$name = $param->name->__toString(); | |
// Get attribute value | |
$value = $param->value->__toString(); | |
if ($value === null || $value === '') { | |
continue; // Skip this iteration if $value is null or empty | |
} | |
// The WC attribute taxonomy is prefixed with pa_, build the slug here | |
$slug = 'pa_' . wc_sanitize_taxonomy_name($name); | |
//$slug = 'pa_' . substr(wc_sanitize_taxonomy_name(cyrillic_to_latin($name)), 0, 28); | |
// Create the attribute with the original name | |
wc_create_attribute(['name' => $name]); | |
// Here, you should use the sanitized name for the taxonomy but the original name for setting the term. | |
wp_set_object_terms( $post_id, $value, $slug, true ); | |
// Add to the attributes data | |
// This is needed for attributes to display in the WP admin | |
$atts[$slug] = [ | |
'name' => $slug, | |
'value' => $value, | |
'is_visible' => '1', | |
'is_variation' => '0', | |
'is_taxonomy' => '1' | |
]; | |
} | |
// Update the _product_attributes meta - this is where the attribute data are stored for products | |
$product = wc_get_product($post_id); | |
if ($product) { | |
$product->update_meta_data('_product_attributes', $atts); | |
$product->save(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment