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 ) );
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行