![](/img/trans.png)
[英]Set a custom add to cart price via the URL (GET) in WooCommerce
[英]WooCommerce custom Ajax add to cart: How to set a custom price?
我正在嘗試在結帳時從 on_click 事件中添加產品。 該產品的價格是使用外部 API 動態計算的,因此它在 woocommerce 本身中注冊為價格為 0。 我正在嘗試將此產品添加到購物車(工作),然后將該產品的價格設置為我從 API 收到的價格(不工作)。 以下是相關代碼:
AJAX 點擊調用:
jQuery('#theftdaminsurance').on('click', function(){
jQuery.ajax({
type: "POST",
url: "/wp-admin/admin-ajax.php",
data: {
action: 'add_product_to_cart_checkout',
// add your parameters here
price: <?php echo $year_dam_price; ?>,
productid: ins_product_id
},
success: function (output) {
document.getElementById("loaderdiv").style.display = "none";
jQuery(document.body).trigger("update_checkout");
}
});
});
我在這里檢查了所有內容,並且正確撥打了電話,並傳遞了所有正確的信息。 這里是 function 被調用:
add_action('wp_ajax_add_product_to_cart_checkout', 'add_product_to_cart_checkout');
// register the ajax action for unauthenticated users
add_action('wp_ajax_nopriv_add_product_to_cart_checkout', 'add_product_to_cart_checkout');
function add_product_to_cart_checkout() {
$product_id = $_REQUEST['productid'];
$price = floatval($_REQUEST['price']);
$ebike_ids = array(17386,17385,17382,17378,17375,17372,17370,17369,17364,16132,16130,4561,4550,3490,2376);
$found = false;
//check if there is something in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
//Delete all preexisting insurance products and find qty of ebikes
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product_idebike = $cart_item['product_id'];
if ( ($cart_item['product_id'] == "16600") || ($cart_item['product_id'] == "16653") || ($cart_item['product_id'] == "16654") || ($cart_item['product_id'] == "16655") ||($cart_item['product_id'] == "16659") || ($cart_item['product_id'] == "16660")) {
WC()->cart->remove_cart_item( $cart_item_key );
}
//get ebike quantity
for ($x = 0; $x <= 14; $x++) {
if ($product_idebike == $ebike_ids[$x]) {
$quantity = $cart_item['quantity'];
break;
}
}
}
// if command is not to remove add relevant products
if ($price != "-1") {
WC()->cart->add_to_cart( $product_id, $quantity );
foreach( WC()->cart->get_cart() as $cart_item ){
$id = $cart_item['product_id'];
if ($product_id == $id) {
$cart_item['data']->set_price( $price );
}
}
}
}
}
在這里,我基本上仔細調試了一切,一切都正常運行,但 set_price function。 我無法將產品的價格更新為 AJAX 調用中傳遞的價格。 結帳更新后價格保持為 0,之前也是如此。 我是否以錯誤的方式調用或使用此 function?
非常感謝你的幫助!
看起來你讓事情變得比在你的 Ajax 接收器功能上要復雜得多......
請注意,您可以在使用WC_Cart
add_to_cart()
方法時添加自定義購物車項目數據,因此您將添加您的保險價格作為自定義購物車項目數據,然后使用它來設置價格。
這是您重新訪問的 Ajax PHP 接收器 function:
add_action('wp_ajax_add_product_to_cart_checkout', 'add_product_to_cart_checkout');
add_action('wp_ajax_nopriv_add_product_to_cart_checkout', 'add_product_to_cart_checkout');
function add_product_to_cart_checkout() {
if ( isset($_POST['productid']) && isset($_POST['price']) && ! WC()->cart->is_empty() ) {
$product_id = intval($_POST['productid']);
$price = floatval($_POST['price']);
$ebike_ids = array(17386,17385,17382,17378,17375,17372,17370,17369,17364,16132,16130,4561,4550,3490,2376);
$insurance_ids = array(16600,16653,16654,16655,16659,16660);
$found = false;
$ebike_quantity = 0;
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
// Delete all preexisting insurance products
if ( in_array( $cart_item['product_id'], $insurance_ids ) ) {
WC()->cart->remove_cart_item( $cart_item_key );
}
// Get ebikes total quantity
if ( in_array( $cart_item['product_id'], $ebike_ids ) ) {
$ebike_quantity += $cart_item['quantity'];
}
}
// If command is not to remove add insurance product with price as custom cart item data
if ( $price >= 0 && $cart_item['insurance_price'] > 0 ) {
WC()->cart->add_to_cart( $product_id, $ebike_quantity, 0, array(), array( 'insurance_price' => $price ) );
}
die();
}
}
然后設置您將使用的價格:
// Set insurance price from custom cart item data
add_action( 'woocommerce_before_calculate_totals', 'set_insurance_price' );
function set_insurance_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
foreach ( $cart->get_cart() as $cart_item ) {
if( isset($cart_item['insurance_price']) && $cart_item['insurance_price'] > 0 ) {
$cart_item['data']->set_price( $cart_item['insurance_price'] );
}
}
}
現在它應該可以工作了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.