在Woocommerce

时间:2018-05-01 23:16:17

标签: 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', $_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)); 
}

部分工作,因为我需要在后端产品&gt;中看到属性列出转换后的属性......

1 个答案:

答案 0 :(得分:1)

以下是完整而正确的方法......它部分基于this answer

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 create_attributtes_from_categories( $product_id = 0 ){
    $product_id = $product_id == 0 ? get_the_ID() : $product_id;
    $taxonomy = 'product_cat';
    $product_categories = wp_get_post_terms( $product_id, $taxonomy );
    $product_categories_data = $product_cats = $product_attributes = array();

    // 1. Get and process product categories from a product
    if( sizeof($product_categories) > 0 ){
        // 1st Loop get parent/child categories pairs
        foreach ( $product_categories as $product_category ) {
            $term_name   = $product_category->name; // Category name
            // keep product categories that have parent category
            if( $product_category->parent > 0 ){
                $parent_name = get_term_by( 'id', $product_category->parent, $taxonomy)->name; // Parent category name
                // Set them in the array
                $product_categories_data[$parent_name] = $term_name;
            }
        }
        // 2nd Loop: The get missing categories (the last child category or a unique category) and assign an temporary value to it
        foreach ( $product_categories as $product_category ) {
            if( ! in_array($product_category->name, array_keys($product_categories_data) ) ){
                // Assign an temporary value for the category name in the array of values
                $product_categories_data[$product_category->name] = 'Temp';
            }
        }
    }

    // 2. create and process product attributes and values from the product categories
    if( sizeof($product_categories_data) > 0 ){
        // Get product attributes post meta data
        $existing_data_attributes = (array) get_post_meta( $product_id, '_product_attributes', true );
        // Get the position
        if( sizeof($existing_data_attributes) > 0 )
            $position = sizeof($existing_data_attributes);

        // Loop through our categories array
        foreach ( $product_categories_data as $key_label => $value_name ) {
            $taxonomy = wc_attribute_taxonomy_name($key_label);
            $attr_name = wc_sanitize_taxonomy_name($key_label); // attribute slug name

            // NEW Attributes: Register and save them (if it doesn't exits)
            if( ! taxonomy_exists( $taxonomy ) )
                save_product_attribute_from_name( $attr_name, $key_label );

            // Check if the Term name exist and if not we create it.
            if( ! term_exists( $value_name, $taxonomy ) ){
                wp_insert_term( $value_name, $taxonomy, array('slug' => sanitize_title($value_name) ) );

                // Set attribute value for the product
                wp_set_post_terms( $product_id, $value_name, $taxonomy, true );
            }

            if( ! in_array( $taxonomy, array_keys($existing_data_attributes) ) ){
                $position++;
                // Setting formatted post meta data if it doesn't exist in the array
                $product_attributes[$taxonomy] = array (
                    'name'         => $taxonomy,
                    'value'        => '',
                    'position'     => $position,
                    'is_visible'   => 1,
                    'is_variation' => 1,
                    'is_taxonomy'  => 1
                );
            }
        }
        // Save merged data updating the product meta data
        update_post_meta( $product_id, '_product_attributes', array_merge( $existing_data_attributes, $product_attributes ) );
    }
}

代码进入活动子主题(或活动主题)的function.php文件。经过测试并正常工作。

3)用法:

  

我仍然要解决一个小问题 ...
  函数需要运行2次才能正确设置属性术语值

     

但它有效,我会尽快解决。

1)对于当前产品(当前产品发布ID):

create_attributtes_from_categories();
create_attributtes_from_categories();

2)对于定义的产品ID (此处为整数)

create_attributtes_from_categories(746);
create_attributtes_from_categories(746);

3)对于动态产品ID,在具有已定义$product_id变量的函数内:

create_attributtes_from_categories( $product_id );
create_attributtes_from_categories( $product_id );