如何在 WooCommerce 中更新可变产品的每个产品变体
How to update each product variation of a variable product in WooCommerce
我有一个包含变体列表的产品,例如,其中一个变体是“36”(ID:17393)。
我想为该产品变体设置新价格和新数量(使用外部信息)。
目前,我有这段代码,但我有一些我不知道的功能。
<?php
if ( $product->is_type( 'variable' ) ) {
$dataCSV = "36,2.0" , "37,3.0" , "39,4.0"; //example of external info
//$dataCSV have for each "talla" the quantity
$misAtributos = $product->get_attribute('Tallas');
//$misAtributos = 35 | 36 | 37 | 38 | 39 | 40
$AllTallas= explode(" | ", $misAtributos);
foreach ($AllTallas as $key => $talla) {
foreach ($dataCSV as $key => $Qnty) {
//first element [36, 2.0]
//$Qnty[0] = 36
//$Qnty[1] = 2.0
if($talla = $Qnty[0]){
//Update stock of and price.
}
}
echo '<br>'.(float)$value;
}
}
?>
- 如何获取产品 ID (17393) 而不是变体名称 (36)?
- 如何为该产品变体设置新价格和新数量?
首先,您的 $dataCSV
(外部信息)应该需要转换为多维显式格式化数组,而不是字符串…
然后您可以通过这种方式遍历父变量产品的每个变体 ID(并更新数据):
<?php
if ( $product->is_type( 'variable' ) ) {
$dataCSV = "36,2.0" , "37,3.0" , "39,4.0"; // <== This requires to be a multidimensional array
$attribute_label_name = 'Tallas';
// Loop through the variation IDs
foreach( $product->get_children() as $key => $variation_id ) {
// Get an instance of the WC_Product_Variation Object
$variation = wc_get_product( $variation_id );
// Get the variation attaribute "size" value
$size = $product->get_attribute($attribute_label_name);
// ------------------------------
// Then in between your code HERE … / …
// ------------------------------
// Set the stock quantity
$variation->set_stock_quantity($stock_quantity);
// Set the stock status
$variation->set_stock_status('instock');
// Set price
$variation->set_regular_price($price);
$variation->set_price($price);
// Save data (refresh cached data)
$variation->save();
}
}
?>
我有一个包含变体列表的产品,例如,其中一个变体是“36”(ID:17393)。 我想为该产品变体设置新价格和新数量(使用外部信息)。
目前,我有这段代码,但我有一些我不知道的功能。
<?php
if ( $product->is_type( 'variable' ) ) {
$dataCSV = "36,2.0" , "37,3.0" , "39,4.0"; //example of external info
//$dataCSV have for each "talla" the quantity
$misAtributos = $product->get_attribute('Tallas');
//$misAtributos = 35 | 36 | 37 | 38 | 39 | 40
$AllTallas= explode(" | ", $misAtributos);
foreach ($AllTallas as $key => $talla) {
foreach ($dataCSV as $key => $Qnty) {
//first element [36, 2.0]
//$Qnty[0] = 36
//$Qnty[1] = 2.0
if($talla = $Qnty[0]){
//Update stock of and price.
}
}
echo '<br>'.(float)$value;
}
}
?>
- 如何获取产品 ID (17393) 而不是变体名称 (36)?
- 如何为该产品变体设置新价格和新数量?
首先,您的 $dataCSV
(外部信息)应该需要转换为多维显式格式化数组,而不是字符串…
然后您可以通过这种方式遍历父变量产品的每个变体 ID(并更新数据):
<?php
if ( $product->is_type( 'variable' ) ) {
$dataCSV = "36,2.0" , "37,3.0" , "39,4.0"; // <== This requires to be a multidimensional array
$attribute_label_name = 'Tallas';
// Loop through the variation IDs
foreach( $product->get_children() as $key => $variation_id ) {
// Get an instance of the WC_Product_Variation Object
$variation = wc_get_product( $variation_id );
// Get the variation attaribute "size" value
$size = $product->get_attribute($attribute_label_name);
// ------------------------------
// Then in between your code HERE … / …
// ------------------------------
// Set the stock quantity
$variation->set_stock_quantity($stock_quantity);
// Set the stock status
$variation->set_stock_status('instock');
// Set price
$variation->set_regular_price($price);
$variation->set_price($price);
// Save data (refresh cached data)
$variation->save();
}
}
?>