0

Everything works fine if i use this structure:

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
        ),
    ));
}

If i create an array and use it like that:

$ItemsId = [];
if(!empty($ItemsInCart) && count($ItemsInCart)>0)
{
    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"=>$qty];
    }
}




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

    $orderGenerator->createOrder($ItemsId);
}

If i print_r($ItemsId); i receive the following output:

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 ) )

It is no longer working. And i do not see the reason.

The code of createOrder function you can see here: http://pastebin.com/0iUbZpHU

Can you please tell me where is my mistake and why my code is not working.

Venelin
  • 2,905
  • 7
  • 53
  • 117
  • 1
    You need to get in the habit of [accepting answers](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) which help you to solve your issues. You'll earn points and *others will be encouraged to help you.* – Jay Blanchard Aug 04 '16 at 13:30
  • Aside: `!empty($ItemsInCart) && count($ItemsInCart)>0` is redundant. Ideally that should just be `$ItemsInCart` (i.e. `$ItemsInCart == true`). If you don't know whether the variable exists (→ why?!), you may do `!empty($ItemsInCart)`. Both cases already cover the `count() > 0` case. – deceze Aug 04 '16 at 13:33

1 Answers1

2

Read up on variable scope. The $ItemsId variable is not available inside the create_order_with_custom_products() function. You'll need to pass it as a parameter:

function create_order_with_custom_products($ItemsId)
{
    // ...
}

And then call it with the variable you created:

create_order_with_custom_products($itemsId);
Alex Howansky
  • 50,515
  • 8
  • 78
  • 98
  • Do i have to change anything else inside the `create_order_with_custom_products` function itself? – Venelin Aug 04 '16 at 13:32