亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

WooCommerce 再次訂購并自定義商品數據以進行價格計算

WooCommerce 再次訂購并自定義商品數據以進行價格計算

PHP
HUX布斯 2023-08-19 17:48:07
重新訂購時,我在嘗試添加/獲取自定義商品數據時遇到一些問題。首先讓我解釋一下:我主要使用 WooCommerce 作為發票制作者,因此我必須進行的自定義更改之一是在每個產品中添加自定義百分比折扣字段(您也可以在購物車頁面中編輯),所以我的問題是,當重新訂購時,購物車商品在那里,但百分比折扣不再影響價格,如果我嘗試更改百分比值,所有價格均為 0(產品價格和產品總計)。這是我正在使用的代碼:// Add a custom field before single add to cartadd_action('woocommerce_before_add_to_cart_button', 'custom_product_price_field', 5);function custom_product_price_field(){    echo '<div class="custom-text text">    <p>Descuento %:</p>    <input type="text" id="custom_price" name="custom_price" value="" placeholder="e.g. 10" title="Custom Text" class="custom_price text_custom text">    </div>';}// Get custom field value, calculate new item price, save it as custom cart item dataadd_filter('woocommerce_add_cart_item_data', 'add_custom_field_data', 20, 3);function add_custom_field_data($cart_item_data, $product_id, $variation_id){    $product_id = $variation_id > 0 ? $variation_id : $product_id;    if (!isset($_POST['custom_price'])) {        return $cart_item_data;    }    $custom_price = (float) sanitize_text_field($_POST['custom_price']);    if ($custom_price > 40) {        wc_add_notice(__('El descuento debe ser menor a 40%'), 'error');        return $cart_item_data;    }    $product = wc_get_product($product_id); // The WC_Product Object    $price = (float) $product->get_price();    $cart_item_data['base_price'] = $price;    $cart_item_data['new_price'] = $price * (100 - $custom_price) / 100;    if($custom_price > 0 || !empty($custom_price))        $cart_item_data['percentage'] = $custom_price . "%";    return $cart_item_data;}
查看完整描述

1 回答

?
一只名叫tom的貓

TA貢獻1906條經驗 獲得超3個贊

您沒有將所有必需的購物車商品數據保存為自定義訂單商品元數據,因此當使用“再次訂購”時,某些自定義購物車商品數據丟失,并且您的價格計算不會應用。

自 WooCommerce 3 起,woocommerce_add_order_item_meta?hook也被棄用。

注意:使用您提供的代碼,我無法更改購物車頁面中的百分比,因此可能缺少某些內容。

您需要在代碼中刪除/替換以下函數:

// Add order item meta.

add_action('woocommerce_add_order_item_meta', 'add_order_item_meta', 10, 3);

function add_order_item_meta($item_id, $cart_item, $cart_item_key)

{

? ? if (isset($cart_item['percentage'])) {

? ? ? ? wc_add_order_item_meta($item_id, 'percentage', $cart_item['percentage']);

? ? }

}

還有這個:


add_filter( 'woocommerce_order_again_cart_item_data', 'order_again_custom', 10, 3 );


function order_again_custom($cart_item_meta, $product, $order){

? ? //Create an array of all the missing custom field keys that needs to be added in cart item.

? ? $customfields = [

? ? ? ? ? ? ? ? ? ? 'Titulo1',?

? ? ? ? 'percentage',

? ? ? ? 'custom_price',

? ? ? ? 'price',

? ? ? ? 'new_price',

? ? ? ? ? ? ? ? ? ? ];

? ? global $woocommerce;

? ? remove_all_filters( 'woocommerce_add_to_cart_validation' );

? ? if ( ! array_key_exists( 'item_meta', $cart_item_meta ) || ! is_array( $cart_item_meta['item_meta'] ) )

? ? foreach ( $customfields as $key ){

? ? ? ? if(!empty($product[$key])){

? ? ? ? ? ? $cart_item_meta[$key] = $product[$key];

? ? ? ? }

? ? }

? ? return $cart_item_meta;

}

由以下人員組成:


// Save custom cart item data as custom order item meta data

add_action( 'woocommerce_checkout_create_order_line_item', 'add_order_item_meta', 10, 4 );

function add_order_item_meta( $item, $cart_item_key, $values, $order ) {

? ??

? ? // Save and display the "Percentage" (optional - if needed)

? ? if (isset($values['percentage'])) {

? ? ? ? $item->update_meta_data( 'Percentage', $values['percentage'] );?

? ? }


? ? // Save All custom cart item data as a hidden data array (important)

? ? if (isset($values['percentage']) && isset($values['base_price']) && isset($values['new_price']) ) {

? ? ? ? $custom_data = array(

? ? ? ? ? ? 'percentage' => $values['percentage'],

? ? ? ? ? ? 'base_price' => $values['base_price'],

? ? ? ? ? ? 'new_price'? => $values['new_price'],

? ? ? ? );

? ? ? ? $item->update_meta_data( '_custom_data', $custom_data ); // save

? ? }

}


// Add custom order item meta as custom cart item meta

add_filter( 'woocommerce_order_again_cart_item_data', 'custom_cart_item_data_for_order_again', 10, 3 );

function custom_cart_item_data_for_order_again( $cart_item_meta, $item, $order ) {

? ? // Get the hidden order item data

? ? $custom_data = (array) $item->get_meta( '_custom_data' );


? ? if( ! empty($custom_data) ) {

? ? ? ? $cart_item_meta = array_merge( $cart_item_meta, $custom_data );

? ? }

? ? return $cart_item_meta;

}

代碼位于活動子主題(或活動主題)的 function.php 文件中。經過測試并有效。


查看完整回答
反對 回復 2023-08-19
  • 1 回答
  • 0 關注
  • 182 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號