努力获取可变产品的产品属性
我目前在使用 WooCommerce 时遇到一个问题,即产品属性随机写入或未写入数据库。结果是在某些项目的订单中我看不到属性。因此,我考虑了一个中间解决方案,一旦客户点击“立即订购”按钮,就将购物车转入给我们自己的电子邮件中。 但这样做,我很难获得正确的属性。
我们销售的咖啡有 2 个属性:coffee_bag_size
和 coffee_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查您的 slug 属性,它可能是“pa_coffee-ground-for”
已测试并正常工作
Check out slug of you attribute it may be 'pa_coffee-ground-for'
Tested and working