wordpress 我如何使用WC_Order_Item_Product扩展我的定制订单项目

t3irkdon  于 2022-12-18  发布在  WordPress
关注(0)|答案(1)|浏览(203)

我想从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 extends WC_Order_Item_Product)时,新的额外数据不会保存在任何表中。下面是我用于向订单对象添加新RoomOrderItem的示例代码:

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()将 meta添加到这个项目中,但是为什么新数据没有自动保存到项目中呢?

polhcujo

polhcujo1#

我觉得你的问题是你没有保存数据。
如果你正在使用一个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);
}

相关问题