随着Woocommerce,我使用WC Fields Factory插件在产品页面中捕获信息,然后将其添加到购物车。我需要访问输入的数据,以便在我的子主题函数文件中执行计算,并在购物车中显示正确的信息,但我无法找到如何操作。不过,我可以确认在产品页面上输入的数据正在正确添加到订单确认电子邮件/订单详细信息屏幕。
基于 “Woocommerce中的自定义购物车项目重量” 答案代码,这里是我完整的修改代码版本:
add_filter( 'woocommerce_add_cart_item_data', 'add_weight_custom_cart_item_data', 10, 3 );
function add_weight_custom_cart_item_data( $cart_item_data, $product_id, $variation_id ){
// For product variations handling
$product_id = $variation_id > 0 ? $variation_id : $product_id;
// Get an instance of the product object
$product = wc_get_product( $product_id );
// The default product weight and dimensions
$cart_item_data['weight']['default'] = $product->get_weight() ?: '0'; // Change this to use weight entered on product page (new field)
$cart_item_data['length']['default'] = $product->get_length() ?: '0'; // Change this to use length entered on product page (new field)
$cart_item_data['width']['default'] = $product->get_width() ?: '0'; // Change this to use width entered on product page (new field)
$cart_item_data['height']['default'] = $product->get_height() ?: '0'; // Change this to use height entered on product page (new field)
//___________________ Also add another field 'carton_qty' from the page to use in the below calculations instead of 2
## ====> HERE YOU CAN MAKE YOUR WEIGHT CALCULATIONS <==== ##
$new_weight_value = (($cart_item_data['length']['default'] * $cart_item_data['width']['default'] * $cart_item_data['height']['default']) / 5000);
$old_total_weight = ($cart_item_data['weight']['default'] * 2);
$new_total_weight = ($new_weight_value * 2);
// Set the new calculated weights
$cart_item_data['weight']['new'] = $new_weight_value;
$cart_item_data['weight']['totalold'] = $old_total_weight;
$cart_item_data['weight']['totalnew'] = $new_total_weight;
return $cart_item_data;
}
add_filter( 'woocommerce_get_item_data', 'display_cart_item_weight', 10, 2 );
function display_cart_item_weight( $item_data, $cart_item ) {
if ( isset($cart_item['weight']) ) {
// Display original weight
if ( isset($cart_item['weight']['default']) ) {
$item_data[] = array(
'key' => __('Carton Weight (std)', 'woocommerce'),
'value' => wc_format_weight( $cart_item['weight']['default'] ),
);
}
// Display calculated weight
if ( isset($cart_item['weight']['new']) ) {
$item_data[] = array(
'key' => __( 'Carton Weight (vol)', 'woocommerce' ),
'value' => wc_format_weight( $cart_item['weight']['new'] ),
);
}
// Display calculated original total weight
if ( isset($cart_item['weight']['totalold']) ) {
$item_data[] = array(
'key' => __( 'Line Weight (std)', 'woocommerce' ),
'value' => wc_format_weight( $cart_item['weight']['totalold'] ),
);
}
// Display calculated total weight
if ( isset($cart_item['weight']['totalnew']) ) {
$item_data[] = array(
'key' => __( 'Line Weight (vol)', 'woocommerce' ),
'value' => wc_format_weight( $cart_item['weight']['totalnew'] ),
);
if ($cart_item['weight']['totalold'] < $cart_item['weight']['totalnew']){
echo "<span style='color:#515151;font-size:1.6em;'><b> ⛟</b></span>";
}
}
}
return $item_data;
}
// Set the new weight in cart items
add_action( 'woocommerce_before_calculate_totals', 'set_custom_cart_item_weight', 25, 1 );
function set_custom_cart_item_weight( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
foreach( $cart->get_cart() as $cart_item ){
if ( isset($cart_item['weight']['new']) ) {
$cart_item['data']->set_weight(max ($cart_item['weight']['totalold'], $cart_item['weight']['totalnew']));
}
}
}
我只需要下面的“默认产品重量和尺寸”部分来使用用户提交的详细信息,而不是存储的产品详细信息,其余的功能粘贴在上面的上下文:
// The default product weight and dimensions
$cart_item_data['weight']['default'] = $product->get_weight() ?: '0'; // Change this to use weight entered on product page (new field)
$cart_item_data['length']['default'] = $product->get_length() ?: '0'; // Change this to use length entered on product page (new field)
$cart_item_data['width']['default'] = $product->get_width() ?: '0'; // Change this to use width entered on product page (new field)
$cart_item_data['height']['default'] = $product->get_height() ?: '0'; // Change this to use height entered on product page (new field)
//___________________ Also add another field 'carton_qty' from the page to use in the below calculations instead of 2
举个例子,重量显示在WC Field Factory的前端,字段名为“carton_weight”。我尝试了各种方法,包括获取订单 meta。我发现插件文件中的一些代码表明我应该使用$value=["wccpf_carton_weight_1"]
等,但我尝试过的都不起作用。
我正在使用WC Fields Factory版本3.0.3(当前版本)。
1条答案
按热度按时间9njqaruj1#
我已经为这个主题奋斗了几个小时...在阅读源代码后,我终于明白了WCFieldsFactory是如何工作的:https://github.com/wp-plugins/wc-fields-factory/blob/master/classes/product-form.php
如果您想在产品添加到购物车之前访问自定义字段值,这非常简单:只需调用
$_REQUEST['field_key']
,其中field_key可以在设置字段参数时在WCFieldsFactory用户界面上找到。〈=测试,它工作。如果你想访问AFTER产品添加到购物车(例如,在结帐期间),你可以通过
cart_item_data['wccpf_'.field_key]
〈=尚未测试,但应该根据源代码工作。希望这能有所帮助,即使3年后...