【问题标题】:woocommerce product attribute not working programmaticallywoocommerce 产品属性无法以编程方式工作
【发布时间】:2020-08-21 06:02:42
【问题描述】:

我想在 woocommerce 中以编程方式应用属性,因为我使用了此代码,但它不起作用,任何人都可以查看我的代码并帮助我解决此问题吗?

$product_id = 4931;
    $attribute_name = "test_attribute";
    $attribute_value = "test_attribute";
    $term_taxonomy_ids = wp_set_object_terms($product_id, $attribute_value, $attribute_name, true);
    $data = array(
    $attribute_name => array(
    'name' => $attribute_name,
    'value' => '',
    'is_visible' => '1',
    'is_variation' => '0',
    'is_taxonomy' => '1'
    )
    );
    //First getting the Post Meta
    $_product_attributes = get_post_meta($product_id, '_product_attributes', TRUE);
    //Updating the Post Meta
    update_post_meta($product_id, '_product_attributes', array_merge($_product_attributes, $data));
}

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    我建议使用 wc-functions 来处理产品数据,因为将来产品数据可能不再存在于 wp_post 和 wp_post_meta 表中。

    $product_id = 4931;
    
    $p = wc_get_product( $product_id );
    $my_custom_value = $p->get_meta('_custom_value');
    
    // to change the costum attribute
    $p->update_meta_data('_custom_value', $my_new_value );
    $p->save();
    

    此链接有有用的信息:

    wc_get_products and WC_Product_Query

    Accessing WC_Product protected data in Woocommerce 3

    【讨论】:

      猜你喜欢
      • 2023-01-30
      • 2022-07-04
      • 2015-06-15
      • 2018-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多