You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WooCommerce获取未分配给变体的购物车商品属性值

解决WooCommerce未分配给变体的产品属性获取问题

这个问题我之前也碰到过,WooCommerce的变体产品对象默认只会加载和自身关联的属性,那些没分配给变体的父产品属性确实没法直接通过变体对象的get_attribute()拿到。下面给你两种可行的解决思路,根据你的实际情况选就行:

方法一:区分产品类型,从父产品获取属性

如果购物车中的产品是变体,我们先获取它的父产品对象,再从父产品里拿这两个属性值;如果是简单产品,直接从当前产品对象获取即可。

代码示例:

function progressive_discount_by_item_quantity( $cart ) {
    // 后台环境且非AJAX请求时直接返回,避免干扰
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    // 遍历购物车中的每个商品
    foreach ( $cart->get_cart() as $cart_item ) {
        $current_product = $cart_item['data'];
        $disc_quantity = '';
        $discount = '';

        // 判断当前商品是否是变体产品
        if ( $current_product->is_type( 'variation' ) ) {
            // 获取父产品对象
            $parent_product = wc_get_product( $current_product->get_parent_id() );
            // 从父产品获取属性
            $disc_quantity = $parent_product->get_attribute( 'DiscQuantity' );
            $discount = $parent_product->get_attribute( 'Discount' );
        } else {
            // 简单产品直接获取属性
            $disc_quantity = $current_product->get_attribute( 'DiscQuantity' );
            $discount = $current_product->get_attribute( 'Discount' );
        }

        // 这里写你的折扣逻辑,确保属性值不为空再执行
        if ( ! empty( $disc_quantity ) && ! empty( $discount ) ) {
            // 示例:当购买数量大于等于DiscQuantity时,按Discount比例打折
            $item_quantity = $cart_item['quantity'];
            if ( $item_quantity >= intval( $disc_quantity ) ) {
                $discount_amount = $cart_item['line_subtotal'] * ( floatval( $discount ) / 100 );
                $cart->add_fee( sprintf( '批量折扣(%s%%)', $discount ), -$discount_amount, true );
            }
        }
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'progressive_discount_by_item_quantity', 10, 1 );

方法二:直接获取完整属性数组(更稳妥)

如果get_attribute()偶尔还是取不到值(比如属性设置为隐藏或者是自定义非全局属性),可以直接获取产品的完整属性数组,从中提取目标属性值:

function progressive_discount_by_item_quantity( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    foreach ( $cart->get_cart() as $cart_item ) {
        $current_product = $cart_item['data'];
        // 确定要获取属性的产品对象:变体取父产品,否则取当前产品
        $target_product = $current_product->is_type( 'variation' ) ? wc_get_product( $current_product->get_parent_id() ) : $current_product;
        
        // 获取产品的所有属性数组
        $all_attributes = $target_product->get_attributes();
        
        // 提取目标属性值(注意属性名称大小写要和后台完全一致)
        $disc_quantity = isset( $all_attributes['DiscQuantity'] ) ? $all_attributes['DiscQuantity']['value'] : '';
        $discount = isset( $all_attributes['Discount'] ) ? $all_attributes['Discount']['value'] : '';

        // 后续折扣逻辑同上...
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'progressive_discount_by_item_quantity', 10, 1 );

关键注意点

  • 属性名称大小写敏感:后台设置的属性名称是DiscQuantity,代码里就不能写成discquantity,必须完全匹配。
  • 属性类型确认:如果你的“属性”其实是自定义字段(不是WooCommerce后台添加的产品属性),那得用get_post_meta( $target_product->get_id(), '你的字段键', true )来获取值。

内容的提问来源于stack exchange,提问作者Ali Almazawi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 10:03:17