【问题标题】:Automatically assign products to a defined product category in WooCommerce自动将产品分配到 WooCommerce 中定义的产品类别
【发布时间】:2018-06-26 12:42:35
【问题描述】:

在 Woocommerce 中,如果产品具有特定的自定义字段值(使用高级自定义字段插件生成此字段),我会尝试自动将给定的产品类别分配给产品。

在我的functions.php 我有:

function auto_add_category ($product_id = 0) {

    if (!$product_id) return;
    $post_type = get_post_type($post_id);
    if ( "product" != $post_type ) return;

    $field = get_field("city");
    if($field == "Cassis"){
        $terms = get_the_terms( $post->ID, 'product_cat' );
        foreach ($terms as $term) {
            $product_cat_id = $term->term_id;
            if($product_cat_id != 93){
                wp_set_post_terms( $product_id, 93, 'product_cat', true );
            }
            break;
        }
    }
}
add_action('save_post','auto_add_category');

但这不起作用。请问有什么办法吗?

【问题讨论】:

    标签: php wordpress woocommerce categories product


    【解决方案1】:

    您好像在使用 ACF?

    get_field() 如果没有在循环中使用,则需要将帖子 ID 传递给它,因此您应该将该行设置为 $field = get_field("city",$product_id);

    另外,在get_the_terms() 中将$post->ID 替换为$product_id

    希望有帮助

    【讨论】:

      【解决方案2】:

      save_post 钩子有 3 个参数:

      • $post_id(帖子 ID)
      • $post WP_Post 对象)
      • $update(无论这是否是正在更新的现有帖子:truefalse)。

      ACF get_field() 函数无需在此处指定帖子 ID

      此外,为了使您的代码更紧凑、更轻便和更高效,您应该使用条件函数 has_term()term_exists() 而不是 get_the_terms() (+ foreach 循环)您网站上现有的产品类别……

      所以你应该这样尝试:

      // Only on WooCommerce Product edit pages (Admin)
      add_action( 'save_post', 'auto_add_product_category', 50, 3 );
      function auto_add_product_category( $post_id, $post, $update ) {
      
          if ( $post->post_type != 'product') return; // Only products
      
          // If this is an autosave, our form has not been submitted, so we don't want to do anything.
          if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
              return $post_id;
      
          // Check the user's permissions.
          if ( ! current_user_can( 'edit_product', $post_id ) )
              return $post_id;
      
          if ( ! ( $post_id && function_exists( 'get_field' ) ) ) 
              return; // Exit if ACF is not enabled (just to be sure)
      
          if ( 'Cassis' != get_field( 'city' ) )
              return; // Exit if ACF field "city" has 'Cassis' as value
      
          $term_id = 93; // <== Your targeted product category term ID
          $taxonomy = 'product_cat'; // The taxonomy for Product category
      
          // If the product has not "93" category id and if "93" category exist
          if ( ! has_term( $term_id, 'product_cat', $post_id ) && term_exists( $term_id, $taxonomy ) )
              wp_set_post_terms( $post_id, $term_id, $taxonomy, true ); // we set this product category
      }
      

      代码进入活动子主题(或活动主题)的function.php 文件中。

      经过测试并且有效。它应该也适合你。

      【讨论】:

        猜你喜欢
        • 2018-03-26
        • 2017-11-13
        • 2019-11-12
        • 1970-01-01
        • 1970-01-01
        • 2019-04-25
        • 1970-01-01
        • 2016-03-20
        • 1970-01-01
        相关资源
        最近更新 更多