在 Woocommerce 中添加新产品时自动添加所有产品属性
Posted
技术标签:
【中文标题】在 Woocommerce 中添加新产品时自动添加所有产品属性【英文标题】:Auto add all product attributes when adding a new product in Woocommerce 【发布时间】:2017-06-26 02:43:12 【问题描述】:我的一个客户要求根据 Wordpress 的插件 Woocommerce 进行这种奇怪的更改,以使事情“更轻松”..
是否有可能自动拥有所有产品属性 在创建产品时添加?
如果属性中没有输入值,是否可以自动禁用“在产品页面上可见”复选框?
任何帮助将不胜感激。
编辑(解释):
这就是上面解释的:
【问题讨论】:
你能再解释一下并举个例子吗? 请在发布您的查询之前参考***.com/help/how-to-ask 当您使用可变产品时,我认为自动在产品创建中自动添加变体有点复杂,因为当您创建它时,您必须首先定义(选择)什么样的产品它是……等等……可能可以做的是:1)首先创建一些可变产品(没有变体)……2)然后使用自定义函数,根据需要为每个创建的产品添加变体。但这是一个真实的开发案例…… 它不一定是可变产品,它也可以是单一产品,这是否仍然保持它将是一个漫长的开发案例的事实? 【参考方案1】:这是在创建新产品时自动添加所有现有产品变体 + 术语的方法。
代码(已注释):
add_action( 'save_post', 'auto_add_product_attributes', 50, 3 );
function auto_add_product_attributes( $post_id, $post, $update )
## --- Checking --- ##
if ( $post->post_type != 'product') return; // Only products
// Exit if it's an autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
// Exit if it's an update
if( $update )
return $post_id;
// Exit if user is not allowed
if ( ! current_user_can( 'edit_product', $post_id ) )
return $post_id;
## --- The Settings for your product attributes --- ##
$visible = ''; // can be: '' or '1'
$variation = ''; // can be: '' or '1'
## --- The code --- ##
// Get all existing product attributes
global $wpdb;
$attributes = $wpdb->get_results( "SELECT * FROM $wpdb->prefixwoocommerce_attribute_taxonomies" );
$position = 0; // Auto incremented position value starting at '0'
$data = array(); // initialising (empty array)
// Loop through each exiting product attribute
foreach( $attributes as $attribute )
// Get the correct taxonomy for product attributes
$taxonomy = 'pa_'.$attribute->attribute_name;
$attribute_id = $attribute->attribute_id;
// Get all term Ids values for the current product attribute (array)
$term_ids = get_terms(array('taxonomy' => $taxonomy, 'fields' => 'ids'));
// Get an empty instance of the WC_Product_Attribute object
$product_attribute = new WC_Product_Attribute();
// Set the related data in the WC_Product_Attribute object
$product_attribute->set_id( $attribute_id );
$product_attribute->set_name( $taxonomy );
$product_attribute->set_options( $term_ids );
$product_attribute->set_position( $position );
$product_attribute->set_visible( $visible );
$product_attribute->set_variation( $variation );
// Add the product WC_Product_Attribute object in the data array
$data[$taxonomy] = $product_attribute;
$position++; // Incrementing position
// Get an instance of the WC_Product object
$product = wc_get_product( $post_id );
// Set the array of WC_Product_Attribute objects in the product
$product->set_attributes( $data );
$product->save(); // Save the product
代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且有效。
【讨论】:
我看到这选择了属性选项卡中的所有全局属性。但它们不会被添加到变体选项卡中。他们应该这样做吗? @Jon 如果您设置$variation = '1';
而不是0
,属性将可用于变体
今天在使用这段代码时,遇到了致命错误:致命错误:未捕获错误:WC_Product_Attribute 类的对象无法在/nas/content/live/contribnews/wp-includes/ 中转换为字符串taxonomy.php 第 975 行
@ZachNicodemous 这个周末我会去看看【参考方案2】:
如果有人使用来自@LoicTheAztec 的代码 sn-p,设置另一个数组项可能会有所帮助。
// Get all term Ids values for the current product attribute (array)
$term_ids = get_terms(array('taxonomy' => $taxonomy, 'fields' => 'ids', 'hide_empty' => false));
希望对大家有所帮助。
【讨论】:
以上是关于在 Woocommerce 中添加新产品时自动添加所有产品属性的主要内容,如果未能解决你的问题,请参考以下文章
通过 URL 将免费产品添加到 WooCommerce 自动生成的优惠券
除某些 WooCommerce 产品类别外,自动将产品添加到购物车