为了账号安全,请及时绑定邮箱和手机立即绑定

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条经验 获得超2个赞

您没有将所有必需的购物车商品数据保存为自定义订单商品元数据,因此当使用“再次订购”时,某些自定义购物车商品数据丢失,并且您的价格计算不会应用。

自 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 关注
  • 97 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信