wc_get_product($id) gave uncorrect results
- products have been imported with WP All Import Pro – variable products imported correctly (all attributes at place and have correct values)
- pmxi_saved_post hook of the WP All Import Pro is used to post-process attributes after a single post-import. Takes one argument product_id of the current imported product
code used to get parent product attributes
function one_attribute_value($id) {
/* get created/updated product object */
$current_imported_product = wc_get_product($id);
/* If product is ‘variable’ – do nothing */
if ($current_imported_product->get_type()==’variable’) return false;
if ($current_imported_product->get_type()==’variation’) {
/* get parent product object */
$product_variation_parent = wc_get_product($current_imported_product->get_parent_id());
/* if only one child - do nothing */
if (sizeof($product_variation_parent->get_children())==1) return false;
/* if more than one child */
if (sizeof($product_variation_parent->get_children())>1) {
echo 'Parent ID: '.$product_variation_parent->get_id().'<br>'; //Echoes correct product ID
//get parent product attributes
$product_variation_parent_attributes = $product_variation_parent->get_attributes();
pr2($product_variation_parent_attributes); //print_r - Some attributes ['option'] values are missing
foreach ( $product_variation_parent_attributes as $key => $attribute ):
//TODO - process attributes - if count of the sizeofattribute['option'] array is one then set attribute['variation'] from 1 to 0
endforeach;
}
}
}
add_action(‘pmxi_saved_post’, ‘one_attribute_value’, 10, 1);
Then I ran wc_get_product(hardcoded parent_product_id)
on the single product page of that same product the attributes array is correct.
Results are here :
results of the example
How is it possible to get correct product object the running pmxi_saved_post
action
Leave an answer