通过 Cron Job 或 REST_API 扩展产品更新时的功能执行

问题描述 投票:0回答:2

通过 Cron Job 或 REST_API 更新产品时是否可以执行此 php 函数? Cron JOB 正在更新 _stock2 元字段,rest_api 正在更新 woocommerce 产品的基本库存数量。

add_action('woocommerce_admin_process_product_object', 'update_product_tag_on_stock_change');
function update_product_tag_on_stock_change($product) {
    // Debugging: Log message to check if the function is being called
    error_log('Function update_product_tag_on_stock_change called.');

    if ($product->is_type('variable')) {
        $variations = $product->get_children();

        $total_stock_quantity = 0;
        $total_stock_quantity2 = 0;

        foreach ($variations as $variation_id) {
            $variation = wc_get_product($variation_id);
            $total_stock_quantity += intval($variation->get_stock_quantity()) - intval($variation->get_meta('_stock2'));
            $total_stock_quantity2 += intval($variation->get_meta('_stock2'));
        }

        // Debugging: Log total_stock_quantity and total_stock_quantity2
        error_log('Total Stock Quantity: ' . $total_stock_quantity);
        error_log('Total Stock Quantity2: ' . $total_stock_quantity2);

        if ($total_stock_quantity2 > 0 && $total_stock_quantity < 2) {
            wp_set_object_terms($product->get_id(), '3-5dni', 'product_tag', true);
            // Debugging: Log message when tag is set
            error_log('Tag set to 3-5dni');
        } else {
            wp_remove_object_terms($product->get_id(), '3-5dni', 'product_tag', true);
            // Debugging: Log message when tag is removed
            error_log('Tag removed from 3-5dni');
        }
    } else {
        $stock_quantity = intval($product->get_stock_quantity()) - intval($product->get_meta('_stock2'));
        $stock_quantity2 = intval($product->get_meta('_stock2'));

        // Debugging: Log stock_quantity and stock_quantity2
        error_log('Stock Quantity: ' . $stock_quantity);
        error_log('Stock Quantity2: ' . $stock_quantity2);

        if ($stock_quantity2 > 0 && $stock_quantity < 2) {
            wp_set_object_terms($product->get_id(), '3-5dni', 'product_tag', true);
            // Debugging: Log message when tag is set
            error_log('Tag set to 3-5dni');
        } else {
            wp_remove_object_terms($product->get_id(), '3-5dni', 'product_tag', true);
            // Debugging: Log message when tag is removed
            error_log('Tag removed from 3-5dni');
        }
    }
}

现在只有当我通过管理页面手动更新产品时此功能才有效。

php wordpress woocommerce cron woocommerce-rest-api
2个回答
0
投票

您的操作专门针对对象的管理处理,因此除非执行此挂钩,否则它不会执行:

woocommerce_admin_process_product_object

但是,除了上面的钩子之外,您还可以定位特定的钩子,并且可以将函数添加到多个钩子中:

add_action( 'woocommerce_admin_process_product_object', 'update_product_tag_on_stock_change' );
add_action( 'woocommerce_update_product', 'update_product_tag_on_stock_change', 10, 1 );

0
投票

如果您想在通过管理员、Cron Job 或 REST_API 请求更新产品时触发函数,您最好使用位于

WC_Product_Data_Store_CPT
类中的 woocommerce_update_product 钩子,该钩子有 2 个参数:
$product_id
 $product

重要提示:

  • 如果使用 Cron 作业,需要使用
    WC_Product
    CRUD 对象设置方法
    ,而不是 WordPress post 函数或直接 SQL UPDATE 查询,以触发 WooCommerce 相关挂钩。
  • 挂钩
    woocommerce_update_product
    仅在更新现有产品时触发。

现在您当前的代码可以进行优化和压缩,删除重复的东西。此外,在尝试添加或删除产品标签之前,您错过了检查产品中是否设置了产品标签“3-5dni”。

尝试以下修改后的代码:

add_action( 'woocommerce_update_product', 'update_product_tag_on_stock_change', 10, 2 );
function update_product_tag_on_stock_change( $product_id, $product ) {
    $taxonomy  = 'product_tag';
    $term_slug = '3-5dni';
    error_log('Function "update_product_tag_on_stock_change" executed.'); // Debugging (function executed)
    error_log('Product type: '.$product->get_type() ); // Debugging (product type)

    if ( $product->is_type('variable') ) {
        $stock_quantity = $stock_quantity2 = 0; // Initializing variables

        // Loop through available variations (array of variations objects)
        foreach ( $product->get_available_variations('objects') as $variation ) {
            $stock_quantity  += intval($variation->get_stock_quantity()) - intval($variation->get_meta('_stock2'));
            $stock_quantity2 += intval($variation->get_meta('_stock2'));
        }
    } else {
        $stock_quantity  = intval($product->get_stock_quantity()) - intval($product->get_meta('_stock2'));
        $stock_quantity2 = intval($product->get_meta('_stock2'));
    }
    error_log('Stock Quantity: ' . $stock_quantity); // Debugging: stock_quantity
    error_log('Stock Quantity2: ' . $stock_quantity2); // Debugging: stock_quantity2

    if ( $stock_quantity2 > 0 && $stock_quantity < 2 
    && ! has_term($term_slug, $taxonomy, $product_id) ) {
        wp_set_post_terms($product_id, $term_slug, $taxonomy, true); 
        error_log('Added tag "3-5dni".'); // Debugging: Tag added
    } elseif ( has_term($term_slug, $taxonomy, $product_id) ) {
        wp_remove_object_terms($product_id, $term_slug, $taxonomy);
        error_log('Removed tag "3-5dni".'); // Debugging: Tag removed
    }
}

应该可以。

© www.soinside.com 2019 - 2024. All rights reserved.