Jump to content

Creating Products via API


alexm
 Share

Recommended Posts

Good Afternoon @kongondo

Hope all is good your side and you've enjoyed your weekend.

I'm just pulling a LOAD of products from a stockist and I was wondering (as I believe it is not yet covered in the docs) what is the best way to create products via API. This should then help me to quickly generate products and manage stock levels. 

I've created the parent product with the usual method. e.g.

$product = new Page();
$product->template = "padloper-product";
$product->parent = $pages->get("template=padloper-products");
$product->title = "Test";
$product->padloper_product_stock->price = 22.25;
$product->padloper_type = "Trousers";
$product->padloper_categories = "Suits";
$product->save();

But then I'd like to add variants on the fly too. So I also used:

$product_variant = new Page();
$product_variant->template = "padloper-product-variant";
$product_variant->parent = $product;
$product_variant->title = "Test Variant";
$product_variant->padloper_product_stock->price = 22.25;
$product_variant->padloper_type = "Trousers";
$product_variant->padloper_categories = "Suits";
$product_variant->save();

This does indeed add the product as a variant below the aforementioned product.

So then I notice that I of course have to enable "use variants" to view them on the custom product dashboard view (they show if you view from admin pages under tree) and can also update the attribute field page reference, so it shows the selectized value. Therefore I've used:

$product = $pages->get(10203);
$product->of(false);
$product->padloper_product_attributes = 'Size';
$product->padloper_product_settings_use_variants = 1;
$product->save();

$product->padloper_product_attributes = "Size"; works perfectly, however $product->padloper_product_settings_use_variants = 1 doth not. I also tried $product->padloper_product_settings->use_variants = 1, but I assume this requires some other method to update its value?

Is it ok for me to go about this bulk creation as I am so far and could you shed some light on the variant matter if you have a mo.

Many Thanks,

Look forward to getting this up and ready to show you ?

Alex

  • Like 2
Link to comment
Share on other sites

On 4/24/2022 at 4:39 PM, alexm said:

what is the best way to create products via API

You are on the right track with your present code ?. I'll have a think about adding this feature to the API.

On 4/24/2022 at 4:39 PM, alexm said:
$product_variant->padloper_type = "Trousers";
$product_variant->padloper_categories = "Suits";

These two will not work since the fields padloper_type and padloper_categories belong to the parent product, i.e., in your case, $product.

 

On 4/24/2022 at 4:39 PM, alexm said:

So then I notice that I of course have to enable "use variants" to view them on the custom product dashboard view

Yes. This is necessary to explicitly specify that the product will use variants. This prevents 'accidental' variants if children were created for a product by mistake. This is unlikely but this safeguard is an extra layer to prevent that. The correct code for this should be:

<?php

namespace ProcessWire;

$product->padloper_product_settings->useVariants = 1;

Alternatively, since you are creating a new product (rather than amending an existing one), you can do it as follows, given that padloper_product_settings is a WireData  object.

<?php

namespace ProcessWire;

$productSettings = new WireData();
// 'physical' | 'physical_no_shipping' | 'digital' | 'service'
$productSettings->shippingType = 'physical';// product shipping type
$productSettings->taxable = 1; // bool int: is product taxable?
$productSettings->trackInventory = 1;// bool int: is product tracking inventory?
$productSettings->useVariants = 1; // bool int: is product using variants?
$productSettings->colour = '#d52f10ff'; // product colour
// ------
// add to product
$product->padloper_product_settings = $productSettings;
// OR
//$product->set('padloper_product_settings', $productSettings);
$product->save();

OR 

<?php

namespace ProcessWire;

$productSettings = [
	'shippingType' => 'physical',
	'taxable' => 1,
	'trackInventory' => 1,
	'useVariants' => 1,
	'colour' => '#d52f10ff',
];
$product->padloper_product_settings->setArray($productSettings);

$product->save();

 

On 4/24/2022 at 4:39 PM, alexm said:

and can also update the attribute field page reference

This requires setting the attribute on the parent product and the combination of attribute options on the variant. For instance, the product can have the attributes Size and Colour. The variants will take combinations of options for those attributes, i.e. combo of Size and Colour, e.g. Red x Small, Red x Medium, Black x Large, Black x Small, etc. The attribute options need to be children of the attribute. Hence, Red and Black are page children of Colour and Small, Medium and Large are children of Size. Attributes and Attribute Options use different templates. 

As you can see, it does get a little complicated. Above does not mean that each each possible Colour and Size combo should be created. For instance, you might not have a Brown x Large variant. Just skip creating it. 

So, for products that have and use variants, in addition to $product->padloper_product_attributes (a multi-page reference field), you also need to populate  $product_variant->padloper_product_attributes_options (a multi-page reference field). Please let me know if you need help with coding this or anything else.

 

On 4/24/2022 at 4:39 PM, alexm said:

Look forward to getting this up and ready to show you ?

Looking forward to seeing it! ?.

  • Like 1
Link to comment
Share on other sites

18 minutes ago, alexm said:

@kongondo apologies!! I didn't actually realise you'd replied to this!! Thank you.

No worries. Actually I think there's something funky going on with the notifications. I'm only getting notified about new threads. Subsequent posts are not triggering notifications.

Link to comment
Share on other sites

On 4/29/2022 at 1:14 PM, alexm said:

How can I then add the attribute option to the product variant page?

Hi @alexm,

This is a multi page reference field (padloper_product_attributes_options) so you would add using any of the ways shown here in ProcessWire docs using PageArray::add() syntax.

https://processwire.com/api/ref/page-array/add/

Here are some Padloper product variant examples

Please note that example 2 assumes the (parent) product page has the attributes Colour and Size in its field padloper_product_attributes as the respective parents of the attribute options Red and Medium.

<?php

namespace ProcessWire;

// the variant product page
$variantPage->of(false);

// Example 1: get and add one attribute option
$attributeOption = $pages->get(1234);// e.g. /colour/red/
// Add one page (attribute option)
$variantPage->padloper_product_attributes_options->add($attributeOption);

// Example 2: Add multiple pages (attribute options)
// $selector: full selector
$selector1 = "template=padloper-attribute-option,title=Red|Medium,check_access=0";
$variantPage->padloper_product_attributes_options->add($pages->find($selector1));
// OR
// $selector2: Padloper short syntax selector
$selector2 = "template=option,title=Red|Medium";
$variantPage->padloper_product_attributes_options->add($padloper->find($selector2));

// Example 3: Add one page by ID (attribute option)
$variantPage->padloper_product_attributes_options->add(1669);// e.g. /size/medium/

// save the variant product page's attribute options field
$variantPage->save('padloper_product_attributes_options');

Hope this helps. 

  • Like 1
Link to comment
Share on other sites

15 hours ago, kongondo said:

Hope this helps. 

Should have dug in deeper and taken a look. Sorry and thank you!

15 hours ago, kongondo said:

Just adding that I am working on an API for adding products, variants, categories, etc. This will be available via the global $padloper.

Amazing! Love it.

  • Like 1
Link to comment
Share on other sites

  • 1 month later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...