php - 在 Woocommerce 上的产品中设置产品类别术语

标签 php wordpress woocommerce product custom-taxonomy

在 Woocommerce 中,我有一个名为“平台”的产品属性,该属性的值为“Steam”:

and then the Attribute and

所以我批量导入产品并且属性已经存在。

但现在我必须为每个产品手动设置类别。 是否可以在函数中自动将值设置为产品类别?

enter image description here

这个函数返回属性值对吗?

function get_attribute_value_from_name( $name ){
  global $wpdb;
   $name = 'Platform';
    $attribute_value = $wpdb->get_var("SELECT attribute_value
     FROM {$wpdb->prefix}woocommerce_attribute_taxonomies
  WHERE attribute_name LIKE '$name'");
 return $attribute_value;
}

现在如何设置产品类别的值?

编辑:

$product = wc_get_product($id); //LOAD PRODUCT
global $product_attribute; //VARIABLE
$product_attribute = $product->get_attribute( 'Platform' ); //GET ATTRIBUTE OF PLATFORM
wp_set_object_terms( $post_id, $product_attribute, 'product_cat' ); //WRITE IT AS CATEGORY
$product->save();  //SAVE PRODUCT

这有意义吗?

最佳答案

更新 2 - 在产品中设置现有产品类别术语(使用定义的产品 ID):

// Get an instance of the WC_Product object
$product = wc_get_product( $product_id );

$term_names = $product->get_attribute( 'Platform' ); // Can have many term names (coma separated)

$term_names = explode( ',', $term_names);
$term_ids   = [];

// Loop through the terms
foreach( $term_names as $term_name ) {
    // Get the term ID and check if it exist
    if( $term_id = term_exists( $term_name, 'product_cat' ) ) {
        // Add each term ID in an array
        $term_ids[] = $term_id; 
    } 
}
// Append the product category terms in the product 
if( sizeof($term_ids) > 0 ) {
    $product->set_category_ids( $term_ids );
    $product->save();
}

下面是一个 Hook 函数的示例,将在产品编辑时自动设置产品类别条款

Note: the product category terms need to exist in woocommerce

// Backend product creation
add_action( 'woocommerce_admin_process_product_object', 'add_product_category_terms_to_product', 100, 1 );
function add_product_category_terms_to_product( $product ){
    global $pagenow;

    // Only on product Edit
    if( $pagenow != 'post.php' ) return; // Exit

    if( $term_names = $product->get_attribute( 'Platform' ) ) 
        $term_names = explode( ',', $term_names);
    else
        return; // Exit

    $term_ids = [];

    // Loop through the terms
    foreach( $term_names as $term_name ) {
        // Get the term ID and check if it exist
        if( $term_id = term_exists( $term_name, 'product_cat' ) ) {
            // Add each term ID in an array
            $term_ids[] = $term_id; 
        }
    }
    // replace the product categories terms in the product 
    if( sizeof($term_ids) > 0 ) {
        $product->set_category_ids( $term_ids );
    }
    // save is not needed in the function as this hook does that
}

代码进入事件子主题(或事件主题)的 function.php 文件。它应该有效。

关于php - 在 Woocommerce 上的产品中设置产品类别术语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53682483/

相关文章:

php - 将产品描述添加到 WooCommerce 电子邮件通知

php - Woocommerce 在订单查看页面中显示客户详细信息中的额外字段

php - 获取 Woocommerce 产品类别图像 url

php - 使用单独的列将 php 导出到 excel

php - Woocommerce,获取当前产品 ID

php - "Warning: Cannot modify header information - headers already sent by"错误

WordPress 自定义字段可从现有帖子中进行选择

php - 直接在信用卡中结帐并禁用电子邮件和密码输入

php - 步骤注册验证以获得更高的转化率

javascript - 检查输入中的值-Google Maps自动完成