0

This is the code:

function create_order_with_custom_products()
{
    $orderGenerator = new OrderGenerator();
    $orderGenerator->setCustomer(6907);

    $orderGenerator->createOrder(array(
        // Add configurable product
        array(
            'product' => 30151,
            'qty' => 1
        ), array(
            'product' => 30150,
            'qty' => 2
        ),
    ));
}

I have to create array with such structure:

        array(
            'product' => 30151,
            'qty' => 1
        ), array(
            'product' => 30150,
            'qty' => 2
        ),

I am trying to create an array with same structure like that:

foreach($ItemsInCart['productid'] as $key=>$value){
    $ProductId = $value;
    $ProductQty = $ItemsInCart["productqty"][$key];
    $product_id = $ProductId; // Replace id with your product id
    $qty = $ProductQty; // Replace qty with your qty

    $ItemsId[] = ['product' => $ProductId, 'qty' => $ProductQty];
}

This gives me result of:

Array ( [0] => Array ( [product] => 30143 [qty] => 1 ) [1] => Array ( [product] => 30144 [qty] => 2 ) [2] => Array ( [product] => 30145 [qty] => 3 ) [3] => Array ( [product] => 30146 [qty] => 4 ) [4] => Array ( [product] => 30147 [qty] => 5 ) )

All i want to know is why this :

function create_order_with_custom_products()
{
    $orderGenerator = new OrderGenerator();
    $orderGenerator->setCustomer(6907);

    $orderGenerator->createOrder(array(
        // Add configurable product
        array(
            'product' => 30151,
            'qty' => 1
        ), array(
            'product' => 30150,
            'qty' => 2
        ),
    ));
}

Is not the same as this:

function create_order_with_custom_products()
{
    $orderGenerator = new OrderGenerator();
    $orderGenerator->setCustomer(6907);

    $orderGenerator->createOrder(array($ItemsId));
}

Why the second approach is not working, where is my mistake?

2
  • Replace $ItemsId[]["product"] = $ProductId; $ItemsId[]["qty"] = $ProductQty; with $ItemsId[] = ['product' => $ProductId, 'qty' => $ProductQty]; Commented Aug 4, 2016 at 12:23
  • I have udated my question can you please check it out. Commented Aug 4, 2016 at 12:41

2 Answers 2

2

The easiest way is

$ItemsId = []; //declaration
$ItemsId [] = ["product"=>30151,"qty"=>1]; //pusing an array with key 'product' & 'qty' to $ItemsId

UPDATE

This line

$orderGenerator->createOrder(array($ItemsId));

is not working because $ItemsId is already an array and you are putting this inside a new array with this array($ItemsId). This will look like

Array([0] => 
     Array ([0] =>  
            Array(
                 [0] =>Array  ( [product] => 30143,[qty] => 1 ) 
                 [1] => Array ( [product] => 30144,[qty] => 2 ) 
                 [2] => Array ( [product] => 30145,[qty] => 3 ) 
                 [3] => Array ( [product] => 30146,[qty] => 4 ) 
                 [3] => Array ( [product] => 30147,[qty] => 5 ) 
            )
      )
)

But the expected array should look like

      Array ([0] =>  
             Array(
                   [0] =>Array  ( [product] => 30143,[qty] => 1 ) 
                   [1] => Array ( [product] => 30144,[qty] => 2 ) 
                   [2] => Array ( [product] => 30145,[qty] => 3 ) 
                   [3] => Array ( [product] => 30146,[qty] => 4 ) 
                   [3] => Array ( [product] => 30147,[qty] => 5 ) 
             )
       )

To overcome this issue just change

$orderGenerator->createOrder(array($ItemsId)); 

To

$orderGenerator->createOrder($ItemsId);
Sign up to request clarification or add additional context in comments.

8 Comments

I have updated my question, can you please check it out?
I believe you are talking about this line $orderGenerator->createOrder(array($ItemsId));
how does OrderGenerator() looks like?
Here it is: pastebin.com/0iUbZpHU thanks so much for the help man!
Can you please help me out ?
|
0

Use a counter that assures that both sub elements of the array share the same index. Like this:

$i = 0;

foreach(...) {
...
$ItemsId[$i]["product"] = $ProductId;             
$ItemsId[$i]["qty"] = $ProductQty;
...
$i++;
}

2 Comments

I have updated my question, can you please check it out?
Do you set $ItemsId in the method? In your example it is not set

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.