Php 在Woocommerce中将产品类别转换为产品属性
在Woocommerce中,我尝试使用以下代码将产品类别转换为产品属性:Php 在Woocommerce中将产品类别转换为产品属性,php,wordpress,woocommerce,product,custom-taxonomy,Php,Wordpress,Woocommerce,Product,Custom Taxonomy,在Woocommerce中,我尝试使用以下代码将产品类别转换为产品属性: $all_categories = wp_get_post_terms( get_the_ID(), 'product_cat' );; $productid = get_the_ID(); foreach ($all_categories as $_term) { //$_term->name // $_term->parent $parent = get_term_by( 'id
$all_categories = wp_get_post_terms( get_the_ID(), 'product_cat' );;
$productid = get_the_ID();
foreach ($all_categories as $_term) {
//$_term->name
// $_term->parent
$parent = get_term_by( 'id', $_term->parent, 'product_cat');
// echo $parent->name . ': ' . $_term->name . '<br />';
$attribute_name = $parent->name; //slug of the attribute(taxonomy)
$attribute_value = $_term->name; //slug of the attribute value (term)
//Appending term to the object/product.
$term_taxonomy_ids = wp_set_object_terms(get_the_ID(), $attribute_value, $attribute_name, true);
$data = array(
$attribute_name => array(
'name' => wc_clean($attribute_name),
'value' => $attribute_value,
'is_visible' => '1',
'is_variation' => '1',
'is_taxonomy' => '0' // i tried with 1
)
);
// getting the Post Meta
$_product_attributes = get_post_meta($productid, '_product_attributes', TRUE);
if ( is_array($_product_attributes) ){
} else {
$_product_attributes = array();
}
//Updating the Post Meta
update_post_meta($productid, '_product_attributes', array_merge($_product_attributes, $data));
}
$all_categories=wp_get_post_terms(获取_ID(),'product_cat');;
$productid=get_the_ID();
foreach($所有类别为$期限){
//$\u术语->名称
//$\u术语->父项
$parent=get_term_by('id',$term->parent,'product_cat');
//echo$parent->name.':'.$\u term->name.
;
$attribute_name=$parent->name;//属性的slug(分类法)
$attribute\u value=$\u term->name;//属性值(term)的slug
//将术语附加到对象/产品。
$term\u taxonomy\u ID=wp\u set\u object\u terms(获取\u ID(),$attribute\u value,$attribute\u name,true);
$data=数组(
$attribute_name=>数组(
'name'=>wc\u clean($attribute\u name),
“值”=>$attribute\u值,
'是否可见'=>'1',
'is_variation'=>'1',
'is_taxonomy'=>'0'//我尝试了1
)
);
//获得博文元
$\u product\u attributes=get\u post\u meta($productid,“\u product\u attributes”,TRUE);
if(是_数组($_产品_属性)){
}否则{
$\产品\属性=数组();
}
//更新后元数据
更新发布元数据($产品ID,'.'产品属性',数组合并($产品属性,$数据));
}
是部分工作的因为我需要在后端产品>属性中看到所列的转换属性…这里是完整和正确的方法…它部分基于 1) 要使其正常工作,需要使用前两个实用程序功能:
/**
* Utility function: Get the product attribute ID from the name.
*
* @since 3.0.0
* @param string $name | The name (slug).
*/
function get_attribute_id_from_name( $name ){
global $wpdb;
$attribute_id = $wpdb->get_var("SELECT attribute_id
FROM {$wpdb->prefix}woocommerce_attribute_taxonomies
WHERE attribute_name LIKE '$name'");
return $attribute_id;
}
/**
* Utility function: Save a new product attribute from his name (slug).
*
* @since 3.0.0
* @param string $name | The product attribute name (slug).
* @param string $label | The product attribute label (name).
*/
function save_product_attribute_from_name( $name, $label='', $set=true ){
if( ! function_exists ('get_attribute_id_from_name') ) return;
global $wpdb;
$label = $label == '' ? ucfirst($name) : $label;
$attribute_id = get_attribute_id_from_name( $name );
if( empty($attribute_id) ){
$attribute_id = NULL;
} else {
$set = false;
}
$args = array(
'attribute_id' => $attribute_id,
'attribute_name' => $name,
'attribute_label' => $label,
'attribute_type' => 'select',
'attribute_orderby' => 'menu_order',
'attribute_public' => 0,
);
if( empty($attribute_id) )
$wpdb->insert( "{$wpdb->prefix}woocommerce_attribute_taxonomies", $args );
if( $set ){
$attributes = wc_get_attribute_taxonomies();
$args['attribute_id'] = get_attribute_id_from_name( $name );
$attributes[] = (object) $args;
set_transient( 'wc_attribute_taxonomies', $attributes );
} else {
return;
}
}
代码进入活动子主题(或活动主题)的function.php文件。测试和工作
2) 现在您重新访问的代码,插入到带有一个可选参数(产品ID)的函数中: 代码进入活动子主题(或活动主题)的function.php文件。测试和工作
3)用法: 我还有一个小错误需要解决 需要运行函数两次才能正确设置属性项值 但它是有效的,我会尽快解决这个问题 1) 对于当前产品(当前产品职务ID): 2) 对于定义的产品ID(此处为整数): 3) 对于动态产品ID,在具有定义的
$product\u ID
变量的函数内:
create_attributtes_from_categories( $product_id );
create_attributtes_from_categories( $product_id );
create_attributtes_from_categories();
create_attributtes_from_categories();
create_attributtes_from_categories(746);
create_attributtes_from_categories(746);
create_attributtes_from_categories( $product_id );
create_attributtes_from_categories( $product_id );