Php 在Woocommerce 3中以编程方式创建产品时设置产品类型

Php 在Woocommerce 3中以编程方式创建产品时设置产品类型,php,wordpress,class,methods,woocommerce,Php,Wordpress,Class,Methods,Woocommerce,在Woocommerce中,我试图找到一种类似于WC\u Product方法set\u name()或set\u status()的方法,例如,将产品类型设置为Simple或Variation 实际上,我使用的是WC\u产品对象如下: $product = new WC_Product; $product->set_name($data['product']['title']); $product->set_status('pending');

在Woocommerce中,我试图找到一种类似于
WC\u Product
方法
set\u name()
set\u status()
的方法,例如,将产品类型设置为SimpleVariation

实际上,我使用的是
WC\u产品
对象如下:

    $product = new WC_Product;

    $product->set_name($data['product']['title']);

    $product->set_status('pending');

    $product->save();
如果用户选中“简单产品”或“可变产品”,如何设置产品类型


感谢您的帮助。

创建产品时通常会设置以下类型:

$prod_data = [
    'name'          => 'A great product',
    'type'          => 'simple',
    'regular_price' => '15.00',
    'description'   => 'A very meaningful product description',
    'images'        => [
        [
            'src'      => 'https://shop.local/path/to/image.jpg',
            'position' => 0,
        ],
    ],
    'categories'    => [
        [
            'id' => 1,
        ],
    ],
];

$woocommerce->post( 'products', $prod_data );

我不确定是否有修改产品类型的函数,但可以使用REST API进行修改:

$api_response = wp_remote_post( 'https://your-website/wp-json/wc/v2/products/{PRODUCT ID}', array(
    //'method'    => 'PUT',
    'headers' => array(
        'Authorization' => 'Basic ' . base64_encode( 'KEY:SECRET' )
    ),
    'body' => array(
        'type' => 'simple', // just update the product type
        // Options: simple, grouped, external and variable
        // more params http://woocommerce.github.io/woocommerce-rest-api-docs/?shell#product-properties
    )
) );

$body = json_decode( $api_response['body'] );
//print_r( $body );

if( wp_remote_retrieve_response_message( $api_response ) === 'OK' ) {
    echo 'The product ' . $body->name . ' has been updated';
}

WC\u产品
类没有类型方法。产品类型由产品子类管理

您将首先获得所需产品类型的
WC\u Product
对象的空实例:

  • 变量类型:
    $product=new WC_product_Variable()
  • 分组类型:
    $product=new WC_product_group()
  • 外部类型:
    $product=new WC_product_External()
  • Simple类型:
    $product=new WC_product_Simple()
  • 然后,您可以在所选产品类型上使用任何
    WC_product
    方法或
    $product
    对象上相应的产品子类方法:

    $product->set_name($data['product']['title']);
    $product->set_status('publish');
    
    然后您将保存产品(并且它将选择性地返回产品ID):

    请参见:


    产品变化

    对于产品变体,这是不同的,因为它们始终是现有可变产品的一部分

    // Get the Variable product object (parent) from an existing product ID
    $product = wc_get_product( $product_id );
    
    // Define the variation data
    $variation_post = array(
        'post_title'  => $product->get_title(),
        'post_name'   => 'product-'.$product_id.'-variation',
        'post_status' => 'publish',
        'post_parent' => $product_id,
        'post_type'   => 'product_variation',
        'guid'        => $product->get_permalink()
    );
    
    // Creating the product variation
    $variation_id = wp_insert_post( $variation_post );
    
    // Get an instance of the product variation Object
    $variation = new WC_Product_Variation( $variation_id );
    
    // Set existing product attribute values (defined in the variable product)
    $variation->update_meta_data( 'attribute_'.'pa_color', 'blue' );
    $variation->update_meta_data( 'attribute_'.'pa_size', 'xxl' );
    
    # You can use any WC_Product allowed setter methods on the $variation object
    
    $variation->set_regular_price( 25 );
    $variation->set_price( 25 );
    
    $variation->save();
    
    请参阅以下两个相关线程:


    尽管WooCommerce没有公开用于更改产品类型的API,但事实上,WooCommerce确实实现了此功能,因为有必要使用https://aaa.bbb.ccc/wp admin/post.php?post=12345&action=edit等管理页面上的“产品类型”选择框来支持更改产品类型。这方面的代码基本上如下所示:

        $classname = WC_Product_Factory::get_product_classname( $post_id, $product_type );
        $product   = new $classname( $post_id );
        $product->save();
    
    请注意,此处发生产品类型转换,因为id==$post\U id的现有产品的产品类型可能不同于$product\U type。显然,可以使用不同类型的产品构造函数,而不是最初用于构造产品的构造函数。这看起来很奇怪,但是当你在管理页面中更改产品类型时,WooCommerce代码就是这么做的


    RESTAPI以基本相同的方式实现产品类型的更改

    “我不确定是否有修改产品类型的函数”-AFAIK没有函数,但功能在WooCommerce中-请参阅我的答案。因此,与其使用JS,不如使用WC API创建提交表单。但这是同样的问题。如果我默认理解WC逻辑,那么一个产品就是一个简单的产品(类型),所以最好的方法是创建一个单选按钮。但是,如果Woocommerce不公开用于更改产品类型的API,该怎么办呢?对于初始产品创建,LoicTheAztec的答案正是您想要的(但您应该使用WC_product_Factory::get_product_classname()来获取类名)。这个答案是针对@entreprenerds的评论——“我不确定是否有修改产品类型的功能”。如果您需要以编程方式更改现有产品的产品类型,那么您可以直接执行我给出的代码。重点是,尽管WooCommerce没有公开用于更改产品类型的API函数,但它必须实现该功能以支持管理页面。然而,如果WooCommerce应该大幅更改产品的实现,它可能会更改现有产品上更改产品类型的方式。在这种情况下,WooCommerce将用其他代码替换给定的代码,因此使用依赖于当前实现且不封装在API函数中的代码是危险的。我建议使用函数WC_Product_Factory::get_classname_from_Product_type()来获取类名。虽然,该函数目前基本上不起任何作用,但未来WooCommerce可能会实现一个钩子,如果不使用API,您将看不到该钩子。@user9372991您给出了答案,我给出了我的答案……每种方法都有其优缺点,但这两种方法都取决于所需的要求,这在这个问题上并不清楚。我们将拭目以待。现在是今天,希望有很多方法。你的答案绝对正确,我的答案确实回答了另一个答案中出于好奇而评论的一个无关紧要的问题。@user9372991我基本上从未使用过WC_Product_Factory…但这是一个很好的途径,你的答案也很有价值。唯一的缺点是,这样看来,你需要一个免费的新帖子ID(或现有的)来让它工作。
        $classname = WC_Product_Factory::get_product_classname( $post_id, $product_type );
        $product   = new $classname( $post_id );
        $product->save();