如何使用WC_order_item_Product扩展我的自定义订单项



我想从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((。但是为什么新数据没有自动保存在项目中。

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

如果您使用的是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);
}

最新更新