【问题标题】:How to get woocommerce product all details when publish a new product?发布新产品时如何获取 woocommerce 产品的所有详细信息?
【发布时间】:2021-10-02 13:34:12
【问题描述】:

我想在我们发布新产品时获取 woocommerce 产品发布数据。我尝试了一些钩子,但它仅在我们更新产品时才有效。 有谁知道添加新产品时如何获取产品发布数据?

这是我的代码。

add_action( 'save_post_product',  'add_product_tocrm_through_woocommerce', 10, 3);
function add_product_tocrm_through_woocommerce($post_id, $post, $update){

    if ( wp_is_post_revision( $post_id ) ) {
        return;
    }

    if (!$product = wc_get_product( $post )) {
        return;
    }

    $post_type = get_post_type( $post_id );
    $post_status = get_post_status();

    if ( $post_type == 'product' && $post_status == 'publish') {

        $product = wc_get_product($post_id);
        $pNameWoo =   $product->name;
        $price = $product->get_price();
        $desc =  $product->get_description();

        $wooProduct[0]['name'] = $pNameWoo;
        $wooProduct[0]['price'] = $price;
        $wooProduct[0]['description'] = $desc;
        save_woo_product_tocrm($wooProduct);
    }

}

【问题讨论】:

标签: php wordpress woocommerce wordpress-hook


【解决方案1】:

您正在使用“save_post_product”挂钩,因此您无需在触发 CRM 保存功能之前检查很多内容。

另外,您需要使用“->get_name()”而不是“->name”,并且无法以您编码的方式获取长描述,而是使用 $post->post_content 代替:

add_action( 'save_post_product', 'add_product_tocrm_through_woocommerce', 9999, 3 );

function add_product_tocrm_through_woocommerce( $post_id, $post, $update ){
    if ( ! $update ) { // ONLY TARGET NEW PRODUCTS
        $product = wc_get_product( $post_id );
        $pNameWoo = $product->get_name();
        $price = $product->get_price();
        $desc = $post->post_content;
        $wooProduct[0]['name'] = $pNameWoo;
        $wooProduct[0]['price'] = $price;
        $wooProduct[0]['description'] = $desc;
        save_woo_product_tocrm($wooProduct);
    }
}

【讨论】:

  • 感谢您的建议。我发布产品时没有获得产品发布数据。有没有办法在添加新产品时提取产品帖子的字段数据?
  • 我已经尝试过你的代码,但我仍然没有得到产品的数据。
  • 嗯,它应该工作。确定 save_woo_product_tocrm() 编码正确?
  • 是的,它已正确编码和测试。我正在使用 RESTapi 将该数据发送到 SuiteCRM,并且该功能正常工作。我们可以访问 $post_id 和 $post 数据吗?我认为当我们发布 product 产品 id 并发布数据时不会生成。在发布产品后,我们就有了这些数据。这就是为什么我要问无论如何要获取所有字段数据?或者我可能错了,所以请为此提出任何解决方案? off topic :- 我从您网站上的博客中学到了很多东西。谢谢:-)
猜你喜欢
  • 2015-01-03
  • 1970-01-01
  • 2020-08-18
  • 2018-01-23
  • 1970-01-01
  • 1970-01-01
  • 2012-05-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多