简体   繁体   English

Woocommerce:如何在functions.php 中使用get_term_by:查找产品类别ID

[英]Woocommerce: How to use get_term_by in functions.php : Lookup Product Category ID

When I run the standard Woocommerce Product Import当我运行标准的 Woocommerce 产品导入时

  • If a meta field in my import spreadsheet indicates that a Product Item must be hidden如果我的导入电子表格中的元字段指示必须隐藏产品项目
    • I set the catalog_visibility to hidden我将catalog_visibility 设置为隐藏
    • I need to lookup the ID for a special Product Category我需要查找特殊产品类别的 ID
      • and append that to that Item's Product Categories并将其附加到该项目的产品类别

My Category Name: Not for Web Sale我的类别名称:非网络销售

My Category Slug: not_for_web_sale我的类别 Slug: not_for_web_sale

So I am calling this in functions.php所以我在functions.php中调用它

if($cat_term_object = get_term_by('slug','not_for_web_sale','product_cat')) {
    $cat_term_id = $cat_term_object->term_id;
}

PROBLEM问题

I am not getting any return value我没有得到任何返回值

WHAT I HAVE FOUND我发现了什么

This post tells me that:这篇文章告诉我:

This is probably happening because the taxonomy you're trying to query is registered yet.这可能是因为您尝试查询的分类法尚未注册。 Eg.例如。 The WordPress environment is loaded when a theme's functions.php file loads, but many plugins/themes/core functions don't register taxonomies until later. WordPress 环境在主题的functions.php 文件加载时加载,但许多插件/主题/核心功能直到稍后才注册分类法。

And it suggests:它表明:

Try hooking into init with a really high priority number and running the get_term_by function.尝试使用非常高的优先级编号挂接到 init 并运行 get_term_by 函数。 Like so:像这样:

<?php
add_action( 'init', 'wpse27111_tester', 999 );
function wpse27111_tester()
{
    $term = get_term_by('slug', 'some-term', 'some-taxonomy');
    var_dump($term);
}

WHERE I AM STUCK我被困在哪里

Although I basically understand the explanation of the problem wrt the Woocommerce taxonomy ... ... I am not grasping how to apply that to my needs.虽然我基本上理解了 Woocommerce 分类法对问题的解释……我不明白如何将其应用于我的需求。

Advice or guidance would be most welcome.建议或指导将是最受欢迎的。

( Unfortunately my rep on that particular stackexchange is too low to comment ) (不幸的是,我在该特定 stackexchange 上的代表太低,无法发表评论)

Solved.解决了。

So what I needed to do in functions.php was所以我需要在functions.php中做的是

  • make my own custom function制作我自己的自定义函数
  • call the get_term_by() from inside my custom function从我的自定义函数内部调用 get_term_by()
  • add_action with a lower priority number = higher execution priority具有较低优先级编号的 add_action = 较高的执行优先级
add_action( 'init', 'get_not_for_sale_category_id', 10 );
function get_not_for_sale_category_id() {
   if ( $cat_term_object = get_term_by('slug','not_for_web_sale','product_cat') ) {
      $cat_term_id_value = $cat_term_object->term_id;
   } else {
      $cat_term_id_value = 1010101;
   }
   return $cat_term_id_value;

Then in my import function, I call like this然后在我的导入函数中,我这样调用

$cat_term_id = get_not_for_sale_category_id();

EDIT编辑

I've hooked it into a more appropriate place.我已经把它挂到了一个更合适的地方。

add_action( 'woocommerce_product_import_before_import', 'get_not_for_sale_category_id', 10 );

It's only called now on Import by an admin and not every page load by every user.它现在只在导入时由管理员调用,而不是每个用户加载的每个页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从functions.php中的Shortcode Hook中排除产品类别ID - WooCommerce - How to exclude Product Category ID from Shortcode Hook inside functions.php - WooCommerce 如何获取在functions.php中的函数中使用的woocommerce订单的订单ID? - How can I get the order id of a woocommerce order to use in a function in functions.php? 在主题的functions.php文件中获取订购的产品详细信息(ID,数量) - WooCommerce - Get ordered product details (ID, quantity) in functions.php file of theme - WooCommerce woocommerce在functions.php中获取当前产品名称 - woocommerce get current product name in functions.php 如何通过WooCommerce的functions.php获取特定产品的当前库存? - How do I get the current stock of a specific product with functions.php for WooCommerce? Wordpress - 如何在函数中获取当前类别名称。php? - Wordpress - how to get current category name inside functions.php? woocommerce获取产品类别ID - woocommerce get product category id 在WooCommerce中按产品获取类别ID - Get category id by product in WooCommerce WooCommerce产品图片可以在functions.php中覆盖吗? - Can WooCommerce product image be overridden in functions.php? 如何在 WordPress 函数中获取 tag_ID。php 并在 function 中使用它? - How to get tag_ID in WordPress functions.php and use it in function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM