jasonknight / woocommerce-json-api

A simple, Abstract JSON API for Wordpress' Awesome Plugin: WooCommerce
58 stars 21 forks source link

Var not set #57

Open necrofede opened 10 years ago

necrofede commented 10 years ago

Hi!! Great work with the api, Thanks so much!! I think I have found some kind of bug in debugging mode, json response gets an undefined var error on Base.php line 435: Helpers::debug("Base::apply_disconnector {$desc['name']} called from $called_from");

Apparently $desc['name'] is not defined:

Json returns: "json: Notice: Undefined index: name in /var/www/wordpress/wp-content/plugins/woocommerce-json-api-master/classes/WCAPI/Base.php on line 435 {"action":"woocommerce_json_api","proc":"set_products","arguments":{"tok....."

In my code I want to disconnect several categories from a product, if I comment the line 435 it works fine.

My code is:

<?php error_reporting(E_ALL);

require_once "../wp-content/plugins/woocommerce-json-api-master/tests/functions.php"; include "../wp-content/plugins/woocommerce-json-api-master/tests/config.php";

$skuDelProducto = "A498C3"; $categorias_a_eliminar = array(array("name" => "SomeCategory", "categoryPos" => -1), array("name" => "AnotherCategory", "categoryPos" => -1));

// STEP 1: Get available categories.

$data = array( 'action' => 'woocommerce_json_api', 'proc' => 'get_categories', 'arguments' => array( 'token' => $token, ) );

$json = curl_post($url,$data); $result = json_decode($json,true); $categories = $result['payload'];

for ($j=0; $j<sizeof($categorias_a_eliminar); $j++){ for ($i=0; $i<sizeof($categories); $i++){ if(strtolower($categories[$i]['name']) == strtolower($categorias_a_eliminar[$j]['name'])){ $categorias_a_eliminar[$j]['categoryPos'] = $i; } }

if($categorias_a_eliminar[$j]['categoryPos'] == -1){
    echo "Category " . $categorias_a_eliminar[$j]['name'] . " not found";
}

}

// STEP 2: Get the product

function get_products($args){ $data = array( 'action' => 'woocommerce_json_api', 'proc' => 'get_products', 'arguments' => array( 'token' => $args['token'], 'skus' => array( $args['sku'], 'DOESNTEXIST' ), 'include' => array( 'variations' => false, 'images' => false, 'featured_image' => false, 'reviews' => false, ), ) );

$json = curl_post($args['url'],$data);
$result = json_decode($json,true);
$products = $result['payload'];

if(sizeof($products) == 0){
    echo "Product not found";
    die;
}
elseif(sizeof($products) > 1){
    echo "Many products founded";
    die;
}   
return $products;

}

/////////////////////////////////////////////////////// // Disconnecting Several Categories from a Product // //////////////////////////////////////////////////////

$args = array('token' => $token, 'sku' => $skuDelProducto, 'url' => $url);

foreach($categorias_a_eliminar as $categoria_a_eliminar){ $products = get_products($args);

for($i=0; $i<sizeof($products[0]['categories']); $i++){
    if(strtolower($products[0]['categories'][$i]['name']) == strtolower($categoria_a_eliminar['name'])){

        $products[0]['categories'][$i]['__disconnect__'] = true;
        $result['payload'] = array($products[0]);
        $result['proc'] = 'set_products';

        $json = curl_post($url,$result);
        $result = json_decode($json,true);
        $products = $result['payload'];
        break;
    }
}

}

jasonknight commented 10 years ago

I'll have to look into it. The main problem is that the API functionality that we use doesn't include this use case.

jasonknight commented 9 years ago

I am still aware of this issue. If you find a fix, let me know.