0

woocommerce_cart_calculate_fees我想在 Order 对象上访问 FEE 的值,它实际上不是费用,而是折扣,但我们在挂钩上使用负值。

我尝试了很多方法来访问它,但它不起作用,起初我使用get_total_discount()但它给了我空白值,我检查了订单对象,因为它是添加在“fee_lines”上的。

我已经尝试过:在 Woocommerce 3 中获取订单费用项目详细信息, 但它不一样,对我不起作用它没有进入循环实际上这是我的代码:

add_action( 'woocommerce_new_order', 'insert_distributor_discount',  1, 1  );
function insert_distributor_discount($order_id){
    global $wpdb;
    
    $order  = wc_get_order( $order_id );

    //Get Discount Amount
    
    // Iterating through order fee items ONLY
    foreach( $order->get_items('fee') as $item_id => $item_fee ){

        // The fee name
        $fee_name = $item_fee->get_name();

        // The fee total amount
        $fee_total = $item_fee->get_total();

        // The fee total tax amount
        $fee_total_tax = $item_fee->get_total_tax();
        
        error_log(print_r('$fee_name '.$fee_name, TRUE)); 
        error_log(print_r('$fee_total '.$fee_total, TRUE)); 
        error_log(print_r('$fee_total_tax '.$fee_total_tax, TRUE)); 
    }
    
    error_log(print_r('$fee_name '.$fee_name, TRUE)); 
    error_log(print_r('$fee_total '.$fee_total, TRUE)); 
    error_log(print_r('$fee_total_tax '.$fee_total_tax, TRUE));

    //INSERT QUERY HERE
    
}

有没有办法可以访问它?


这是示例订单对象,我想要得到的是Distributor Discountunder的值fee_lines

{"id":0,"parent_id":0,"status":"","currency":"","version":"","prices_include_tax":false,"date_created":null,"date_modified":null,"discount_total":0,"discount_tax":0,"shipping_total":0,"shipping_tax":0,"cart_tax":0,"total":0,"total_tax":0,"customer_id":0,"order_key":"","billing":{"first_name":"","last_name":"","company":"","address_1":"","address_2":"","city":"","state":"","postcode":"","country":"","email":"","phone":""},"shipping":{"first_name":"","last_name":"","company":"","address_1":"","address_2":"","city":"","state":"","postcode":"","country":""},"payment_method":"","payment_method_title":"","transaction_id":"","customer_ip_address":"","customer_user_agent":"","created_via":"","customer_note":"","date_completed":null,"date_paid":null,"cart_hash":"","number":"0","meta_data":[{"key":"is_vat_exempt","value":"no"},{"key":"stockist_id","value":"102"}],"line_items":{"new:line_items0":{"legacy_values":{"key":"9461cce28ebe3e76fb4b931c35a169b0","product_id":481,"variation_id":0,"variation":[],"quantity":1,"data_hash":"b5c1d5ca8bae6d4896cf1807cdf763f0","line_tax_data":{"subtotal":[],"total":[]},"line_subtotal":1000,"line_subtotal_tax":0,"line_total":1000,"line_tax":0,"data":{}},"legacy_cart_item_key":"9461cce28ebe3e76fb4b931c35a169b0"}},"tax_lines":[],"shipping_lines":{"new:shipping_lines0":{"legacy_package_key":0}},"fee_lines":{"new:fee_lines0":{"legacy_fee":{"id":"distributor-discount","name":"Distributor Discount","tax_class":"","taxable":true,"amount":"-500","total":-500,"tax_data":[],"tax":0},"legacy_fee_key":"distributor-discount"}},"coupon_lines":[]}

我也尝试使用这个 json 循环遍历它并获取该值,但我无法使其工作。

这是我尝试过的

$order_arr = json_decode($order, true);

foreach ($order_arr['fee_lines'][0]['legacy_fee'] as $key => $value) {
    error_log(print_r($key . ' - '. $value, TRUE)); 
}
4

0 回答 0