Cou*_*ent 5 php methods wordpress class woocommerce
在 Woocommerce 中,我试图找到一种类似于WC_Product方法的方法set_name(),或者set_status()例如设置产品类型,如Simple或Variation。
实际上我使用WC_Product这样的对象:
$product = new WC_Product;
$product->set_name($data['product']['title']);
$product->set_status('pending');
$product->save();
Run Code Online (Sandbox Code Playgroud)
如果用户勾选“简单产品”或“可变产品”,如何设置产品类型?
任何帮助表示赞赏。
该类没有类型方法WC_Product。产品类型由产品子类\xe2\x80\xa6管理
WC_Product您将首先获得所需产品类型的对象的空实例:
$product = new WC_Product_Variable();$product = new WC_Product_Grouped();$product = new WC_Product_External();$product = new WC_Product_Simple();然后,您可以在所选产品类型上使用任何WC_product方法或对象上相应的产品子类方法$product:
$product->set_name($data[\'product\'][\'title\']);\n$product->set_status(\'publish\');\nRun Code Online (Sandbox Code Playgroud)\n\n然后您将保存产品(并且它会选择性地返回产品 ID):
\n\n$product_id = $product->save();\nRun Code Online (Sandbox Code Playgroud)\n\n\n \n\n\n
产品变化
\n\n对于产品变体,情况有所不同,因为它们始终是现有可变产品的一部分\xe2\x80\xa6
\n\n// Get the Variable product object (parent) from an existing product ID\n$product = wc_get_product( $product_id );\n\n// Define the variation data\n$variation_post = array(\n \'post_title\' => $product->get_title(),\n \'post_name\' => \'product-\'.$product_id.\'-variation\',\n \'post_status\' => \'publish\',\n \'post_parent\' => $product_id,\n \'post_type\' => \'product_variation\',\n \'guid\' => $product->get_permalink()\n);\n\n// Creating the product variation\n$variation_id = wp_insert_post( $variation_post );\n\n// Get an instance of the product variation Object\n$variation = new WC_Product_Variation( $variation_id );\n\n// Set existing product attribute values (defined in the variable product)\n$variation->update_meta_data( \'attribute_\'.\'pa_color\', \'blue\' );\n$variation->update_meta_data( \'attribute_\'.\'pa_size\', \'xxl\' );\n\n# You can use any WC_Product allowed setter methods on the $variation object\n\n$variation->set_regular_price( 25 );\n$variation->set_price( 25 );\n\n$variation->save();\nRun Code Online (Sandbox Code Playgroud)\n\n请参阅这 2 个相关主题:
\n\n\n小智 0
该类型通常在您创建产品时设置:
$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 );
Run Code Online (Sandbox Code Playgroud)
我不确定是否有修改产品类型的功能,但可以使用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';
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10566 次 |
| 最近记录: |