首页 > 解决方案 > 使用未正确链接的 Woocommerce 方法添加的属性

问题描述

这不是线程的重复问题,例如:

这里的问题是没有添加每个说的属性。问题是将添加的属性选项与我猜的正确术语相关联。如下图所示,属性选项设置正确,但它们与它们的术语无关(如图一所示[文本而不是图三所示的灰色背景属性选项]),即使我手动运行每个产品的 set_object_terms 和关系在数据库中正确设置。

foreach($terms as $term_id) {
    $term = get_term( $term_id );
    $term_name = $term->name;
    $term_taxonomy = $term->taxonomy;
    $term_slug = $term->slug;
                            
    if( ! has_term( $term_name, $term_taxonomy, $parent_id ) ) {
        $set_terms = wp_set_object_terms($parent_id, $term_slug, $term_taxonomy, true );
    }
                            
}

在将某些属性从一个数据库传输到另一个数据库时,我遇到了一个问题,即属性被正确传输但它们以文本形式保存......

在此处输入图像描述

我需要在 wordpress 管理 > 产品编辑中手动单击“保存属性”...

在此处输入图像描述

让它真正在系统中正确注册,如此处所示。

在此处输入图像描述

主要问题是,虽然在第一个屏幕截图中显示为文本形式,但这些属性实际上在面向用户的商店中不可见,并且它们在产品的变体中也不可用。

我正在使用 woocommerce 方法来更新产品。这是一个代码示例:

$attribute_object = new WC_Product_Attribute();
$attribute_object->set_name( $attribute_name );
$attribute_object->set_options( $value );
$attribute_object->set_visible(1);
$attribute_object->set_variation(1);
$attribute_object->set_position(0);
$attribute_object->set_id( 0 );
$new_product_attributes[$attribute_name] = $attribute_object;
$new_product->set_attributes($new_product_attributes);
                        
$new_product->save();

如何让 WP 将产品与其新添加的属性/属性值同步?

图一和图三(灰色背景)中的属性选项有什么区别?

重现问题的简单代码

<?php
require_once(rtrim($_SERVER['DOCUMENT_ROOT'], '/') . '/wp-load.php');

global $wpdb;
wp_cache_flush();


$objProduct = new WC_Product_Variable();

$objProduct->set_name("Product Title");
$objProduct->set_status("publish");  // can be publish,draft or any wordpress post status
$objProduct->set_catalog_visibility('visible'); // add the product visibility status
$objProduct->set_description("Product Description");
$objProduct->set_sku("product-sku"); //can be blank in case you don't have sku, but You can't add duplicate sku's
$objProduct->set_price(10.55); // set product price
$objProduct->set_regular_price(10.55); // set product regular price
$objProduct->set_manage_stock(true); // true or false
$objProduct->set_stock_quantity(10);
$objProduct->set_stock_status('instock'); // in stock or out of stock value
$objProduct->set_backorders('no');
$objProduct->set_sold_individually(false);

$product_attributes = [];
$attribute_list = array('pa_marime' => array('l','m')); // -- choose any existing taxonomy and two existing attribute options belonging to that taxonomy (terms)

foreach($attribute_list as $attribute_name => $value) {
    $attribute_object = new WC_Product_Attribute();
    $attribute_object->set_name( $attribute_name );
    $attribute_object->set_options( $value );
    $attribute_object->set_visible( 1 );
    $attribute_object->set_variation( 1 );
    $attribute_object->set_id( 0 );
    $product_attributes[$attribute_name] = $attribute_object;
}

$objProduct->set_attributes($product_attributes);

$product_id = $objProduct->save();

$terms_list = [];

foreach($attribute_list as $attribute_name => $value) {
    foreach($value as $val) {
        $term = term_exists($val, $attribute_name);
        if($term) {
            array_push($terms_list, $term['term_id']);
        }
    }
}

foreach($terms_list as $term_id) {
    $term = get_term( $term_id );
    $term_name = $term->name;
    $term_taxonomy = $term->taxonomy;
    $term_slug = $term->slug;
    
    if( ! has_term( $term_name, $term_taxonomy, $product_id ) ) {
        $set_terms = wp_set_object_terms($product_id, $term_slug, $term_taxonomy, true );   
    }
    
}

使用以下规范在 Wordpress 安装中测试代码:

WP 版本:5.4.2 Woocommerce 版本:4.2.0

可能也适用于其他版本。

================== 编辑:附加细节==========================

我发现基本上发生的情况是,在设置属性时,woocommerce 不会将属性选项识别为分类法。

Array
(
    [pa_marime] => Array
        (
            [name] => pa_marime
            [value] => 1444 | 1445
            [position] => 0
            [is_visible] => 1
            [is_variation] => 1
            [is_taxonomy] => 0
        )

)

所以现在的问题是:

在 WC_Product_Attribute() ->set_options() 中为 set_attributes 方法将值识别为分类法的正确格式是什么?

我努力了:

$values = array(term_1_id, term_2_id); // array of existing term ids
$values = array('term_1_id' => 'term_1_name', 'term_2_id' => 'term_2_name'); // array of existing term_id and term_name pairs
$values = array('term_1_name', 'term_2_name); // array of existing term names
$values = array('term_1_slug', 'term_2_slug); // array of existing term slugs

以上所有导致 wc_product->set_attributes 无法将它们识别为分类法,即使它们是有效的术语/分类法。

标签: phpwordpresswoocommerceproducttaxonomy-terms

解决方案


推荐阅读