Woocommerce create_product_cat 挂钩没有 thumbnail_id / 为产品类别附加图像信息
Woocommerce create_product_cat hook has no thumbnail_id / image information attached for product categories
我已经编写了代码来挂钩在 Woocommerce 上创建类别。此代码获取除“displayType”和“thumbnail_id”之外的所有信息。当连接到 'edited_product_cat' 时,这些字段会出现。
我的问题是是否有一个 way/code 也可以在创建时获得 thumbnail_id,或者是否有一个不同的钩子使这成为可能?
当前代码如下所示:
function loader() {
add_action('create_product_cat', [$this, 'onCategoryCreated'], 10, 2);
add_action('edited_product_cat', [$this, 'onCategoryCreated'], 10, 2);
}
function onCategoryCreated ($categoryId){
$cat = get_term_by( 'id', $categoryId, 'product_cat', 'ARRAY_A' );
$catMeta = get_term_meta( $cat["term_id"] );
$thumbnailId = get_term_meta( $cat["term_id"], 'thumbnail_id', true );
$imageUrl = wp_get_attachment_url( $thumbnailId );
error_log(json_encode($cat));
error_log(json_encode($catMeta));
error_log($thumbnailId);
error_log($imageUrl);
}
为创建打印出以下内容:
-> {"term_id":52,"name":"create","slug":"create","term_group":0,"term_taxonomy_id":52,"taxonomy":"product_cat","description":"create desc","parent":0,"count":0,"filter":"raw"}
-> {"order":["0"]}
->
->
以及以下更新:
-> {"term_id":35,"name":"update","slug":"update","term_group":0,"term_taxonomy_id":35,"taxonomy":"product_cat","description":"update desc","parent":0,"count":0,"filter":"raw"}
-> {"order":["0"],"display_type":[""],"thumbnail_id":["7"]}
-> 7
-> http://localhost:8888/myWebsite/wp-content/uploads/2021/11/6ac25e82-9d4c-3f59-ad83-a06f7966a0fd.jpg
使用created_product_cat代替create_product_cat
function loader() {
add_action('created_product_cat', [$this, 'onCategoryCreated'], 10, 2);
add_action('edited_product_cat', [$this, 'onCategoryCreated'], 10, 2);
}
function onCategoryCreated ($categoryId){
$cat = get_term_by( 'id', $categoryId, 'product_cat', 'ARRAY_A' );
$catMeta = get_term_meta( $cat["term_id"] );
$thumbnailId = get_term_meta( $cat["term_id"], 'thumbnail_id', true );
$imageUrl = wp_get_attachment_url( $thumbnailId );
error_log(json_encode($cat));
error_log(json_encode($catMeta));
error_log($thumbnailId);
error_log($imageUrl);
}
我已经编写了代码来挂钩在 Woocommerce 上创建类别。此代码获取除“displayType”和“thumbnail_id”之外的所有信息。当连接到 'edited_product_cat' 时,这些字段会出现。
我的问题是是否有一个 way/code 也可以在创建时获得 thumbnail_id,或者是否有一个不同的钩子使这成为可能?
当前代码如下所示:
function loader() {
add_action('create_product_cat', [$this, 'onCategoryCreated'], 10, 2);
add_action('edited_product_cat', [$this, 'onCategoryCreated'], 10, 2);
}
function onCategoryCreated ($categoryId){
$cat = get_term_by( 'id', $categoryId, 'product_cat', 'ARRAY_A' );
$catMeta = get_term_meta( $cat["term_id"] );
$thumbnailId = get_term_meta( $cat["term_id"], 'thumbnail_id', true );
$imageUrl = wp_get_attachment_url( $thumbnailId );
error_log(json_encode($cat));
error_log(json_encode($catMeta));
error_log($thumbnailId);
error_log($imageUrl);
}
为创建打印出以下内容:
-> {"term_id":52,"name":"create","slug":"create","term_group":0,"term_taxonomy_id":52,"taxonomy":"product_cat","description":"create desc","parent":0,"count":0,"filter":"raw"}
-> {"order":["0"]}
->
->
以及以下更新:
-> {"term_id":35,"name":"update","slug":"update","term_group":0,"term_taxonomy_id":35,"taxonomy":"product_cat","description":"update desc","parent":0,"count":0,"filter":"raw"}
-> {"order":["0"],"display_type":[""],"thumbnail_id":["7"]}
-> 7
-> http://localhost:8888/myWebsite/wp-content/uploads/2021/11/6ac25e82-9d4c-3f59-ad83-a06f7966a0fd.jpg
使用created_product_cat代替create_product_cat
function loader() {
add_action('created_product_cat', [$this, 'onCategoryCreated'], 10, 2);
add_action('edited_product_cat', [$this, 'onCategoryCreated'], 10, 2);
}
function onCategoryCreated ($categoryId){
$cat = get_term_by( 'id', $categoryId, 'product_cat', 'ARRAY_A' );
$catMeta = get_term_meta( $cat["term_id"] );
$thumbnailId = get_term_meta( $cat["term_id"], 'thumbnail_id', true );
$imageUrl = wp_get_attachment_url( $thumbnailId );
error_log(json_encode($cat));
error_log(json_encode($catMeta));
error_log($thumbnailId);
error_log($imageUrl);
}