php - 尝试在 woocommerce 结帐页面的总计部分之后放置自定义字段

标签 php wordpress woocommerce

此代码可以正常工作,但没有将自定义字段放置在我想要的位置。在 add_action 中,我想使用 woocommerce_review_order_after_order_total 作为位置属性将其放置在页面上。我从这个视觉指南 https://businessbloomer.com/woocommerce-visual-hook-guide-checkout-page/ 中得到了钩子(Hook)名称然而这个钩子(Hook)(在下面代码的注释中看到)破坏了我的代码并给出了以下 php 错误。

Fatal error: Uncaught Error: Call to a member function get_value() on string in /Users/anderskitson/Local Sites/river-cafe/app/public/wp-content/themes/salient-child/functions.php on line 96

希望有人能帮我一把。谢谢

/**
* Add custom field to the checkout page
*/

add_action('woocommerce_after_order_notes', 'custom_checkout_field');

/* add_action('woocommerce_review_order_after_order_total', 'custom_checkout_field');*/
/* ^ This is the code that is breaking */ 

function custom_checkout_field($checkout)
{

    echo '<div id="custom_checkout_field"><h2>' . __('New Heading') . '</h2>';

    woocommerce_form_field(
        'custom_field_name', 
        array(
            'type' => 'text',
            'class' => array(
                'my-field-class form-row-wide'
            ) ,
            'label' => __('Custom Additional Field') ,
            'placeholder' => __('New Custom Field') ,
        ) ,
        $checkout->get_value('custom_field_name')
    );
    echo '</div>';
}

最佳答案

删除 $checkout->get_value('custom_field_name') 以便它只是:

woocommerce_form_field(
    'custom_field_name', 
    array(
        'type' => 'text',
        'class' => array(
            'my-field-class form-row-wide'
        ) ,
        'label' => __('Custom Additional Field') ,
        'placeholder' => __('New Custom Field') ,
    )
);

任何用户值都应自动填充,否则将只是空白。

关于php - 尝试在 woocommerce 结帐页面的总计部分之后放置自定义字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58718665/

相关文章:

php - 如何使用 ffmpeg-php 从 .3gp 和 .mp4 读取信息?

php - PHP "Get"数据库未更新

php - 使用 WPML 插件更改所有语言的 "return to shop"URL

php - 删除 WooCommerce Checkout 中一些基于虚拟产品的 Hook 功能

php - 如何计算数组中连续的重复值?

php - 如何在 PHP 中将链接转换为字符串

php - 无法获取 Bootstrap 布局上列的内容以与列左侧对齐

wordpress - 如何在 Woocommerce 中编辑 “ship to different address” 表单中的字段?

css - 如何在博客文章之间添加图片

php - 如何在 functions.php 中使用 WooCommerce 复制产品类