- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我想以编程方式创建一个具有两个新变体属性的可变产品(“父”产品)——所有这些都来自 WordPress 插件(因此没有对 API 的 HTTP 请求)。
这两个变体属性也应该即时创建。
如何做到这一点?
(使用 WooCommerce 版本 3)
更新:我已经写了更多我希望的代码行,并尝试了很多方法来解决它,使用 wooCommerce 对象,并在数据库中添加了关于术语、术语、术语与帖子的关系的缺失数据,使用WordPress 数据库对象 - 但没有什么足以使其工作。而且我无法指出我哪里出错了 - 这就是为什么我无法提供更窄的问题 - stackoverflow 更适合的事情。
最佳答案
After: Create programmatically a WooCommerce product variation with new attribute values
在这里,您可以使用新的产品属性 + 值创建新的可变产品:
/**
* 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 );
set_transient( 'wc_attribute_taxonomies', false );
}
if( $set ){
$attributes = wc_get_attribute_taxonomies();
$args['attribute_id'] = get_attribute_id_from_name( $name );
$attributes[] = (object) $args;
//print_r($attributes);
set_transient( 'wc_attribute_taxonomies', $attributes );
} else {
return;
}
}
/**
* 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_col("SELECT attribute_id
FROM {$wpdb->prefix}woocommerce_attribute_taxonomies
WHERE attribute_name LIKE '$name'");
return reset($attribute_id);
}
/**
* Create a new variable product (with new attributes if they are).
* (Needed functions:
*
* @since 3.0.0
* @param array $data | The data to insert in the product.
*/
function create_product_variation( $data ){
if( ! function_exists ('save_product_attribute_from_name') ) return;
$postname = sanitize_title( $data['title'] );
$author = empty( $data['author'] ) ? '1' : $data['author'];
$post_data = array(
'post_author' => $author,
'post_name' => $postname,
'post_title' => $data['title'],
'post_content' => $data['content'],
'post_excerpt' => $data['excerpt'],
'post_status' => 'publish',
'ping_status' => 'closed',
'post_type' => 'product',
'guid' => home_url( '/product/'.$postname.'/' ),
);
// Creating the product (post data)
$product_id = wp_insert_post( $post_data );
// Get an instance of the WC_Product_Variable object and save it
$product = new WC_Product_Variable( $product_id );
$product->save();
## ---------------------- Other optional data ---------------------- ##
## (see WC_Product and WC_Product_Variable setters methods)
// THE PRICES (No prices yet as we need to create product variations)
// IMAGES GALLERY
if( ! empty( $data['gallery_ids'] ) && count( $data['gallery_ids'] ) > 0 )
$product->set_gallery_image_ids( $data['gallery_ids'] );
// SKU
if( ! empty( $data['sku'] ) )
$product->set_sku( $data['sku'] );
// STOCK (stock will be managed in variations)
$product->set_stock_quantity( $data['stock'] ); // Set a minimal stock quantity
$product->set_manage_stock(true);
$product->set_stock_status('');
// Tax class
if( empty( $data['tax_class'] ) )
$product->set_tax_class( $data['tax_class'] );
// WEIGHT
if( ! empty($data['weight']) )
$product->set_weight(''); // weight (reseting)
else
$product->set_weight($data['weight']);
$product->validate_props(); // Check validation
## ---------------------- VARIATION ATTRIBUTES ---------------------- ##
$product_attributes = array();
foreach( $data['attributes'] as $key => $terms ){
$taxonomy = wc_attribute_taxonomy_name($key); // The taxonomy slug
$attr_label = ucfirst($key); // attribute label name
$attr_name = ( wc_sanitize_taxonomy_name($key)); // attribute slug
// NEW Attributes: Register and save them
if( ! taxonomy_exists( $taxonomy ) )
save_product_attribute_from_name( $attr_name, $attr_label );
$product_attributes[$taxonomy] = array (
'name' => $taxonomy,
'value' => '',
'position' => '',
'is_visible' => 0,
'is_variation' => 1,
'is_taxonomy' => 1
);
foreach( $terms as $value ){
$term_name = ucfirst($value);
$term_slug = sanitize_title($value);
// Check if the Term name exist and if not we create it.
if( ! term_exists( $value, $taxonomy ) )
wp_insert_term( $term_name, $taxonomy, array('slug' => $term_slug ) ); // Create the term
// Set attribute values
wp_set_post_terms( $product_id, $term_name, $taxonomy, true );
}
}
update_post_meta( $product_id, '_product_attributes', $product_attributes );
$product->save(); // Save the data
}
代码在您的事件子主题(或事件主题)的 function.php 文件中。经过测试并有效。
用法(具有 2 个新属性 + 值的示例):
create_product_variation( array(
'author' => '', // optional
'title' => 'Woo special one',
'content' => '<p>This is the product content <br>A very nice product, soft and clear…<p>',
'excerpt' => 'The product short description…',
'regular_price' => '16', // product regular price
'sale_price' => '', // product sale price (optional)
'stock' => '10', // Set a minimal stock quantity
'image_id' => '', // optional
'gallery_ids' => array(), // optional
'sku' => '', // optional
'tax_class' => '', // optional
'weight' => '', // optional
// For NEW attributes/values use NAMES (not slugs)
'attributes' => array(
'Attribute 1' => array( 'Value 1', 'Value 2' ),
'Attribute 2' => array( 'Value 1', 'Value 2', 'Value 3' ),
),
) );
经过测试并有效。
相关:
关于php - 在 WooCommerce 中以编程方式创建可变产品和两个新属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47518333/
我需要按不同类别过滤我网站上的产品。例如,如果您选择“DRINKS”类别,它将向我显示属于该类别的产品。 为了更好地解释自己。 我需要按类别过滤我网站的出版物 [产品],例如,在选择一个类别时说“饮料
我有 orders 集合,其中包含 products 集合。我将一些产品 ID 作为列表传递给该方法。我需要返回与任何匹配的产品列表input 列表中的 id。 我需要像这样使用 foreach 循环
我已经为临时分发进行存档好几个月了,但今天突然我无法这样做,因为“存档”菜单项已被禁用。我没有改变任何东西。我完成了该项目的配置设置,看起来没问题。 我的临时个人资料即将在 14 天后过期。这可能是问
我正在尝试找出产品和产品属性之间的关系。我有一个 product 表和一个 product_attributes 表。产品可以具有多种属性。我需要查询来查找具有一个特定属性和另一个属性之一的所有产品。
我正在使用 MySQL Workbench 创建 EER 图。 实现产品、类别和公司表之间关系的最佳方式是什么? 我正在考虑这种关系,但考虑到我想让公司的客户管理自己的产品/类别,这是最好的方式吗?如
我正在使用 itertools 包,并尝试在具有 900 个值的数组中创建 1、2 和 3 的所有可能组合,然后将其转换为 30 x 30 矩阵。我必须执行此操作的代码在下面并且工作正常。 for d
我有几个关于 Cartridge 启动器的问题: 我的产品不需要评级或发布日期。他们永远不会出售。一些产品是可下载的,因此“num_in_stock”不相关或本质上是无限制的。没有颜色选项,只有尺寸。
在 MySQL 中,存储产品价格(或一般货币)的首选列类型是什么?谷歌知道我经常使用 DECIMAL of FLOAT,但我想知道哪个更好。 我存储的价格范围是 0.01 到 25.00。当然更高的值
在软件开发过程中,尤其是在准备将新功能或修复后的版本上线之前,进行详尽的自测和上线前检查是至关重要的。以下是一个从多个维度综合考量的上线升级检查清单(Checklist),旨在帮助团队确保软件质量、稳
我正在创建一个闪购网站,并且我已经在主页和商店页面上根据日期范围显示产品。但我也想根据其他地方的日期范围显示产品,因此使用简码。 这是我的代码: function testt($meta_query)
可以在 WooCommerce 上批量创建产品吗?我正在使用 wp-cli Product 命令,但似乎我必须一个一个地创建。 'My product 1'), array('title'
我有一个带有数量和价格列的 excel 文件,我用它来为插件 WooCommerce Dynamic Pricing 创建必要的输出的定价规则。 我几乎想通了,但是 WooCommerce 进口商正在
我刚刚继承了一个woocommerce项目,我需要将主页更改为仅显示特定品牌。他们设置了 Product-Data => Attribute => pa_brand。 如果我打印 pa_brand 数
在插件中如何使用 wc_get_products() 获取产品。或者有其他方法可以做到吗? if ( in_array( 'woocommerce/woocommerce.php', apply_fi
我正在做一个无法从公司网络外部访问的内部网,他们希望在 Plone 中显示一些关于文件下载和最常查看的页面的不错的统计数据。 由于网络限制,我无法使用谷歌分析或任何类型的外部服务,那么是否有任何产品可
我正在就以下问题寻求建议: 保留哪些产品 key 属于哪个客户端的列表。例如,如果我的产品 key 为 8456-7894-4567-7894,则应该这样设计,以便将列表写入数据库而不是文件。 如何将
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
尝试将产品搜索栏添加到 Wordpress 管理栏后端,以进行 Woocommerce 产品搜索。它将位于后端管理菜单栏的顶部,这样无论您在后端的哪个位置,都可以搜索 woo 的产品。我很接近但在小绊
这让我抓狂.. 我正在尝试根据特定属性查询和输出 WooCommerce 产品。例如,我设置了一个名为 on 的属性,可能的值为 yes或 no . 我使用以下查询: $args = array(
我正在尝试从 Shopify 商店获取所有产品的 JSON。我一直在向 {STORE URL}/products.json 端点。但这最终只显示了商店提供的部分产品(很多,但不是全部)。当我将参数更改
我是一名优秀的程序员,十分优秀!