2017-12-26 3 views
1

제품 가격이 사용자에 의해 정의 된 $order = wc_create_order();을 추가하려고합니다. 특정 제품이 이미 사용자가 입력 한 값으로 대체해야하는 기본 가격을 갖는 주문에 추가됩니다.WooCommerce에서 프로그래밍 방식으로 생성 된 주문에 사용자 정의 가격의 제품 추가

나는 woocommerce_before_calculate_totals 기능을 사용해 보았지만 운이 없었습니다. 장바구니에 추가되는 주문에 제품이 직접 추가 되어도 작동하지 않는다고 가정합니다.

은 또한

$order = wc_create_order(); 
$order->set_total($amount); //where the $amount is my custom price. 

처럼 set_total($value, $deprecated = '') 사용하여 시도했지만 순서 값은 변경되지 않습니다. 같은 것을 성취 할 수있는 다른 방법이 있습니까?

답변

0

주문을 생성 할 때 제품에 맞춤형 가격을 포함시키는 방법입니다.

당신이 새로 상자에 포장 위해 다른 모든 데이터와이 문제의 일부가 이미 다른 스레드에서 이전에 대한 답이 같은 항목 유형 (고객 주소와 같은, 세금 항목 ...)를 설정 한 것으로 가정

코드 :

테스트
## -- HERE Define everything related to your product -- ## 

$product_id = '41'; // a product ID or a variation ID 
$new_product_price = 120; // the new product price <==== <==== <==== 
$quantity = 1; // The line item quantity 

## - - - - - - - - - - - - - - - - - - - - - - - - - - ## 

// Get an instance of the WC_Product object 
$product = wc_get_product($product_id); 

// Change the product price 
$product->set_price($new_product_price); 

## - - - - - - - - - - - - - - - - - - - - - - - - - - ## 

// Create the order 
$order = wc_create_order(); 

// Add the product to the order 
$order->add_product($product, $quantity); 

## You will need to add customer data, tax line item … ## 

$order->calculate_totals(); // updating totals 

$order->save(); // Save the order data 

하고

작동
관련 문제