doujiling4377 2018-04-09 04:09
浏览 27
已采纳

Wordpress Woocommerce更新Meta

ok, so I have an affiliate system. that I am embedding a cookie with. I have gotten the code to work to add a new field to the checkout page and then got it to force the person to enter the cookie's # if they type it into the new field. this is the working code.

   add_action('woocommerce_after_order_notes', 'affid_field');

function affid_field($checkout)
{
    if(isset($_COOKIE['ap_ref_tracking'])) {
        echo '<div style=" width: 335px;clear: both;
    margin-bottom: 75px;" id="affid_field">';
        woocommerce_form_field('affid_field', array(
            'type' => 'text',
            'class' => array(
                'my-field-class form-row-wide'
            ) ,
            'label' => __('Retype Number Shown Below') ,
            'placeholder' => $_COOKIE['ap_ref_tracking'] ,
            'required' => true,
        ) , $checkout->get_value('affid_field'));
        echo '</div>';
    }
}


add_action('woocommerce_checkout_process', 'customise_checkout_field_process');

function customise_checkout_field_process()
{
    if(isset($_COOKIE['ap_ref_tracking'])) {
    // if the field is set, if not then show an error message.
        $field = $_POST['affid_field'];
        $aff = $_COOKIE['ap_ref_tracking'];
        if (!$field or $field != $aff) 
        wc_add_notice(__('Please re-enter '.$aff.' as shown on the Shipping Tab.') , 'error');
    }
}

add_action('woocommerce_checkout_update_order_meta', 'customise_checkout_field_update_order_meta');

function customise_checkout_field_update_order_meta($order_id)
{
    $affid_field = $_POST['affid_field'];
    if ( ! empty( $affid_field ) ) {
        update_post_meta( $order_id, '_affid_field', sanitize_text_field( $affid_field ) );
    }
}

But I was thinking, Can't I just shorten all this up, by just injecting the cookie into the meta update. therefore removing the need for the extra field and the customer having to input the number. So I did this.

add_action('woocommerce_checkout_update_order_meta', 'customise_checkout_field_update_order_meta');

function customise_checkout_field_update_order_meta($order_id)
{
    if(isset($_COOKIE['ap_ref_tracking'])) {
            $aff = $_COOKIE['ap_ref_tracking'];
            update_post_meta( $order_id, '_affid_field', sanitize_text_field( $aff ) );
    )
}

which didn't work at all, it crashed the site. no clue what I am missing here, this should work, right? anyone spots my error and give me a hand it would be greatly appreciated.

  • 写回答

1条回答 默认 最新

  • douang1243 2018-04-09 04:54
    关注

    You are using ")" instead of "}" to close the if statement... Try this :-

     add_action('woocommerce_checkout_update_order_meta', 'customise_checkout_field_update_order_meta');
    
    function customise_checkout_field_update_order_meta($order_id)
    {
        if(isset($_COOKIE['ap_ref_tracking'])) {
                $aff = $_COOKIE['ap_ref_tracking'];
                update_post_meta( $order_id, '_affid_field', sanitize_text_field( $aff ) );
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型