Bigcommerce Product SKU's -> Options

742 views Asked by At

Having a little trouble with the Bigcommerce API when trying to access Product SKU's 'Options' array_object.

I can access everything else within the SKU object, just not the Options - doing a print_r on the $sku->options doesn't show any returned data and var_dump shows '(bool)false'. Here is my code:

$filter = array('sku' => '940801DB');
$skus = Bigcommerce::getSkus($filter);

foreach ( $skus as $sku ){
    echo '<pre>';
    print_r( $sku->options );
    echo '</pre>';
}

Any ideas how to access this array/object?

Further info:

If I print_r($sku) I get:

Array
(
    [0] => Bigcommerce\Api\Resources\Sku Object
    (
        [ignoreOnCreate:protected] => Array
            (
                [0] => product_id
            )

        [ignoreOnUpdate:protected] => Array
            (
                [0] => id
                [1] => product_id
            )

        [fields:protected] => stdClass Object
            (
                [id] => 1
                [product_id] => 225
                [sku] => 940801DB
                [cost_price] => 0.0000
                [upc] => 
                [inventory_level] => 0
                [inventory_warning_level] => 0
                [bin_picking_number] => 
                [options] => Array
                    (
                        [0] => stdClass Object
                            (
                                [product_option_id] => 1
                                [option_value_id] => 834
                            )

                        [1] => stdClass Object
                            (
                                [product_option_id] => 2
                                [option_value_id] => 829
                            )

                        [2] => stdClass Object
                            (
                                [product_option_id] => 3
                                [option_value_id] => 827
                            )

                    )

            )

        [id:protected] => 1
        [ignoreIfZero:protected] => Array
            (
            )

        [fieldMap:protected] => Array
            (
            )

    )
)
3

There are 3 answers

0
Yingdong Zhang On BEST ANSWER

This seems like a bug of Bigcommerce API. I installed it using composer, if you take a look at the source code of Bigcommerce API, inside vendor/bigcommerce/api/src/Bigcommerce/Api/Resources/Sku.php:

public function options()
{
    $options = Client::getCollection($this->fields->options->resource, 'SkuOption');

    foreach ($options as $option) {
        $option->product_id = $this->product_id;
    }

    return $options;
}

See that it gets $this->fields->options->resource, but the options array doesn't have resource in it. In products it's like this:

"options": {
    "url": "https://store-et7xe3pz.mybigcommerce.com/api/v2/products/32/options.json",
    "resource": "/products/32/options"
  },

but in sku it's this:

"options": [
      {
        "product_option_id": 15,
        "option_value_id": 18
      },
      {
        "product_option_id": 16,
        "option_value_id": 26
      }
    ]

seems like a bug to me.

0
Alex Channelunity On

options is below fields

Try

print_r($sku->fields->options);
0
Emilian F. On

Not sure why you are not able to access that variable. It seems more like an issue with PHP than Bigcommerce.

Still, a workaround would be to get the options data yourself. Just send a GET request to the following endpoint:

/products/{{product_id}}/options.json