반응형
Woocommerce 3에서 프로그래밍 방식으로 주문에 요금 추가
카트 아이템이 다른 CMS에서 Import되었기 때문에 Woocommerce의 합계 'on-the-fly'를 만들고 있습니다.
현재 각 주문에 대해 커스텀 '요금'을 설정하고 주문을 '보류'로 표시하는 데 문제가 있습니다.
$order->set_date_created($creation_tsz);
$order->set_address( $address, 'billing' );
$order->set_address( $address, 'shipping' );
$order->set_currency('GBP');
$order->add_fee('Imported Total', $imported_total_here);
$order->set_fee();
$order->calculate_totals();
$order->update_status('on-hold');
어떤 트랙이든 감사할 것입니다.
그
WC_Abstract_Legacy_Order
메서드는 권장되지 않습니다.set_fee()
에 대한 메서드는 존재하지 않습니다.WC_Order
클래스(및 클래스 전용).
Woocommerce 3부터 프로그래밍 방식으로 주문에 요금을 추가하는 것은 좀 더 복잡합니다.수수료 이름, 세금 상태, 세금 등급(필요한 경우) 및 수수료 금액(세금 제외)으로 설정할 매개 변수가 있습니다.
또한 세금 설정에 따라 세금을 계산하려면 고객의 국가 코드를 최소로 포함하는 어레이를 설정해야 합니다(세금이 국가에 따라 적용되는 경우).
수수료 금액 변수 이름은 다음과 같습니다.$imported_total_fee
다음 코드 중 하나:
$order->set_date_created($creation_tsz);
$order->set_address( $address, 'billing' );
$order->set_address( $address, 'shipping' );
$order->set_currency('GBP');
## ------------- ADD FEE PROCESS ---------------- ##
// Get the customer country code
$country_code = $order->get_shipping_country();
// Set the array for tax calculations
$calculate_tax_for = array(
'country' => $country_code,
'state' => '',
'postcode' => '',
'city' => ''
);
// Get a new instance of the WC_Order_Item_Fee Object
$item_fee = new WC_Order_Item_Fee();
$item_fee->set_name( "Fee" ); // Generic fee name
$item_fee->set_amount( $imported_total_fee ); // Fee amount
$item_fee->set_tax_class( '' ); // default for ''
$item_fee->set_tax_status( 'taxable' ); // or 'none'
$item_fee->set_total( $imported_total_fee ); // Fee amount
// Calculating Fee taxes
$item_fee->calculate_taxes( $calculate_tax_for );
// Add Fee item to the order
$order->add_item( $item_fee );
## ----------------------------------------------- ##
$order->calculate_totals();
$order->update_status('on-hold');
$order->save();
테스트 완료, 완벽하게 동작.
언급URL : https://stackoverflow.com/questions/53603746/add-a-fee-to-an-order-programmatically-in-woocommerce-3
반응형
'IT' 카테고리의 다른 글
MVC 웹 API:요청된 리소스에 'Access-Control-Allow-Origin' 헤더가 없습니다. (0) | 2023.02.10 |
---|---|
WordPress 템플릿에서 현재 페이지가 WooCommerce 카트인지 체크아웃 페이지인지 어떻게 알 수 있습니까? (0) | 2023.02.10 |
php 정적 함수 (0) | 2023.02.06 |
실행 중인 스크립트의 상위 디렉토리 가져오기 (0) | 2023.02.06 |
mysql.sock을 찾을 수 없습니다. (0) | 2023.02.06 |