【问题标题】:how i extends my custom order item using WC_Order_Item_Product我如何使用 WC_Order_Item_Product 扩展我的自定义订单项
【发布时间】:2022-12-05 05:01:27
【问题描述】:

我想从 WC_Order_Item_Product 扩展我的自定义实体。我将我的自定义额外数据添加到此类。

class RoomOrderItem extends OrderItemLegacy {

    protected $extra_data = array(
        'product_id' => 0,
        'variation_id' => 0,
        'quantity' => 1,
        'tax_class'=> '', 
        'subtotal' => 0,
        'subtotal_tax' => 0,
        'total' => 0,
        'total_tax' => 0,
        'taxes' => [
            'subtotal' => [], 
            'total' => [], 
        ],  
        'period' => '', 
        'extra' => '', 
        'manager' => [], 
        'entity' => __CLASS__,
    );  

    public function set_period(DateTime $day){
        $this->set_prop('period',$day->format('Y-m-d'));
        return $this;
    }   

    public function set_extra(int $extra){
        $this->set_prop('extra',$extra);
        return $this;
    }   

    public function set_manager(array $manager){
        $this->set_prop('manager',$manager);
        return $this;
    }   

} 

但是当添加我的自定义订单项(RoomOrderItem 扩展 WC_Order_Item_Product)时,新的额外数据不会保存在任何表中。这是我将新的 RoomOrdeItem 添加到订单对象的示例代码:

    public function add(RoomCartItem $room_cart_item){
        $this->set_props([
            'quantity'     => $room_cart_item->get_prop('quantity'),
            'variation'    => $room_cart_item->get_prop('variation'),
            'subtotal'     => $room_cart_item->get_prop('line_subtotal'),
            'total'        => $room_cart_item->get_prop('line_total'),
            'name'         => $room_cart_item->get_hotel()->get_name(),
            'product_id'   => $room_cart_item->get_prop('product_id'),
            'variation_id' => $room_cart_item->get_prop('variation_id'),
            '_period'      => $room_cart_item->get_period(),
            '_manager'     => $room_cart_item->get_manager(),
            '_extra'       => $room_cart_item->get_extra(),
            '_entity'      => __CLASS__,
        ]);
        return $this->get_order()->add_item($this);
    }

此外,我知道可以使用 $this->add_meta_data() 将元数据添加到此项。但是为什么新数据没有自动保存在item中。

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    我认为你的问题是你没有保存数据。

    如果您正在使用 orderitem 对象类,您应该能够使用 save() 函数。

    public function add(RoomCartItem $room_cart_item){
        $this->set_props([
            'quantity'     => $room_cart_item->get_prop('quantity'),
            'variation'    => $room_cart_item->get_prop('variation'),
            'subtotal'     => $room_cart_item->get_prop('line_subtotal'),
            'total'        => $room_cart_item->get_prop('line_total'),
            'name'         => $room_cart_item->get_hotel()->get_name(),
            'product_id'   => $room_cart_item->get_prop('product_id'),
            'variation_id' => $room_cart_item->get_prop('variation_id'),
            '_period'      => $room_cart_item->get_period(),
            '_manager'     => $room_cart_item->get_manager(),
            '_extra'       => $room_cart_item->get_extra(),
            '_entity'      => __CLASS__,
        ]);
        $this->save(); //save the updated props
        return $this->get_order()->add_item($this);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-15
      • 1970-01-01
      • 1970-01-01
      • 2013-12-22
      • 1970-01-01
      相关资源
      最近更新 更多