以编程方式为 WooCommerce 中的产品设置特定的“品牌”属性术语

问题描述 投票:0回答:1

我想检查例如品牌“Apple”是否作为品牌属性中的术语存在,然后将此产品的品牌苹果设置为分类类型。

$product_id = 214246; 
$product = wc_get_product($product_id);

// check if the product exist 
if ($product) {  
    $attributes = $product->get_attributes();

    if (!empty($attributes)) {
        // Loop through each attribute
        foreach ($attributes as $attribute) {
            // Get the attribute label
            $attribute_label = wc_attribute_label($attribute->get_name());

            // Get the attribute value
            $attribute_value = '';

            // Check if the attribute is a taxonomy type
            if ($attribute->is_taxonomy()) {
                $attribute_value = implode(', ', wc_get_product_terms($product->get_id(), $attribute->get_name(), array('fields' => 'names')));
            } 

            // Display the attribute label and value
            echo '<p><strong>' . $attribute_label . ':</strong> ' . $attribute_value . '</p>';
        }
    } else {
        
        // I want to add if the brands Apple exist if not then add apple as a brand then set this brand to this product.
   
    }
}

品牌是全球产品属性:

enter image description here

php wordpress woocommerce product taxonomy-terms
1个回答
0
投票

我们可以使用以下函数(添加到活动主题的functions.php文件中):

function set_pre_existing_attribute_to_product( $product, $attribute_name, $term_ids, $for_variations = false, $visible = true, $position = 0 ) {
    $taxonomy  = wc_attribute_taxonomy_name($attribute_name); // new attribute, already registered in wordpress

    $attribute = new WC_Product_Attribute(); // new attribute object
    $attribute->set_id( wc_attribute_taxonomy_id_by_name( $attribute_name ) ); // setting attribute configs
    $attribute->set_name($taxonomy); 
    $attribute->set_options($term_ids);
    $attribute->set_variation($for_variations); 
    $attribute->set_visible($visible); 
    $attribute->set_position($position);

    $attributes = (array) $product->get_attributes(); // Get existing attributes set in the product

    // If this attribute exists, we remove it
    if ( in_array( $taxonomy, array_keys($attributes) ) ) {
        unset($attributes[$taxonomy]);
    }
    // Merge attribute with other existing ones
    $product->set_attributes( array_merge( $attributes, array($taxonomy => $attribute) ) );
    $product->save();
}

那么您当前的代码将是:

$product_id = 214246; 
$product    = wc_get_product($product_id);

if ( is_a($product, 'WC_Product') ) {
    $taxonomy   = 'pa_brands'; // The taxonomy for "Brands"
    $attr_name  = wc_attribute_label($taxonomy); // Attribute label name
    $term_name  = 'Apple'; // Term name
    $term_id    = get_term_by('name', $term_name, $taxonomy)->term_id; // Can be replaced with the term ID

    // If "Apple" Brand is not set, we add it.
    if ( $product->get_attribute($taxonomy) !== $term_name ) {  
        set_pre_existing_attribute_to_product( $product, $attr_name, [$term_id]);
    }
    printf('<p><strong>%s:</strong> %s</p>', $attr_name, $term_name);
} 

已测试且有效。

注意: 之前需要在 WooCommerce 全局产品属性设置中为“品牌”设置“Apple”术语。

© www.soinside.com 2019 - 2024. All rights reserved.