在新的WooCommerce产品手动发布后如何调用功能

发布于 2025-01-21 07:33:34 字数 1340 浏览 0 评论 0原文

每当要手动创建产品时,我都在尝试创建额外的WooCommerce产品。

这就是我尝试的:

add_action('transition_post_status', 'on_product_creation', 10, 3);
 function on_product_creation($new_status, $old_status, $post) {
 if( 
        $old_status != 'publish' 
        && $new_status == 'publish' 
        && !empty($post->ID) 
        && in_array( $post->post_type, 
            array( 'product') 
            )
        ) {
            
            
         $post_data = array(
            'post_name'     => 'test2',
            'post_title'    => 'test2',
            'post_status'   => 'publish',
            'post_type'     => 'product',
        );

        $product_id = wp_insert_post( $post_data );
        $product = new WC_Product_Subscription( $product_id );
        $product->set_regular_price( 1 );
        $product->set_price( 1 );
        $product->save();   
        $id = $product->get_id();
        update_post_meta($id, "_subscription_length", 12);
        update_post_meta($id, "_subscription_price", 1); 
            
            
            
            
         $log = "productCreated";
         file_put_contents('./log_'.date("j.n.Y").'.log', $log, FILE_APPEND);
     }

  }

由于某种原因,我的功能在创建产品后不会运行。

我只有在手动创建产品以避免递归(产品创建会触发本身)时才试图发射钩子 一种了解产品是如何创建的方法?

I am trying to create an extra WooCommerce product whenever a product is being created manually.

This is what i tried:

add_action('transition_post_status', 'on_product_creation', 10, 3);
 function on_product_creation($new_status, $old_status, $post) {
 if( 
        $old_status != 'publish' 
        && $new_status == 'publish' 
        && !empty($post->ID) 
        && in_array( $post->post_type, 
            array( 'product') 
            )
        ) {
            
            
         $post_data = array(
            'post_name'     => 'test2',
            'post_title'    => 'test2',
            'post_status'   => 'publish',
            'post_type'     => 'product',
        );

        $product_id = wp_insert_post( $post_data );
        $product = new WC_Product_Subscription( $product_id );
        $product->set_regular_price( 1 );
        $product->set_price( 1 );
        $product->save();   
        $id = $product->get_id();
        update_post_meta($id, "_subscription_length", 12);
        update_post_meta($id, "_subscription_price", 1); 
            
            
            
            
         $log = "productCreated";
         file_put_contents('./log_'.date("j.n.Y").'.log', $log, FILE_APPEND);
     }

  }

For some reason my function doesn't run after I create a product.

I am trying to fire the hook only when the product been created manually to avoid recursion (a product creation will trigger itself) is there
a way to know how the product have been created ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮萍、无处依 2025-01-28 07:33:34

此挂钩woocommerce_process_product_meta才能在手动创建/更新产品时运行。 WC 6.2 测试确定

/**
 * Save meta box data.
 *
 * @param int     $post_id WP post id.
 * @param WP_Post $post Post object.
 */
function action_woocommerce_process_product_meta($post_id, $post) {
    // make action magic happen here... 


    $product = new WC_Product_Simple();
    $product->set_name('Sample product');
    $product->set_status('publish');
    $product->set_catalog_visibility('visible');
    $product->set_price(19.99);
    $product->set_regular_price(19.99);
    $product->set_sold_individually(true);
    $product->save();
}

// add the action 
add_action('woocommerce_process_product_meta', 'action_woocommerce_process_product_meta', 10, 2);

This hook woocommerce_process_product_meta will run only when the product is manually created/updated. Tested OK with WC 6.2

/**
 * Save meta box data.
 *
 * @param int     $post_id WP post id.
 * @param WP_Post $post Post object.
 */
function action_woocommerce_process_product_meta($post_id, $post) {
    // make action magic happen here... 


    $product = new WC_Product_Simple();
    $product->set_name('Sample product');
    $product->set_status('publish');
    $product->set_catalog_visibility('visible');
    $product->set_price(19.99);
    $product->set_regular_price(19.99);
    $product->set_sold_individually(true);
    $product->save();
}

// add the action 
add_action('woocommerce_process_product_meta', 'action_woocommerce_process_product_meta', 10, 2);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文