Php 基于单选按钮更改电子商务签出中的项目税率
在WooCommerce中,我使用插件[WooCommerce checkout addon][1],并在checkout页面添加了一个由单选按钮(多选)组成的附加字段。当客户在单选按钮字段中选择特定选项时,我想更改税率 根据更改特定付款方式税率的应答代码,我已尝试自定义代码:Php 基于单选按钮更改电子商务签出中的项目税率,php,jquery,ajax,wordpress,woocommerce,Php,Jquery,Ajax,Wordpress,Woocommerce,在WooCommerce中,我使用插件[WooCommerce checkout addon][1],并在checkout页面添加了一个由单选按钮(多选)组成的附加字段。当客户在单选按钮字段中选择特定选项时,我想更改税率 根据更改特定付款方式税率的应答代码,我已尝试自定义代码: // Change Tax add_action( 'woocommerce_before_calculate_totals', 'change_tax_class_based_on_radio_choice', 10,
// Change Tax
add_action( 'woocommerce_before_calculate_totals', 'change_tax_class_based_on_radio_choice', 10, 1 );
function change_tax_class_based_on_radio_choice( $cart ) {
$installation = WC()->session->get('wc_checkout_add_ons') ; // This code must be change
$value = 'pas-dinstallation' ; // the value of the radio button "woocommerce checkout addon"
$value2 = 'bacs' ; // value of payement methode
$payement_methode = WC()->session->get('chosen_payment_method') ;
//if ( $payement_methode !== $value2 ) //this one is ok for change tax if payement methode is bank transfert
//return;
if ( $installation !== $value ) // here i try to set the same condition with one of radio button "woocommerce checkout addon"
return;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Loop through cart items
foreach( $cart->get_cart() as $cart_item ){
// We set "Zero rate" tax class
$cart_item['data']->set_tax_class("Reduced rate");
}
}
add_action('wp_footer', 'option_trigger_update_checkout');
function option_trigger_update_checkout() {
if( is_checkout() && ! is_wc_endpoint_url() ) :
?>
<script type="text/javascript">
jQuery(function($){
$( 'form.checkout' ).on('change', 'input[type="radio"]', function() {
$(document.body).trigger('update_checkout');
});
});
</script>
<?php
endif;
}
没有得到很好的实施。我错过了什么
更新-添加(与以下答案相关): 由于我不需要支付方式限制,我已删除此相关行:
// Only for a specific defined payment method
if ( ! in_array( WC()->session->get('chosen_payment_method'), $payment_ids ) )
return;
那么,正如我所料,现在代码确实运行良好。
然而,使用“WooCommerce checkout addon”插件,当选择单选按钮时,我可以在管理中看到订单摘要和收到的电子邮件中,做出了什么选择
有了这个代码,税就改变了,但是我在管理员或电子邮件中没有关于客户无线电选择的信息
你有解决办法吗
其他事项: 我想在付款前移动单选按钮 但当我替换这个时:
// Display radio buttons field (optional)
add_action( 'woocommerce_after_order_notes', 'installation_custom_radio_field' );
function installation_custom_radio_field( $checkout ) {
借
它不再工作了。你能解释一下原因吗?如何使其工作?更新2:使其工作起来要复杂得多,因为它还需要Ajax和更多的附加代码 因此,以下内容将允许更改结帐页面上的购物车项目税务类别,具体取决于:
- (可选)所选的支付网关(此处使用空数组禁用)
- 所选单选按钮值(来自自定义单选按钮)
// Custom function that handle your settings
function change_tax_class_settings(){
return array(
'payment_ids' => array(), // (optional) Your targeted payment method Id(s) | Leave an empty array to disable.
'tax_class' => 'Reduced rate', // The desired tax rate
'field_id' => 'additonal_services', // the Field Id (from property name ="?????")
'field_value' => 'no-dinstallation', // The field targetted option key (value)
// The below lines are optional (used for the radio buttons field display)
'field_type' => 'radio', // Field type
'field_title' => __('Additional services', 'woocommerce'),
'field_default' => 'basic-installation', // The field targetted option key (value)
'field_options' => array(
'basic-installation' => __('Basic Installation', 'woocommerce'),
'premium-installation' => __('Premium Installation', 'woocommerce'),
'no-dinstallation' => __('No Installation', 'woocommerce'),
),
);
}
现在我们可以在需要的任何函数上加载该设置
然后,在付款方式签出部分之前显示单选按钮:
// Display radio buttons field (optional)
add_action( 'woocommerce_review_order_before_payment', 'installation_custom_radio_field' );
function installation_custom_radio_field() {
extract( change_tax_class_settings() ); // Load settings and convert them in variables
echo "<style>.$field_id-wrapper{padding:1em 1.41575em;background-color:#f5f5f5;margin-bottom:24px;}
.form-row.$field_id-$field_type span label{display:inline-block;margin:0 18px 0 6px;}
.$field_id-wrapper h3{font-weight:bold;}</style>";
echo '<div class="'.$field_id.'-wrapper">
<h3>'.$field_title.'</h3>';
// Get WC Session variable value
$value = WC()->session->get($field_id);
woocommerce_form_field( $field_id, array(
'type' => $field_type,
'label' => '',
'class' => array('form-row-wide ' . $field_id . '-' . $field_type ),
'options' => $field_options,
'default' => $field_default,
'required' => true,
), empty($value) ? WC()->checkout->get_value('_'.$field_id) : $value );
echo '</div>';
}
添加:将客户选择保存到订单中,并在前端订单、管理员和电子邮件通知中的任何位置显示:
// Save custom field as order meta data
add_action( 'woocommerce_checkout_create_order', 'save_additonal_services_as_order_meta' );
function save_additonal_services_as_order_meta( $order ) {
// Load settings and convert them in variables
extract( change_tax_class_settings() );
$choice = WC()->session->get($field_id);
if( ! empty( $choice ) ) {
$order->update_meta_data( '_'.$field_id, $choice );
}
}
// Display additonal services choice before payment method everywhere (orders and emails)
add_filter( 'woocommerce_get_order_item_totals', 'display_additonal_services_on_order_item_totals', 1000, 3 );
function display_additonal_services_on_order_item_totals( $total_rows, $order, $tax_display ){
// Load settings and convert them in variables
extract( change_tax_class_settings() );
$choice = $order->get_meta( '_'.$field_id ); // Get additonal services choice
if( ! empty($choice) ) {
$new_total_rows = [];
// Loop through order total rows
foreach( $total_rows as $key => $values ) {
// Inserting the pickp store under shipping method
if( $key === 'payment_method' ) {
$new_total_rows[$field_id] = array(
'label' => $field_title,
'value' => esc_html($field_options[$choice]),
);
}
$new_total_rows[$key] = $values;
}
return $new_total_rows;
}
return $total_rows;
}
// Display additonal services choice in Admin order pages
add_action( 'woocommerce_admin_order_data_after_billing_address', 'admin_order_display_additonal_services', 1000 );
function admin_order_display_additonal_services( $order ) {
// Load settings and convert them in variables
extract( change_tax_class_settings() );
$choice = $order->get_meta( '_'.$field_id ); // Get additonal services choice
if( ! empty($choice) ) {
// Display
echo '<p><strong>' . $field_title . '</strong>: ' . $field_options[$choice] . '</p>';
}
}
//将自定义字段另存为订单元数据
添加操作(“woocommerce\u checkout\u create\u order”、“save\u additional\u services\u as\u order\u meta”);
函数将附加服务另存为订单元($order){
//加载设置并将其转换为变量
提取(更改税类设置());
$choice=WC()->session->get($field\u id);
如果(!空($choice)){
$order->update_meta_数据('.'.$field_id,$choice);
}
}
//在任何地方(订单和电子邮件)付款方式之前显示附加服务选项
添加过滤器('woocommerce\u get\u order\u item\u totals','display\u Additional\u services\u on\u order\u item\u totals',1000,3);
功能显示\附加\服务\订单\项目\总计($total\行、$order、$tax\显示){
//加载设置并将其转换为变量
提取(更改税类设置());
$choice=$order->get_meta('.'.$field_id);//获取附加服务选项
如果(!空($choice)){
$new_total_rows=[];
//循环排序所有行
foreach($key=>$value的行总数){
//在shipping方法下插入Pick store
如果($key==“付款方法”){
$new_total_rows[$field_id]=数组(
“标签”=>$field\u标题,
'value'=>esc_html($field_options[$choice]),
);
}
$new_total_行[$key]=$value;
}
返回$new\u total\u行;
}
返回$total_行;
}
//在管理订单页面中显示附加服务选项
添加操作('woocommerce\u admin\u order\u data\u after\u billing\u address'、'admin\u order\u display\u Additional\u services',1000);
功能管理命令显示附加服务($order){
//加载设置并将其转换为变量
提取(更改税类设置());
$choice=$order->get_meta('.'.$field_id);//获取附加服务选项
如果(!空($choice)){
//展示
回显“。$field\u title.”:“。$field\u选项[$choice]”。;
}
}
所有代码都位于活动子主题(或主题)的functions.php文件中。测试和工作
在订单和电子邮件通知上显示选项(此处位于订单接收页面)
在管理单订单页面上:
好的,抱歉重复回答,谢谢你的代码。如果您有任何更新解决方案,那就太好了;)谢谢你的代码,它工作得几乎完美。只是一个小问题,请参见更新3。感谢您提供的信息,所有可翻译的文本字符串都类似于
\uuuu('text to translate','woocommerce')
,因此使用。现在这个线程为我关闭了。好的,它工作正常。只是一个插件“WooCommerce的签出管理器”的错误。当我删除这个插件时,所有的工作都很顺利,谢谢
// Display radio buttons field (optional)
add_action( 'woocommerce_review_order_before_payment', 'installation_custom_radio_field' );
function installation_custom_radio_field() {
extract( change_tax_class_settings() ); // Load settings and convert them in variables
echo "<style>.$field_id-wrapper{padding:1em 1.41575em;background-color:#f5f5f5;margin-bottom:24px;}
.form-row.$field_id-$field_type span label{display:inline-block;margin:0 18px 0 6px;}
.$field_id-wrapper h3{font-weight:bold;}</style>";
echo '<div class="'.$field_id.'-wrapper">
<h3>'.$field_title.'</h3>';
// Get WC Session variable value
$value = WC()->session->get($field_id);
woocommerce_form_field( $field_id, array(
'type' => $field_type,
'label' => '',
'class' => array('form-row-wide ' . $field_id . '-' . $field_type ),
'options' => $field_options,
'default' => $field_default,
'required' => true,
), empty($value) ? WC()->checkout->get_value('_'.$field_id) : $value );
echo '</div>';
}
// jQuery code (client side) - Ajax sender
add_action('wp_footer', 'installation_checkout_js_script');
function installation_checkout_js_script() {
if( is_checkout() && ! is_wc_endpoint_url() ) :
// Load settings and convert them in variables
extract( change_tax_class_settings() );
// jQuery Ajax code
?>
<script type="text/javascript">
jQuery( function($){
if (typeof wc_checkout_params === 'undefined')
return false;
var field = '#<?php echo $field_id; ?>_field input', fchecked = field+':checked';
// Function that sen the Ajax request
function sendAjaxRequest( value ) {
$.ajax({
type: 'POST',
url: wc_checkout_params.ajax_url,
data: {
'action': '<?php echo $field_id; ?>',
'value': value
},
success: function (result) {
$(document.body).trigger('update_checkout'); // Refresh checkout
}
});
}
// On ready (DOM loaded)
sendAjaxRequest( $(fchecked).val() );
// On change event
$(document.body).on( 'change', field, function(){
sendAjaxRequest( $(fchecked).val() );
});
// Refresh checkout on payment method change
$( 'form.checkout' ).on('change', 'input[name="payment_method"]', function() {
$(document.body).trigger('update_checkout'); // Refresh checkout
});
});
</script>
<?php
endif;
}
// The Wordpress Ajax PHP receiver
add_action( 'wp_ajax_additonal_services', 'get_additonal_services' );
add_action( 'wp_ajax_nopriv_additonal_services', 'get_additonal_services' );
function get_additonal_services() {
if ( isset($_POST['value']) ){
// Load settings and convert them in variables
extract( change_tax_class_settings() );
// Update session variable
WC()->session->set($field_id, esc_attr($_POST['value']));
// Send back the data to javascript (json encoded)
echo $_POST['value']; // optional
die();
}
}
// Change the tax class conditionally
add_action( 'woocommerce_before_calculate_totals', 'change_tax_class_conditionally', 1000 );
function change_tax_class_conditionally( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
extract( change_tax_class_settings() ); // Load settings and convert them in variables
// Only for a specific defined payment methods (can be disabled in the settings, with an empty array)
if ( ! empty($payment_ids) && ! in_array( WC()->session->get('chosen_payment_method'), $payment_ids ) )
return;
$choice = WC()->session->get($field_id);
// Loop through cart items
foreach( $cart->get_cart() as $cart_item ){
if( $choice === $field_value ) {
$cart_item['data']->set_tax_class($tax_class);
}
}
}
// Save custom field as order meta data
add_action( 'woocommerce_checkout_create_order', 'save_additonal_services_as_order_meta' );
function save_additonal_services_as_order_meta( $order ) {
// Load settings and convert them in variables
extract( change_tax_class_settings() );
$choice = WC()->session->get($field_id);
if( ! empty( $choice ) ) {
$order->update_meta_data( '_'.$field_id, $choice );
}
}
// Display additonal services choice before payment method everywhere (orders and emails)
add_filter( 'woocommerce_get_order_item_totals', 'display_additonal_services_on_order_item_totals', 1000, 3 );
function display_additonal_services_on_order_item_totals( $total_rows, $order, $tax_display ){
// Load settings and convert them in variables
extract( change_tax_class_settings() );
$choice = $order->get_meta( '_'.$field_id ); // Get additonal services choice
if( ! empty($choice) ) {
$new_total_rows = [];
// Loop through order total rows
foreach( $total_rows as $key => $values ) {
// Inserting the pickp store under shipping method
if( $key === 'payment_method' ) {
$new_total_rows[$field_id] = array(
'label' => $field_title,
'value' => esc_html($field_options[$choice]),
);
}
$new_total_rows[$key] = $values;
}
return $new_total_rows;
}
return $total_rows;
}
// Display additonal services choice in Admin order pages
add_action( 'woocommerce_admin_order_data_after_billing_address', 'admin_order_display_additonal_services', 1000 );
function admin_order_display_additonal_services( $order ) {
// Load settings and convert them in variables
extract( change_tax_class_settings() );
$choice = $order->get_meta( '_'.$field_id ); // Get additonal services choice
if( ! empty($choice) ) {
// Display
echo '<p><strong>' . $field_title . '</strong>: ' . $field_options[$choice] . '</p>';
}
}