簡體   English   中英

在Woocommerce中添加自定義結帳字段時,結帳頁面為空白

[英]Blank checkout page when adding a custom checkout field in Woocommerce

在WooCommerce結帳頁面中,我添加了一個自定義字段,如果我使用woocommerce_after_checkout_billing_form鈎子或woocommerce_before_checkout_form鈎子,一切正常。

問題是我需要該字段位於“ Billing Details標題上方,但是當我使用woocommerce_checkout_before_customer_details鈎子時, 所有內容都會消失 (即使是側邊欄付款面板),也只有我的自定義字段標題可見。

我的代碼:

// Create Custom checkout Field
add_action('woocommerce_checkout_before_customer_details', 'create_custom_field');

function create_custom_field($checkout) {

    global $woocommerce;
    $cart = $woocommerce->cart->get_cart();
    foreach($cart as $key => $value)
    {               
        $bespoke = $woocommerce->cart->get_item_data($value);
        if (strpos($bespoke, 'yes') !== false) {

            echo '<div id="customise_checkout_field"><h3>' . __('Bespoke Details') . '</h3>';
            woocommerce_form_field('bespoke_field', array(
                'type' => 'textarea',
                'class' => array('my-field-class form-row-wide'),
                'label' => __('Tell us about your idea') ,
                'placeholder' => __('Please explain what you want as detailed as possible...') ,
                'required' => true,),
            $checkout->get_value('bespoke_field'));
            echo '</div>';
        }
    }
}

有什么想法嗎? 非常感謝您的指導。

該鈎子不存在$ checkout,因此未定義。 因此,它使空白頁(錯誤)。 但是您可以使用WC()->checkout作為替換。

從Woocommerce版本3.3開始,不推薦使用WC_cart方法get_item_data()並使用wc_get_formatted_cart_item_data()函數來替換它。

global $woocommerce$woocommerce->cart現在也被WC()->cart取代。

嘗試以下方法:

// Add a Custom checkout Field
add_action( 'woocommerce_checkout_before_customer_details', 'add_custom_checkout_field' );
function add_custom_checkout_field() {

    // Loop through cart items
    foreach( WC()->cart->get_cart() as $cart_item ) {
        $bespoke = wc_get_formatted_cart_item_data( $cart_item );

        if ( strpos($bespoke, 'yes') !== false ) {

            echo '<div id="customise_checkout_field">';

            echo '<h3>' . __('Bespoke Details') . '</h3>';

            woocommerce_form_field('bespoke_field', array(
                'type'        => 'textarea',
                'class'       => array('my-field-class form-row-wide'),
                'label'       => __( "Tell us about your idea", "woocommerce" ),
                'placeholder' => __( "Please explain what you want as detailed as possible...", "woocommerce" ),
                'required'    => true,
            ), WC()->checkout->get_value('bespoke_field') );

            echo '</div>';
        }
    }
}

代碼進入活動子主題(或活動主題)的function.php文件中。 經過測試和工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM