通过WP API将产品添加到WooCommerce

时间:2017-11-23 作者:Piyush Rawat

我正在尝试创建WooCommerce扩展,以便通过API获取产品并将其存储在WordPress商店中。我成功地存储了简单的产品。但我一直致力于创建可变产品或任何其他类型的产品。

我还没有创建API,因为需要确定它需要的格式,但可以很容易地以中所述的格式实现this 链接

以下是我针对简单产品的工作代码:

if (in_array( \'woocommerce/woocommerce.php\', apply_filters( \'active_plugins\', get_option( \'active_plugins\' ) ) )) { 
function create_wc_product_by_api(){
    $api_response = wp_remote_get(\'www.myapi.com\');
    $body = wp_remote_retrieve_body($api_response);
    
    /****** $data will be replaced by my API response data *********/
    $data = [
        \'name\' => \'Premium Quality\',
        \'type\' => \'simple\',
        \'regular_price\' => \'21.99\',
        \'description\' => \'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.\',
        \'short_description\' => \'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.\',
        \'categories\' => [
            [
                \'id\' => 9
            ],
            [
                \'id\' => 14
            ]
        ],
        \'images\' => [
            [
                \'src\' => \'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg\',
                \'position\' => 0
            ],
            [
                \'src\' => \'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_back.jpg\',
                \'position\' => 1
            ]
        ]
    ];

    $wp_rest_request = new WP_REST_Request( \'POST\' );
    $wp_rest_request->set_body_params( $data );

    $products_controller = new WC_REST_Products_Controller;

    $wp_rest_response = $products_controller->create_item( $wp_rest_request );

}
}

UPDATE

我已经研究了默认的WooCommerce代码并找到了类WC_REST_Product_Variations_Controller. 看来这会解决我的问题。如果有效,将再次更新。

1 个回复
最合适的回答,由SO网友:Piyush Rawat 整理而成

在进行了大量搜索和到处碰头之后,我成功地实现了通过RESTAPI添加WooCommerce产品。以下是我的完整工作代码供参考:-

    $api_response = wp_remote_get(\'www.myapi.com\');
    $body = wp_remote_retrieve_body($api_response);

    /****** API not ready yet...working on it  *******/
    $data = [
        \'type\' => \'variable\',
        \'description\' => \'Trying it out for real\',
        \'short_description\' => \'Pellentesque habitant.\',
        \'categories\' => [
            [
                \'id\' => 37
            ],
            [
                \'id\' => 38
            ]
        ],
        \'images\' => [
            [
                \'src\' => \'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_4_front.jpg\',
                \'position\' => 0
            ]
        ],
        \'attributes\' => [
            [
                \'name\' => \'Color\',
                \'position\' => 0,
                \'visible\' => true,
                \'variation\' => true,
                \'options\' => [
                    \'Black\',
                    \'Green\'
                ]
            ],
            [
                \'name\' => \'Size\',
                \'position\' => 0,
                \'visible\' => true,
                \'variation\' => true,
                \'options\' => [
                    \'S\',
                    \'M\'
                ]
            ]
        ],
        \'default_attributes\' => [
            [
                \'name\' => \'Color\',
                \'option\' => \'Black\'
            ],
            [
                \'name\' => \'Size\',
                \'option\' => \'S\'
            ]
        ],
        \'variations\' => [ 
            [
                \'regular_price\' => \'29.98\', 
                \'attributes\' => [ 
                    [
                        \'slug\'=>\'color\',
                        \'name\'=>\'Color\',
                        \'option\'=>\'Black\'
                    ]
                ]   
            ],
            [
                \'regular_price\' => \'69.98\',
                \'attributes\' => [
                    [
                        \'slug\'=>\'color\', 
                        \'name\'=>\'Color\', 
                        \'option\'=>\'Green\'
                    ]
                ]
            ] 
        ]
    ];

    $wp_rest_request = new WP_REST_Request( \'POST\' );
    $wp_rest_request->set_body_params( $data );
    $products_controller = new WC_REST_Products_Controller;
    $res = $products_controller->create_item( $wp_rest_request );
    $res = $res->data;


    // The created product must have variations
    // If it doesn\'t, it\'s the new WC3+ API which forces us to build those manually
    if ( !isset( $res[\'variations\'] ) ){
        $res[\'variations\'] = array();
    }
    if ( count( $res[\'variations\'] ) == 0 && count( $data[\'variations\'] ) > 0 ) {
        if ( ! isset( $variations_controler ) ) {
            $variations_controler = new WC_REST_Product_Variations_Controller();
        }
        foreach ( $data[\'variations\'] as $variation ) {

            $wp_rest_request = new WP_REST_Request( \'POST\' );
            $variation_rest = array(
                \'product_id\' => $res[\'id\'],
                \'regular_price\' => $variation[\'regular_price\'],
                \'attributes\' => $variation[\'attributes\'],
            );
            $wp_rest_request->set_body_params( $variation_rest );
            $new_variation = $variations_controler->create_item( $wp_rest_request );
            $res[\'variations\'][] = $new_variation->data;
        }
    }

结束

相关推荐

WP-API RESTFULL JSON和POST META?

好的,我正在使用这个插件:https://github.com/WP-API/WP-API/ 将我的站点转换为rest完整API,效果很好。。然后我使用ths插件添加自定义帖子元:https://wordpress.org/plugins/types/我的问题是,我似乎无法查询url端点以返回我的帖子元,就像我在查询以下内容一样:get_post_meta( $post_id, $key = \'\', $single = false ) 我该怎么办?克里斯