Moving WP-Ecommerce dashboard to front end
-
Or in other words, nested loop/recursive function:
I have WP E-Commerce installed and I’m moving whole it’s dashboard to frontend.
I have everything ready and am on the last part. In fact I thought this part was ready, until I added two variations, where I understood that it works a little bit different.Let me show you what I’m talking about.
IMG1
IMG2here every variation from second category is assigned to first
like (1,a;1,b;1,c);(2,a;2,b;2,c);(3,a;3,b;3,c);Here is my code
https://pastebin.com/nH9wk9JPSo how to remake my for loop with some nested for loop ( I guess ) to work like combinations. At this moment it just creates separate variations without nesting them.
$parent = $_POST['variationParent']; $children = $_POST['variationChild']; if(!empty($parent)) { $affparent = wp_set_post_terms( $post_id, $parent, 'wpsc-variation', true ); $affchildren = wp_set_post_terms( $post_id, $children, 'wpsc-variation', true ); $N = count($children); for($i=0; $i < $N; $i++) { $cat_id = (int) $children[$i]; if(!in_array($cat_id,$varArray)){ $cat = get_term( $cat_id, 'wpsc-variation' ); $new_entr = array(); $new_entr['post_title'] = esc_attr(strip_tags($_POST['postTitle'])) . " (" . $cat->name . ")"; $new_entr['post_content'] = esc_attr(strip_tags($_POST['postContent'])); $new_entr['post_status'] = 'inherit'; $new_entr['post_category'] = $cat->term_id; $new_entr['post_type'] = 'wpsc-product'; $new_entr['post_parent'] = $post_id; $new_entr['post_author'] = $user_id; $var_id = wp_insert_post($new_entr); wp_set_post_terms( $var_id, $cat->term_id, 'wpsc-variation' ); update_post_meta($var_id, '_wpsc_price', $price); update_post_meta($var_id, '_wpsc_special_price', $special_price); update_post_meta($var_id, '_wpsc_sku', $sku); update_post_meta($var_id, '_wpsc_stock', $stock); } } }
I think at first I need to remake my FORM to chain parents with their parent.
Here I changed Childre[] to take also parents ID
<fieldset> <ul class="formVariations" id="formVariationsID"><li> <?php $parentArray = array(); foreach ($parentVariations as $variation) { $parentArray[] = $variation->term_id; } foreach($variationCategories as $category) { //var_dump($category); //echo ; // check if this variation is already checked if (in_array($category->term_id, $parentArray)) { if ($category->parent == 0) { echo "</li><li class='formVariationParent'><input type='checkbox' checked name='variationParent[]' autocomplete='off' value='". $category->term_id ."'>" . $category->name . ""; } else { echo "<ul><li class='formVariationChild'><input type='checkbox' checked name='variationChild[". $category->parent ."][]' autocomplete='off' value='". $category->term_id ."'>" . $category->name . "</li></ul>"; } } else { if ($category->parent == 0) { echo "</li><li class='formVariationParent'><input type='checkbox' name='variationParent[]' autocomplete='off' value='". $category->term_id ."'>" . $category->name . ""; } else { echo "<ul><li class='formVariationChild'><input type='checkbox' name='variationChild[". $category->parent ."][]' autocomplete='off' value='". $category->term_id ."'>" . $category->name . "</li></ul>"; } } } ?> </ul> </fieldset>
That’s the output of this form. first value in array is parent termID and second is ChildID.
array(2) { [3]=> array(3) { [0]=> string(1) "4" [1]=> string(1) "5" [2]=> string(1) "6" } [7]=> array(3) { [0]=> string(1) "8" [1]=> string(1) "9" [2]=> string(2) "10" } }
So my code should assign all values from parent[7] to parent[3]. meaning in the end of the loop, there will be 9 variations.
Question is, how to solve this nested loop. count parents first and and nest that many loops (or foreach) ?
(also might be interesting how wpsc deals with that, which I don’t understand at all https://pastebin.com/LTdh0EM4 )
- The topic ‘Moving WP-Ecommerce dashboard to front end’ is closed to new replies.