在 Woocommerce 中添加带有表单的自定义支付网关
Add a custom payment gateway with a form in Woocommerce
在 Woocommerce 中,我想要一个带有完整表格的自定义支付网关。该表格的答案显示在验证页面(我认为这是英文的感谢页面)和后台,按顺序显示。
我在 PHP 方面还不够好,无法创建这个,完全靠我自己。
但我学会了变得更好,试图理解代码是如何工作的。
所以我试图找到一些接近我的问题的东西
我找到了这个很好的答案:
Add a custom payment gateway with additional radio buttons in Woocommerce
哪个回答了我问题的 90%。
我发现自己如何添加其他类型的表单元素,如文本、复选框...
/**
* Output the "payment type" radio buttons fields in checkout.
*/
public function payment_fields(){
if ( $description = $this->get_description() ) {
echo wpautop( wptexturize( $description ) );
}
/**echo '<style>#transaction_type_field label.radio { display:inline-block; margin:0 .8em 0 .4em}</style>';**/
$option_keys = array_keys($this->options);
woocommerce_form_field( 'transaction_type-1', array(
'type' => 'checkbox',
'class' => array('transaction_type form-row-wide'),
'label' => __('Espèces', $this->domain),
), reset( $option_keys ) );
woocommerce_form_field( 'transaction_type-2', array(
'type' => 'checkbox',
'class' => array('transaction_type form-row-wide'),
'label' => __('Tickets restaurants', $this->domain),
), reset( $option_keys ) );
woocommerce_form_field( 'transaction_type-3', array(
'type' => 'checkbox',
'class' => array('transaction_type form-row-wide'),
'label' => __('Chèques vacances', $this->domain),
), reset( $option_keys ) );
woocommerce_form_field( 'transaction_type-4', array(
'type' => 'radio',
'class' => array('transaction_type form-row-wide'),
'label' => __('Payment Information - Test', $this->domain),
'options' => $this->options,
), reset( $option_keys ) );
}
但我不明白如何 "stock" 答案并显示出来。
我尝试使用第一个复选框并尝试了这个:
/**
* Save the chosen payment type as order meta data.
*
* @param object $order
* @param array $data
*/
public function save_order_payment_type_meta_data( $order, $data ) {
if ( $data['payment_method'] === $this->id && isset($_POST['transaction_type-1']) )
$order->update_meta_data('_transaction_type', esc_attr($_POST['transaction_type-1']) );
}
但它不起作用。
我想我错过了一些我不明白的东西,也许是关于这个的:
reset( $option_keys )
因此,如果您对我的问题有解决方案和解释,或者至少有线索,那将对我有很大帮助。
尝试以下替换代码函数(对于多个复选框):
/**
* Output the "payment type" fields in checkout.
*/
public function payment_fields(){
if ( $description = $this->get_description() ) {
echo wpautop( wptexturize( $description ) );
}
woocommerce_form_field( 'transaction_type-1', array(
'type' => 'checkbox',
'class' => array('transaction_type form-row-wide'),
'label' => __('Espèces', $this->domain),
), '' );
woocommerce_form_field( 'transaction_type-2', array(
'type' => 'checkbox',
'class' => array('transaction_type form-row-wide'),
'label' => __('Tickets restaurants', $this->domain),
), '' );
woocommerce_form_field( 'transaction_type-3', array(
'type' => 'checkbox',
'class' => array('transaction_type form-row-wide'),
'label' => __('Chèques vacances', $this->domain),
), '' );
$option_keys = array_keys($this->options);
woocommerce_form_field( 'transaction_type-4', array(
'type' => 'radio',
'class' => array('transaction_type form-row-wide'),
'label' => __('Payment Information - Test', $this->domain),
'options' => $this->options,
), reset( $option_keys ) );
}
/**
* Save the chosen payment type as order meta data.
*
* @param object $order
* @param array $data
*/
public function save_order_payment_type_meta_data( $order, $data ) {
if ( $data['payment_method'] === $this->id ) {
$meta_value = array(); // Initializing
if ( isset($_POST['transaction_type-1']) ) {
$meta_value[1] = __('Espèces', $this->domain);
}
if ( isset($_POST['transaction_type-2']) ) {
$meta_value[2] = __('Tickets restaurants', $this->domain);
}
if ( isset($_POST['transaction_type-3']) ) {
$meta_value[3] = __('Chèques vacances', $this->domain);
}
// Save transaction type (from fields 1 to 3) as an array
if( sizeof($meta_value) > 0 ) {
$order->update_meta_data('_transaction_type', $meta_value );
}
// Save transaction type test (from fields 4) as a string
if ( isset($_POST['transaction_type-4']) ) {
$order->update_meta_data('_transaction_type_test', esc_attr($_POST['transaction_type-4']) );
}
}
}
/**
* Display the chosen payment type on order totals table
*
* @param array $total_rows
* @param WC_Order $order
* @param bool $tax_display
* @return array
*/
public function display_transaction_type_order_item_totals( $total_rows, $order, $tax_display ){
if( is_a( $order, 'WC_Order' ) && $order->get_meta('_transaction_type') ) {
$new_rows = []; // Initializing
$options = $this->options;
// Loop through order total lines
foreach( $total_rows as $total_key => $total_values ) {
$new_rows[$total_key] = $total_values;
if( $total_key === 'payment_method' ) {
// Get transaction type array
if( $meta_data = $order->get_meta('_transaction_type') ) {
$new_rows['payment_type'] = [
'label' => __("Transaction type", $this->domain) . ':',
'value' => implode(',', $meta_data),
];
}
}
}
$total_rows = $new_rows;
}
return $total_rows;
}
它应该更好用……
相关主题:Add a custom payment gateway with additional radio buttons in Woocommerce
在 Woocommerce 中,我想要一个带有完整表格的自定义支付网关。该表格的答案显示在验证页面(我认为这是英文的感谢页面)和后台,按顺序显示。
我在 PHP 方面还不够好,无法创建这个,完全靠我自己。 但我学会了变得更好,试图理解代码是如何工作的。
所以我试图找到一些接近我的问题的东西
我找到了这个很好的答案:
Add a custom payment gateway with additional radio buttons in Woocommerce
哪个回答了我问题的 90%。
我发现自己如何添加其他类型的表单元素,如文本、复选框...
/**
* Output the "payment type" radio buttons fields in checkout.
*/
public function payment_fields(){
if ( $description = $this->get_description() ) {
echo wpautop( wptexturize( $description ) );
}
/**echo '<style>#transaction_type_field label.radio { display:inline-block; margin:0 .8em 0 .4em}</style>';**/
$option_keys = array_keys($this->options);
woocommerce_form_field( 'transaction_type-1', array(
'type' => 'checkbox',
'class' => array('transaction_type form-row-wide'),
'label' => __('Espèces', $this->domain),
), reset( $option_keys ) );
woocommerce_form_field( 'transaction_type-2', array(
'type' => 'checkbox',
'class' => array('transaction_type form-row-wide'),
'label' => __('Tickets restaurants', $this->domain),
), reset( $option_keys ) );
woocommerce_form_field( 'transaction_type-3', array(
'type' => 'checkbox',
'class' => array('transaction_type form-row-wide'),
'label' => __('Chèques vacances', $this->domain),
), reset( $option_keys ) );
woocommerce_form_field( 'transaction_type-4', array(
'type' => 'radio',
'class' => array('transaction_type form-row-wide'),
'label' => __('Payment Information - Test', $this->domain),
'options' => $this->options,
), reset( $option_keys ) );
}
但我不明白如何 "stock" 答案并显示出来。 我尝试使用第一个复选框并尝试了这个:
/**
* Save the chosen payment type as order meta data.
*
* @param object $order
* @param array $data
*/
public function save_order_payment_type_meta_data( $order, $data ) {
if ( $data['payment_method'] === $this->id && isset($_POST['transaction_type-1']) )
$order->update_meta_data('_transaction_type', esc_attr($_POST['transaction_type-1']) );
}
但它不起作用。 我想我错过了一些我不明白的东西,也许是关于这个的:
reset( $option_keys )
因此,如果您对我的问题有解决方案和解释,或者至少有线索,那将对我有很大帮助。
尝试以下替换代码函数(对于多个复选框):
/**
* Output the "payment type" fields in checkout.
*/
public function payment_fields(){
if ( $description = $this->get_description() ) {
echo wpautop( wptexturize( $description ) );
}
woocommerce_form_field( 'transaction_type-1', array(
'type' => 'checkbox',
'class' => array('transaction_type form-row-wide'),
'label' => __('Espèces', $this->domain),
), '' );
woocommerce_form_field( 'transaction_type-2', array(
'type' => 'checkbox',
'class' => array('transaction_type form-row-wide'),
'label' => __('Tickets restaurants', $this->domain),
), '' );
woocommerce_form_field( 'transaction_type-3', array(
'type' => 'checkbox',
'class' => array('transaction_type form-row-wide'),
'label' => __('Chèques vacances', $this->domain),
), '' );
$option_keys = array_keys($this->options);
woocommerce_form_field( 'transaction_type-4', array(
'type' => 'radio',
'class' => array('transaction_type form-row-wide'),
'label' => __('Payment Information - Test', $this->domain),
'options' => $this->options,
), reset( $option_keys ) );
}
/**
* Save the chosen payment type as order meta data.
*
* @param object $order
* @param array $data
*/
public function save_order_payment_type_meta_data( $order, $data ) {
if ( $data['payment_method'] === $this->id ) {
$meta_value = array(); // Initializing
if ( isset($_POST['transaction_type-1']) ) {
$meta_value[1] = __('Espèces', $this->domain);
}
if ( isset($_POST['transaction_type-2']) ) {
$meta_value[2] = __('Tickets restaurants', $this->domain);
}
if ( isset($_POST['transaction_type-3']) ) {
$meta_value[3] = __('Chèques vacances', $this->domain);
}
// Save transaction type (from fields 1 to 3) as an array
if( sizeof($meta_value) > 0 ) {
$order->update_meta_data('_transaction_type', $meta_value );
}
// Save transaction type test (from fields 4) as a string
if ( isset($_POST['transaction_type-4']) ) {
$order->update_meta_data('_transaction_type_test', esc_attr($_POST['transaction_type-4']) );
}
}
}
/**
* Display the chosen payment type on order totals table
*
* @param array $total_rows
* @param WC_Order $order
* @param bool $tax_display
* @return array
*/
public function display_transaction_type_order_item_totals( $total_rows, $order, $tax_display ){
if( is_a( $order, 'WC_Order' ) && $order->get_meta('_transaction_type') ) {
$new_rows = []; // Initializing
$options = $this->options;
// Loop through order total lines
foreach( $total_rows as $total_key => $total_values ) {
$new_rows[$total_key] = $total_values;
if( $total_key === 'payment_method' ) {
// Get transaction type array
if( $meta_data = $order->get_meta('_transaction_type') ) {
$new_rows['payment_type'] = [
'label' => __("Transaction type", $this->domain) . ':',
'value' => implode(',', $meta_data),
];
}
}
}
$total_rows = $new_rows;
}
return $total_rows;
}
它应该更好用……
相关主题:Add a custom payment gateway with additional radio buttons in Woocommerce