Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

variations1 And this are product variations variations2

here 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 codebin

So 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.

<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 wpsc class

share|improve this question
    
I don't know what the question is. –  s_ha_dum Apr 14 '13 at 14:38
    
If you understand what I want to make, I just need a way how to implement it, I'm not asking for code, just some flowchart in words how to achieve this kind of form. how to this loop might look like. I'm not programmer, I just know how to code ) –  CBeTJlu4ok Apr 14 '13 at 14:41
    
I don't know what you want to make. –  s_ha_dum Apr 14 '13 at 14:49
    
The same thing I have on screens, first one (with checkboxes) is a form and second is generated from this form. this one is on a dashboard. Now I'm moving "add product" to frontend. With a little modifications my children checkboxies array looks: 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" } } First dimension are parents and seconds values are children I want to generate. Now I want to make loop to create posts like on second screen –  CBeTJlu4ok Apr 14 '13 at 15:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.