【问题标题】:Blank checkout page when adding a custom checkout field in Woocommerce在 Woocommerce 中添加自定义结帐字段时出现空白结帐页面
【发布时间】:2019-04-01 12:34:24
【问题描述】:

在 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>';
        }
    }
}

有什么想法吗?非常感谢您的指导。

【问题讨论】:

    标签: php wordpress woocommerce cart checkout


    【解决方案1】:

    $checkout 对于这个钩子不存在,所以它没有被定义。所以它会产生一个空白页(错误)。但是你可以使用WC()-&gt;checkout 作为替代。

    WC_cart 方法 get_item_data() 已弃用,wc_get_formatted_cart_item_data() 函数自 Woocommerce 3.3 版起将其替换。

    此外,global $woocommerce$woocommerce-&gt;cart 现在也被简单地替换为 WC()-&gt;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 文件中。经过测试并且可以工作。

    【讨论】:

    • 惊人的@LoicTheAztec!这很完美。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 2016-07-29
    • 1970-01-01
    • 2023-04-10
    • 2022-01-03
    相关资源
    最近更新 更多