通过 WooCommerce 中的特定数据以编程方式添加产品变体
Add product variations programmatically via specific data in WooCommerce
我想做的是保存一个新的变体,但是在保存它时,它会抛出一个错误
在 string 上调用成员函数 get_name();
正在创建变体,但没有任何属性值。
public static function create_product_variants($id,$product,$variant_data){
for($i=0; $i< count($variant_data); $i++){
$variant = new WC_Product_Variation();
$variant->set_parent_id($id);
foreach($variant_data[$i] as $key => $value){
if($key == 'attribute_name'){
$attribute_name = $value;
}
if($key == 'variant'){
if(gettype($value) == 'string'){
$variants = [$value];
}else{
$variants = $value;
}
}
}
if(count($variants) == count($attribute_name)){
$variant_attributes = array();
for($j=0; $j< count($variants); $j++){
$variant_attributes[$attribute_name[$j]] = $variants[$j];
}
$variant->set_attributes($variant_attributes);
}
$id = $variant->save();
}
}
经过$variant_data
的数据是这样的:
[
{
attribute_name: ["Color","Size"]
price: "10"
quantity: "20"
variant: ["red","24"]
}
]
$id
是 product_id
像
一样简单
public static function create_product_variants($id,$product,$variant_data){
foreach ($variant_data as $object){
$variant = new WC_Product_Variation();
$variant->set_parent_id($product->get_id());
$attributes = array_combine($object->attribute_name, $object->variant)
$variant->set_attributes($attributes);
$variant->save();
}
}
如果对象是
[
{
attribute_name: ["Color","Size"]
price: "10"
quantity: "20"
variant: ["red","24"]
}
]
您的代码中存在一些复杂情况和错误。而是尝试重新访问以下代码:
public static function create_product_variants( $id, $product, $data ){
foreach( $data as $variation_data ){
if( isset($variation_data['attribute_name']) && isset($variation_data['variant']) ){
$variation = new WC_Product_Variation();
$variation->set_parent_id($id); // Set parent ID
$variation->set_regular_price($variation_data['price']); // Set price
$variation->set_price($variation_data['price']); // Set price
// Enable and set stock
if ( isset($variation_data['quantity']) ) {
$variation->set_manage_stock(true);
$variation->set_stock_quantity($variation_data['quantity']);
$variation->set_stock_status('instock');
}
$attributes = array(); // Initializing
$attribute_names = (array) $variation_data['attribute_name'];
$attribute_terms = (array) $variation_data['variant'];
// Formatting attributes data array
foreach( $attribute_names as $key => $attribute_name ){
$attributes[sanitize_title($attribute_name)] = $attribute_terms[$key];
}
$variation->set_attributes($attributes); // Set attributes
$variation_id = $variation->save(); // Save to database (return the variation Id)
}
}
}
它应该更好用。
变化将是 created/saved,方法 get_name()
这次应该不会抛出任何错误。
我想做的是保存一个新的变体,但是在保存它时,它会抛出一个错误 在 string 上调用成员函数 get_name(); 正在创建变体,但没有任何属性值。
public static function create_product_variants($id,$product,$variant_data){
for($i=0; $i< count($variant_data); $i++){
$variant = new WC_Product_Variation();
$variant->set_parent_id($id);
foreach($variant_data[$i] as $key => $value){
if($key == 'attribute_name'){
$attribute_name = $value;
}
if($key == 'variant'){
if(gettype($value) == 'string'){
$variants = [$value];
}else{
$variants = $value;
}
}
}
if(count($variants) == count($attribute_name)){
$variant_attributes = array();
for($j=0; $j< count($variants); $j++){
$variant_attributes[$attribute_name[$j]] = $variants[$j];
}
$variant->set_attributes($variant_attributes);
}
$id = $variant->save();
}
}
经过$variant_data
的数据是这样的:
[
{
attribute_name: ["Color","Size"]
price: "10"
quantity: "20"
variant: ["red","24"]
}
]
$id
是 product_id
像
一样简单public static function create_product_variants($id,$product,$variant_data){
foreach ($variant_data as $object){
$variant = new WC_Product_Variation();
$variant->set_parent_id($product->get_id());
$attributes = array_combine($object->attribute_name, $object->variant)
$variant->set_attributes($attributes);
$variant->save();
}
}
如果对象是
[
{
attribute_name: ["Color","Size"]
price: "10"
quantity: "20"
variant: ["red","24"]
}
]
您的代码中存在一些复杂情况和错误。而是尝试重新访问以下代码:
public static function create_product_variants( $id, $product, $data ){
foreach( $data as $variation_data ){
if( isset($variation_data['attribute_name']) && isset($variation_data['variant']) ){
$variation = new WC_Product_Variation();
$variation->set_parent_id($id); // Set parent ID
$variation->set_regular_price($variation_data['price']); // Set price
$variation->set_price($variation_data['price']); // Set price
// Enable and set stock
if ( isset($variation_data['quantity']) ) {
$variation->set_manage_stock(true);
$variation->set_stock_quantity($variation_data['quantity']);
$variation->set_stock_status('instock');
}
$attributes = array(); // Initializing
$attribute_names = (array) $variation_data['attribute_name'];
$attribute_terms = (array) $variation_data['variant'];
// Formatting attributes data array
foreach( $attribute_names as $key => $attribute_name ){
$attributes[sanitize_title($attribute_name)] = $attribute_terms[$key];
}
$variation->set_attributes($attributes); // Set attributes
$variation_id = $variation->save(); // Save to database (return the variation Id)
}
}
}
它应该更好用。
变化将是 created/saved,方法 get_name()
这次应该不会抛出任何错误。