使用JSON提要并尝试找出最佳的导入方法

时间:2019-07-17 作者:Darryl Morley

大家早上好,我需要能够每天导入json产品提要来更新我们的产品和库存。我正在开始编写一个插件来实现这一点,但如果有人能提供建议,我有几个问题要问?(我对Wordpress比较陌生)

下面是我需要导入的json示例:

{
            "Type": "Air Pistol",
            "Mechanism": "CO2",
            "Calibre": "4.5mm BB",
            "Make": "ASG",
            "Model": "SCHOFIELD",
            "Variant": "GREY",
            "Origin": null,
            "Orientation": "Right Handed",
            "Ejection": null,
            "Trigger": "0",
            "BarrelLength": "0.000",
            "BarrelLengthInches": null,
            "BarrelLengthFraction": null,
            "StockLength": "0.000",
            "StockLengthInches": null,
            "StockLengthFraction": null,
            "WeightPounds": null,
            "WeightOunces": null,
            "Choke1": null,
            "Choke2": null,
            "ScopeMake": null,
            "ScopeMag": null,
            "Summary": null,
            "Description": null,
            "Condition": "New",
            "Price": 170,
            "ExpiryDate": "2019-08-10",
            "ImageCount": 4,
            "StockNumber": "180328/003",
            "PairedGun": null,
            "SerialNumber": "17J11774",
            "ProofedLO": null,
            "ProofedRU": null,
            "Cased": null,
            "Chamber": null,
            "Paired": 0,
            "Images": [
                {
                    "Number": 1,
                    "FullPath": "https://images.guntrader.uk/GunImages/18/1803/18032/180329160440003/180329160440003-1.jpg",
                    "ThumbPath": "https://images.guntrader.uk/GunImages/Thumbnails/180329160440003-1-120x120.jpg"
                },
                {
                    "Number": 2,
                    "FullPath": "https://images.guntrader.uk/GunImages/18/1803/18032/180329160440003/180329160440003-2.jpg",
                    "ThumbPath": "https://images.guntrader.uk/GunImages/Thumbnails/180329160440003-2-120x120.jpg"
                },
                {
                    "Number": 3,
                    "FullPath": "https://images.guntrader.uk/GunImages/18/1803/18032/180329160440003/180329160440003-3.jpg",
                    "ThumbPath": "https://images.guntrader.uk/GunImages/Thumbnails/180329160440003-3-120x120.jpg"
                },
                {
                    "Number": 4,
                    "FullPath": "https://images.guntrader.uk/GunImages/18/1803/18032/180329160440003/180329160440003-4.jpg",
                    "ThumbPath": "https://images.guntrader.uk/GunImages/Thumbnails/180329160440003-4-120x120.jpg"
                }
            ],
            "ID": "180329160440003",
            "Licence": "No Licence",
            "Created": "2018-03-29 16:04:40",
            "Modified": "2019-07-10 02:00:47"
        },
我已经创建了类别来匹配我的数据中的“Type”键和所有其他键的属性,这是正确的方法吗?

假设这是正确的方法,我将如何将这些键映射到属性。

提前谢谢。

1 个回复
SO网友:Antti Koskinen

您可以使用以下组合update_post_meta(), set_objetc_terms(), 和wp_update_post() 更新您的产品。尽可能广泛地使用分类法(WooC中的产品类别和产品属性)来存储常见的产品数据是一个好主意,因为与将数据存储在post_meta.

这里有一个产品更新程序的概念,可以实现这一目的。这自然是未经测试的,需要您填写详细信息。

如果每次都有数百或数千个产品需要更新,那么我不确定这是否是最有效的更新方式。也许将更新拆分为多个批次(可能保存为瞬态),并使用(真实的)cronjob执行这些批次会有所帮助。

foreach ( $products_from_decoded_json as $product_data_array ) {
  product_updater( $product_data_array );
}

function product_updater( array $product_data ) {

  $meta = array(
    \'Price\' => \'_price\',
    // rest of the meta_key mappings
    // where json_key => WP meta_key
  );

  $terms = array(
    \'Make\' => \'pa_make\',
    \'Type\' => \'product_category\',
    // rest of product taxonomy mappings
    // where json_key => WP taxonomy_slug
  );

  $product_id = get_product_id( $product_data );

  update_product_meta( $product_id, $meta, array_filter( $product_data, function($key) {
    return isset( $meta[$key] );
  }, ARRAY_FILTER_USE_KEY ) );

  update_product_terms( $product_id, $terms, array_filter( $product_data, function($key) {
    return isset( $terms[$key] );
  }, ARRAY_FILTER_USE_KEY ) );

  // Check params array params from wp_insert_post docs
  wp_update_post( array(
    \'ID\' => $product_id,
    \'post_content\' => $product_data[\'Description\'],
    \'post_excerpt\' => $product_data[\'Summary\'],
    \'post_modified\' => $product_data[\'Modified\'],
  ) );

}

function get_product_id( $data ) {
  // Returned value should match an existing post ID
  // return int $id;
}

function update_product_meta( int $id, array $meta, array $product_data ) {
  foreach ( $meta as $json_key => $meta_key ) {
    // update_post_meta( $post_id, $meta_key, $meta_value, $prev_value = \'\' )
    update_post_meta( $id, $meta_key, $product_data[$json_key] );
  }
}

function update_product_terms( int $id, array $meta, array $product_data ) {
  foreach ( $terms as $json_key => $taxonomy ) {
    // get_term_by( $field, $value, $taxonomy, $output = OBJECT, $filter = \'raw\' )
    $term_data = get_term_by( \'name\', $product_data[$json_key], $taxonomy );
    if ( $term_data ) {
      // wp_set_object_terms( $object_id, $terms, $taxonomy, $append = false )
      wp_set_object_terms( $id, $term_data->term_id, $taxonomy, false );
    }
  }
}