0

I have a XML file that consist of product information.

Format of XML file :

<product>
    <name>Product-1</name>
    <description>this is product 1 description</description>
    <category>Category-1<category>
    <merchant>1234</merchant>
    <price>12</price>
    <instock>1<instock>
    <stockstatus>45</stockstatus>
    <image>http://www.example.com/1.jpg</image>
    <manufacture>987</manufacture>
</product>

I need to write a script that will add these detail as a product. For now I am not using any WP plugin for eCommerce purpose.

I am planning to create a custom post type as wpproduct that will store these products details.

So for now I can use name as post title, description as post content and image as post thumbnail. And for storing the meta data I can store it in postmeta table.

So I can create the post by using wp_insert_post and whatever ID it will return, I will use that ID in update_post_meta to update the meta data such as price, stock status , stock quantity, manufacturer etc.

Like this it can be done..

Main Concern

So for a single product I need to write 1 wp_insert_post and then 6 update_meta_data query. So 7 query for 1 product. Not exactly 7 query as WP inbuilt function uses many query for validation check.

What will happen if my XML file has 1 lakh product. So for adding 1 lakh product 7 lakh mysql query will run.

Assumption

I am just eager to know like can I add custom column in WP wp_posts table so that I can add 1 product in 1 query only. I might use raw mysql query to add it.

Please help me and guide the best way to do it.

Thanks

1
  • I build stores this way and with good hosting, database maintenance and full page caching it's fine. I believe but need to check that WP pulls all post meta and caches it alongside the main query anyway. I wouldn't add columns to WP's tables as it seems likely to set you up for trouble at a later date. Commented Jul 23, 2016 at 19:27

2 Answers 2

1

I've checked and WordPress does, by default, retrieve & cache all post meta along with the main query's posts, so you're perfectly fine storing a product's additional fields as standard post meta.

You might want to store Brands (merchants? manufacturers?) as a custom taxonomy so that you can take advantage of built-in templates and queries to show all products of a certain brand.

3
  • Hi @Andy , my main concern is can I customise WP default table. If yes/no what will be its demerit.
    – Aftab
    Commented Jul 24, 2016 at 12:02
  • Well the obvious possibility is that if there were ever a new column with the same name as yours added in a future version of WP then they would clash. With the right (i.e. unlikely string) namespacing I guess that risk would be reduced to near nothing. However I believe (and I can't find the reference now) that the bigger problem would be that WP's dbDelta function could try to "repair" your change when you upgrade WP. Commented Jul 25, 2016 at 13:34
  • I will make the custom column name with a random string as prefix. I don't think WP will ever add a column like that.
    – Aftab
    Commented Jul 26, 2016 at 8:28
0

Import is inherently heavy operation, because it is bulk operation and writes are inherently very heavy (relative to reads).

If you are not time–constrained for the operation — does it really matter?

  1. Write your import as command line tool.
  2. Make sure you disable PHP timeout.
  3. Make sure you can efficiently restart process at certain point, in case something goes wrong.

After that it takes as long as it takes.

If you are time constrained (for example having to run this often/repeatedly) then you would need to approach this as general performance problem. That is you need to profile actual performance and determine what the bottlenecks are in the process.

In case if multiple queries are an issue you could consider staged process — when meta is first stored as single entry on import and then expanded into more convenient form at a later point.

I would recommend to stay away from modifying native tables since there seems to be little functional need to do so in this case.

2
  • Thanks for the reply. Now about staying away from modifying native tables, can you please give us more details on why we shouldn't do it. It will be great if you can let us know here or direct to some link(s) with details on why modifying the native tables is not such good idea :)
    – Aftab
    Commented Jul 26, 2016 at 13:35
  • Code is liability, everything you introduce needs to be maintained and kept compatible with the rest (forever). Why not is suboptimal approach, solutions should be driven by necessity rather than absence of objections.
    – Rarst
    Commented Jul 26, 2016 at 13:46

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.