在 Woocommerce 上为购买的产品自动设置特定的属性术语值 [英] Auto set specific attribute term value to purchased products on Woocommerce
问题描述
我想在下订单并处于暂停"状态时自动为订购的产品添加一个特定的属性值(之前已设置).
I want to automatically add a specific attribute value (which was previously set up) to ordered products when the order is placed and has "on-hold" status.
我销售独特的产品,并设置了STOCK"属性和Out Of Stock"(缺货)值.
I sell unique products and I have set up the "STOCK" attribute and the "Out Of Stock" (out-of-stock) value.
当下订单并处于暂停"状态时,我想自动更改所订购产品的特色状态,并为其添加缺货属性值.
When an order is placed and has "on-hold" status, I want to automatically change the featured status of the ordered products and also to add the out-of-stock attribute value to it.
特色部分已完成并有效,但我不知道如何为产品添加特定的属性值.
The featured part is done and works, but I can't figure out how to add a specific attribute value to the products.
这是我的代码:
add_action('woocommerce_order_status_on-hold', 'order_status_on_hold_update_products', 20, 2);
function order_status_on_hold_update_products( $order_id, $order ) {
foreach ( $order->get_items() as $item_id => $item ) {
$product = $item->get_product();
$product->set_featured(true);
$product->set_attributes(???); // I don't know if and how set_attributes() should be used
$product->save();
}
推荐答案
要设置库存状态缺货",您将使用 WC_Product
方法 set_stock_status()
this方式:
To set stock status "Out of Stock" you will use the WC_Product
method set_stock_status()
this way:
$product->set_stock_status('outofstock'); // Or "instock"
$product->save();
在你的挂钩函数中设置产品属性项(也适用于可变产品):
To set a product attribute term in your hooked function (work for variable products too):
add_action('woocommerce_order_status_on-hold', 'order_status_on_hold_update_products', 20, 2);
function order_status_on_hold_update_products( $order_id, $order ) {
foreach ( $order->get_items() as $item_id => $item ) {
$product = $item->get_product();
// Handling variable products
$_product = $product->is_type('variation') ? wc_get_product( $item->get_product_id() ) : $product;
$_product->set_featured( true );
// Your product attribute settings
$taxonomy = 'pa_stock'; // The taxonomy
$term_name = "Out Of Stock"; // The term
$attributes = (array) $_product->get_attributes();
$term_id = get_term_by( 'name', $term_name, $taxonomy )->term_id;
// 1) If The product attribute is set for the product
if( array_key_exists( $taxonomy, $attributes ) ) {
foreach( $attributes as $key => $attribute ){
if( $key == $taxonomy ){
$attribute->set_options( array( $term_id ) );
$attributes[$key] = $attribute;
break;
}
}
$_product->set_attributes( $attributes );
}
// 2. The product attribute is not set for the product
else {
$attribute = new WC_Product_Attribute();
$attribute->set_id( sizeof( $attributes) + 1 );
$attribute->set_name( $taxonomy );
$attribute->set_options( array( $term_id ) );
$attribute->set_position( sizeof( $attributes) + 1 );
$attribute->set_visible( true );
$attribute->set_variation( false );
$attributes[] = $attribute;
$_product->set_attributes( $attributes );
}
$_product->save();
// Append the new term in the product
if( ! has_term( $term_name, $taxonomy, $_product->get_id() ) )
wp_set_object_terms($_product->get_id(), $term_slug, $taxonomy, true );
}
}
代码位于活动子主题(或活动主题)的 function.php 文件中.它应该有效.
Code goes in function.php file of your active child theme (or active theme). it should works.
这篇关于在 Woocommerce 上为购买的产品自动设置特定的属性术语值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!