Automatically change product tag on stock change in WooCommerce.

huangapple go评论69阅读模式
英文:

Automatically change product tag on stock change in WooCommerce

问题

I have a WooCommerce store, and every day I check my email that lists all products that are low in stock.

So if I see one of these products, I log in, find the product, and change the tag to 'Last Chance' which alerts my users that the item is almost out of stock, so you should buy it before it's gone.

This is fine, but it's a redundant task that I'm hoping can be automated.

Can I create a webhook that automatically applies this tag to any product that reaches low stock quantity in the inventory? It seems like it should be a native function of WooCommerce, but I don't see anywhere to do that.

Pseudocode:

<!-- language: lang-none -->
IF PRODUCT QUANTITY = 'low'
    ADD TAG 'Last Chance' TO PRODUCT
英文:

I have a WooCommerce store, and every day I check my email that lists all products that are low in stock.

So if I see one of these products, I log in, find the product, and change the tag to 'Last Chance' which alerts my users that the item is almost out of stock, so you should buy it before it's gone.

This is fine, but it's a redundant task that I'm hoping can be automated.

Can I create a webhook that automatically applies this tag to any product that reaches low stock quantity in the inventory? It seems like it should be a native function of WooCommerce, but I don't see anywhere to do that.

Pseudocode:

<!-- language: lang-none -->

IF PRODUCT QUANTITY = &#39;low&#39;
    ADD TAG &#39;Last Chance&#39; TO PRODUCT

答案1

得分: 0

以下是已翻译的内容:

"stock change events, triggers emails and adds order notes" 的可用挂钩位于

> https://github.com/woocommerce/woocommerce/blob/master/includes/wc-stock-functions.php
>
> 在这里我们找到了函数 wc_trigger_stock_change_notifications(),其中包含一些操作挂钩


可用的挂钩:

<!-- 语言:php -->

// 无库存
function action_woocommerce_no_stock( $wc_get_product ) {
	// 在这里执行操作...
}
add_action( &#39;woocommerce_no_stock&#39;, &#39;action_woocommerce_no_stock&#39;, 10, 1 );

<!-- 语言:php -->

// 低库存
function action_woocommerce_low_stock( $wc_get_product ) { 
	// 在这里执行操作...
}
add_action( &#39;woocommerce_low_stock&#39;, &#39;action_woocommerce_low_stock&#39;, 10, 1 );

<!-- 语言:php -->

// 订购缺货商品
function action_woocommerce_product_on_backorder( $array ) { 
	// 在这里执行操作...
}
add_action( &#39;woocommerce_product_on_backorder&#39;, &#39;action_woocommerce_product_on_backorder&#39;, 10, 1 );

所以,您可以使用 woocommerce_low_stock3.0 版中的 CRUD 对象 来实现您想要的功能。

<!-- 语言:php -->

function action_woocommerce_low_stock( $wc_get_product ) {    
	// 产品设置标签ID,可以添加多个ID,用逗号分隔
	$new_tag_ids = array( &#39;YOUR TAG ID&#39;, &#39;ANOTHER TAG ID&#39; );
	$wc_get_product-&gt;set_tag_ids( $new_tag_ids );

	// 可选:设置类别ID
	//$wc_get_product-&gt;set_category_ids( array( 39, 2 ) );

    // 保存
    $wc_get_product-&gt;save();
}
add_action( &#39;woocommerce_low_stock&#39;, &#39;action_woocommerce_low_stock&#39;, 10, 1 );

注意) 如果您不仅想要添加新的标签到产品,还要保留现有的标签。

将:

<!-- 语言:lang-php -->

// 产品设置标签ID,可以添加多个ID,用逗号分隔
$new_tag_ids = array( &#39;YOUR TAG ID&#39;, &#39;ANOTHER TAG ID&#39; );
$wc_get_product-&gt;set_tag_ids( $new_tag_ids );

替换为:

<!-- 语言:lang-php -->

// 获取当前标签ID(s)
$current_tag_ids = $wc_get_product-&gt;get_tag_ids();

// 产品设置标签ID(s),可以添加多个ID,用逗号分隔
$new_tag_ids = array( &#39;YOUR TAG ID&#39;, &#39;ANOTHER TAG ID&#39; );
$wc_get_product-&gt;set_tag_ids( array_unique( array_merge( $current_tag_ids, $new_tag_ids ), SORT_REGULAR ) );
英文:

The available hooks after stock change events, triggers emails and adds order notes are located in

> https://github.com/woocommerce/woocommerce/blob/master/includes/wc-stock-functions.php
>
> Where we find the function wc_trigger_stock_change_notifications() which contains some action hooks


Available hooks:

<!-- language: php -->

// No stock
function action_woocommerce_no_stock( $wc_get_product ) {
	// make action magic happen here... 
}
add_action( &#39;woocommerce_no_stock&#39;, &#39;action_woocommerce_no_stock&#39;, 10, 1 );

<!-- language: php -->

// Low stock
function action_woocommerce_low_stock( $wc_get_product ) { 
	// make action magic happen here... 
}
add_action( &#39;woocommerce_low_stock&#39;, &#39;action_woocommerce_low_stock&#39;, 10, 1 );

<!-- language: php -->

// On backorder
function action_woocommerce_product_on_backorder( $array ) { 
	// make action magic happen here... 
}
add_action( &#39;woocommerce_product_on_backorder&#39;, &#39;action_woocommerce_product_on_backorder&#39;, 10, 1 );


So for what you want you can use woocommerce_low_stock and CRUD Objects in 3.0

<!-- language: php -->

function action_woocommerce_low_stock( $wc_get_product ) {    
	// Product set tag id(s), multiple IDs can be added, separated by a comma
	$new_tag_ids = array( &#39;YOUR TAG ID&#39;, &#39;ANOTHER TAG ID&#39; );
	$wc_get_product-&gt;set_tag_ids( $new_tag_ids );

	// OPTIONAL: Set category ids
	//$wc_get_product-&gt;set_category_ids( array( 39, 2 ) );

    // Save
    $wc_get_product-&gt;save();
}
add_action( &#39;woocommerce_low_stock&#39;, &#39;action_woocommerce_low_stock&#39;, 10, 1 );

Note) If you don't just want to add new tags to the product, but also keep the existing ones.

Replace:

<!-- language: lang-php -->

// Product set tag id(s), multiple IDs can be added, separated by a comma
$new_tag_ids = array( &#39;YOUR TAG ID&#39;, &#39;ANOTHER TAG ID&#39; );
$wc_get_product-&gt;set_tag_ids( $new_tag_ids );

With:

<!-- language: lang-php -->

// Get current tag id(s)
$current_tag_ids = $wc_get_product-&gt;get_tag_ids();

// Product set tag id(s), multiple IDs can be added, separated by a comma
$new_tag_ids = array( &#39;YOUR TAG ID&#39;, &#39;ANOTHER TAG ID&#39; );
$wc_get_product-&gt;set_tag_ids( array_unique( array_merge( $current_tag_ids, $new_tag_ids ), SORT_REGULAR ) );

huangapple
  • 本文由 发表于 2023年5月10日 22:03:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76219364.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定