【发布时间】:2019-07-25 15:53:01
【问题描述】:
在 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 ) );
}
但我不明白如何“储存”答案并显示它。 我尝试了第一个复选框,我尝试了这个:
/**
* 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 )
因此,如果您对我的问题有解决方案和解释,或者至少有一个线索,那将对我有很大帮助。
【问题讨论】:
-
您正在制造混乱,例如,人们不会同时选择“Espèces”和“Tickets Restaurants”。所有字段都需要在一个字段中分组为“选项”……
-
事实上,就我而言,问题在于人们可以选择“Espèces”和“tickets Restaurants”和“chèque vacances”。他们可以用这三样东西混合支付。否则我会让单选按钮,这对我来说会容易得多。但我会失去一个学习的机会。
标签: php wordpress woocommerce payment-gateway custom-fields