我正在创建一个前端表单,允许用户使用预定义的属性和变体从前端向我的店铺发布可变产品。
我发现了一个非常有用的问题:here 它向我展示了如何将产品类型设置为变量,并在产品数据的属性部分指定预定义的属性。
然而,当我在Wordpress/Woocommerce的后端编辑产品时,我单击变体,但没有设置任何变体,我查看属性,我的“分辨率”属性是用我的3个项目设置的。
如何将这些属性设置为我的表单的变体?我是否需要使用wp\\U insert\\U post?在phpmyadmin中,看起来产品变体被分配给了一个parent\\u id(product id),而post类型是product\\u varion,依此类推。
$new_post = array(
\'post_title\' => esc_attr(strip_tags($_POST[\'postTitle\'])),
\'post_content\' => esc_attr(strip_tags($_POST[\'postContent\'])),
\'post_status\' => \'publish\',
\'post_type\' => \'product\',
\'tags_input\' => array($tags)
);
$skuu = rand();
$post_id = wp_insert_post($new_post);
update_post_meta($post_id, \'_sku\', $skuu );
//my array for setting the attributes
$avail_attributes = array(
\'high-resolution\',
\'medium-resolution\',
\'low-resolution\'
);
//Sets the attributes up to be used as variations but doesnt actually set them up as variations
wp_set_object_terms ($post_id, \'variable\', \'product_type\');
wp_set_object_terms( $post_id, $avail_attributes, \'pa_resolution\' );
$thedata = array(
\'pa_resolution\'=> array(
\'name\'=>\'pa_resolution\',
\'value\'=>\'\',
\'is_visible\' => \'1\',
\'is_variation\' => \'1\',
\'is_taxonomy\' => \'1\'
)
);
update_post_meta( $post_id,\'_product_attributes\',$thedata);
update_post_meta( $post_id, \'_visibility\', \'search\' );
update_post_meta( $post_id, \'_stock_status\', \'instock\');
因此,要清楚(我容易混淆)上述内容确实从前端创建了我的可变产品,当我查看后端的产品时,它是一个可变产品,它具有分辨率属性集,并将我的3个术语(高分辨率、中分辨率、低分辨率)作为属性。我只需要进一步将它们设置为变体,以便人们可以下订单。