我正在尝试创建一个以编程方式创建可变产品的 Wordpress 插件,但问题是,我想使用之前从管理仪表板手动定义的属性。我需要将属性分配给我正在创建的产品。
这是我正在使用的代码(不是我的,我从这个答案中得到它:以编程方式创建可变产品和 WooCommerce 中的两个新属性):
function addProduct(){
//Create main product
$product = new WC_Product_Variable();
//Create the attribute object
$attribute = new WC_Product_Attribute();
//pa_size tax id
$attribute->set_id( 0 ); // -> SET to 0
//pa_size slug
$attribute->set_name( 'Couleur' ); // -> removed 'pa_' prefix
//Set terms slugs
$attribute->set_options( array(
'Noir'
) );
$attribute->set_position( 0 );
//If enabled
$attribute->set_visible( 1 );
//If we are going to use attribute in order to generate variations
$attribute->set_variation( 1 );
$product->set_attributes(array($attribute)); …Run Code Online (Sandbox Code Playgroud)