努力获取可变产品的产品属性

发布于 2025-01-20 18:58:01 字数 1536 浏览 0 评论 0原文

我目前在使用 WooCommerce 时遇到一个问题,即产品属性随机写入或未写入数据库。结果是在某些项目的订单中我看不到属性。因此,我考虑了一个中间解决方案,一旦客户点击“立即订购”按钮,就将购物车转入给我们自己的电子邮件中。 但这样做,我很难获得正确的属性。

我们销售的咖啡有 2 个属性:coffee_bag_sizecoffee_ground_for。这两个属性都设置为变体,但我仅根据 coffee_bag_size 创建了 2 个变体(适用于 250 克和 500 克袋),价格不同,而 coffee_ground_for 可以是任何值。我只需要知道客户选择什么。

我在另一篇文章中读到,如果某个属性未在变体中使用,则必须从父级获取它,所以我这样做了:

 foreach ( WC()->cart->get_cart() as $cart_item ) 
   {
    $product = $cart_item['data'];
    
    $bagsize = $product->get_attribute('pa_coffee_bag_size')  ;
      
    // For Product Variation type
    if( $product->get_variation_id() > 0 )
     {
      $parent = wc_get_product($product->get_parent_id());
      $ground =  $parent->get_attribute('pa_coffee_ground_for')  ;          
     }
    // For other Product types
    else 
     {
      $ground =  $product->get_attribute('pa_coffee_ground_for')  ;  
     }    
   }

获取 coffee_bag_size 属性没有问题。 获取coffee_ground_for 是个问题。如果我从产品中获取它,它是空的,如果我从父级获取它,那么我会得到所有可能值的逗号分隔列表。但我只需要选择的值。我该怎么做?

我又尝试了一些事情......他们都给了我一个空字符串:

$ground = $product->get_meta('pa_coffee_ground_for',true);
$ground = $cart_item['variation']['pa_coffee_ground_for'];
      
$attributes = $product->get_attributes();
foreach ( $attributes as $attribute => $attribute_term ) 
 {
  $term = get_term_by('name','pa_coffee_ground_for', $attribute);    
  $ground = $term->term;      
 }

I'm currently having an issue with WooCommerce that randomly a product attribute is or is not written to the database. Result is that in the order for some items I can't see the attributes. Hence, I thought about having an intermediate solution by dumping the cart into an email to ourselves as soon as the customer hits the "order now" button.
But doing so, I'm struggling to get the attributes right.

We're selling coffee for which I have 2 attributes: coffee_bag_size and coffee_ground_for. Both attributes are set for variations, but I have created 2 only variations based on coffee_bag_size (for 250 grammes and 500 grammes bag) with different prices whereas the coffee_ground_for can be any value. I just have to know what the customer chooses.

I read in another post that in case an attribute is not used in variations, one has to get it from the parent, so I did:

 foreach ( WC()->cart->get_cart() as $cart_item ) 
   {
    $product = $cart_item['data'];
    
    $bagsize = $product->get_attribute('pa_coffee_bag_size')  ;
      
    // For Product Variation type
    if( $product->get_variation_id() > 0 )
     {
      $parent = wc_get_product($product->get_parent_id());
      $ground =  $parent->get_attribute('pa_coffee_ground_for')  ;          
     }
    // For other Product types
    else 
     {
      $ground =  $product->get_attribute('pa_coffee_ground_for')  ;  
     }    
   }

Getting the coffee_bag_size attribute is no problem.
Getting the coffee_ground_for is the problem. If I get it from the product it's empty, if I get it from the parent then I get a comma-separated list of all possible values. But I only need the value that was chosen. How do I do that?

I tried a few things more .... they all give me an empty string back:

$ground = $product->get_meta('pa_coffee_ground_for',true);
$ground = $cart_item['variation']['pa_coffee_ground_for'];
      
$attributes = $product->get_attributes();
foreach ( $attributes as $attribute => $attribute_term ) 
 {
  $term = get_term_by('name','pa_coffee_ground_for', $attribute);    
  $ground = $term->term;      
 }

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

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

发布评论

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

评论(1

悲凉≈ 2025-01-27 18:58:01

检查您的 slug 属性,它可能是“pa_coffee-ground-for”

  $product = $cart_item['data'];
  

  if( $product->is_type( 'variation' ) )
   {
     $product = wc_get_product($product->get_parent_id());          
   }
   $ground = $product->get_attribute( 'pa_coffee-ground-for' );

已测试并正常工作

Check out slug of you attribute it may be 'pa_coffee-ground-for'

  $product = $cart_item['data'];
  

  if( $product->is_type( 'variation' ) )
   {
     $product = wc_get_product($product->get_parent_id());          
   }
   $ground = $product->get_attribute( 'pa_coffee-ground-for' );

Tested and working

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