Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Last active May 31, 2019 06:52
Show Gist options
  • Save wpmudev-sls/d5decb8dab5933a45bcbc384bf449ef8 to your computer and use it in GitHub Desktop.
Save wpmudev-sls/d5decb8dab5933a45bcbc384bf449ef8 to your computer and use it in GitHub Desktop.
[MarketPress] - Remove privacy field and add custom fields
<?php
/**
* Plugin Name: [MarketPress] - Remove privacy field and add custom fields
* Plugin URI: https://premium.wpmudev.org/
* Author: WPMUDEV
* Author URI: https://premium.wpmudev.org
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
add_action( 'plugins_loaded', 'wpmudev_mp_privacy_add_custom_field', 100 );
function wpmudev_mp_privacy_add_custom_field() {
if ( defined( 'MP_VERSION' ) && class_exists( 'Marketpress' ) ) {
function wpmudev_mp_field_name( $name, $prefix = null ) {
if ( !is_null( $prefix ) ) {
return $prefix . '[' . $name . ']';
}
return $name;
}
// add custom fields
add_filter( 'mp_checkout/address_fields_array', 'wpmudev_mp_add_purchase_order_field', 10, 2 );
// remove privacy checkbox
add_filter( 'mp_checkout/address_fields', 'wpmudev_mp_hide_privacy_field_checkout_form', 10, 3 );
add_filter( 'mp_checkout/display', 'wpmudev_mp_remove_privacy_field_checkout_form');
add_filter( 'mp_checkout/required_fields', 'wpmudev_mp_remove_policy_from_checkout_required_fields' );
add_filter( 'mp_order/get_address', 'wpmudev_mp_admin_add_purchase_order_and_note_fields', 10, 3 );
/**
* Add purchase order field
* @param array $address_fields list address fields
* @param string $type address type
* @return string custom html
*/
function wpmudev_mp_add_purchase_order_field( $address_fields, $type ){
// Purchase Order Number
$address_fields[] = array(
'type' => 'text',
'label' => __( 'Enter Purchase Order Number', 'mp' ),
'name' => wpmudev_mp_field_name( 'purchase_order', $type ),
'value' => mp_get_user_address_part( 'purchase_order', $type ),
'atts' => array(
'class' => 'mp_form_input',
),
);
// Optional Note
// $address_fields[] = array(
// 'type' => 'textarea',
// 'label' => __( 'Optional Note', 'mp' ),
// 'name' => wpmudev_mp_field_name( 'optional_note', $type ),
// 'value' => mp_get_user_address_part( 'optional_note', $type ),
// 'atts' => array(
// 'class' => 'mp_form_input',
// ),
// );
return $address_fields;
}
/**
* Add Optional Note field and remove privacy field
* @param string $html default address html
* @param string $type type address
* @param boolean $value_only value only if false lable included
* @return string custom html
*/
function wpmudev_mp_hide_privacy_field_checkout_form( $html, $type, $value_only ){
// only for billing (checkout page)
if( 'billing' === $type ){
$field = array(
// 'type' => 'textarea',
'label' => $value_only ? false : __( 'Optional Note', 'mp' ),
'name' => wpmudev_mp_field_name( 'optional_note', $type ),
'value' => mp_get_user_address_part( 'optional_note', $type ),
'atts' => array(
'class' => 'mp_form_input',
),
'value_only' => $value_only
);
if ( $label = mp_arr_get_value( 'label', $field ) ) {
$required = ( mp_arr_get_value( 'validation->required', $field ) ) ? ' <span class="mp_field_required">*</span>' : '';
$html .= '
<label class="mp_form_label">' . mp_arr_get_value( 'label', $field, '' ) . $required . '</label>';
}
$atts = '';
// Convert validation arg into attributes
foreach ( (array) mp_arr_get_value( 'validation', $field, array() ) as $key => $val ) {
if ( is_bool( $val ) ) {
$val = ( $val ) ? 'true' : 'false';
}
$val = mp_quote_it( $val );
$atts .= " data-rule-{$key}={$val}";
}
// Get attributes
$attributes = (array) mp_arr_get_value( 'atts', $field, array() );
// Add ID attribute
if ( false === mp_arr_get_value( 'id', $attributes ) ) {
$attributes[ 'id' ] = 'mp-checkout-field-' . uniqid( true );
}
// Convert atts arg into attributes
foreach ( $attributes as $key => $val ) {
$val = mp_quote_it( $val );
$atts .= " {$key}={$val}";
}
if ( mp_arr_get_value( 'value_only', $field ) ) {
$html .= mp_arr_get_value( 'value', $field, '' );
}else{
$html .= sprintf( '<textarea name="%s" %s>%s</textarea>', mp_arr_get_value( 'name', $field ), $atts, esc_textarea( mp_arr_get_value( 'value', $field ) ) );
}
$html .= '<style>
#mp-checkout-column-billing-info .mp_checkout_field.mp_privacy_policy{
display: none!important;
}
</style>
<script>
(function($){
$(function(){
var _mpel_privacy_policy = $("#mp-checkout-column-billing-info").find(".mp_privacy_policy");
if( _mpel_privacy_policy.length ){
_mpel_privacy_policy.remove();
}
})
}(window.jQuery));
</script>
';
}
return $html;
}
/**
* Remove policy checkbox from dom of checkout page
* @param string $html default html
* @return string custom html
*/
function wpmudev_mp_remove_privacy_field_checkout_form( $html ){
$html = str_replace('<input type="checkbox" class="mp_form_checkbox" data-rule-required="true" name="billing[policy]" value="1" autocomplete="off">', '', $html);
return $html;
}
/**
* Remove policy field from list required fields
* @param array $required_fields list required fields
* @return array custom required fields
*/
function wpmudev_mp_remove_policy_from_checkout_required_fields($required_fields){
if( $found = array_search('policy', $required_fields) ){
unset( $required_fields[ $found ] );
}
return $required_fields;
}
/**
* Add purchase order and not fields in admin
* @param strig $html default address html
* @param string $type address type
* @param object $order MP_Order
* @return string custom html
*/
function wpmudev_mp_admin_add_purchase_order_and_note_fields( $html, $type, $order ){
if( 'billing' === $type ){
$prefix = 'mp[' . $type . '_info]';
$html .= '<table class="form-table">
<tr>
<th scope="row">' . __( 'Purchase Order Number', 'mp' ) . '</th>
<td><input type="text" name="' . $prefix . '[purchase_order]" value="' . $order->get_meta( "mp_{$type}_info->purchase_order", '' ) . '"></td>
</tr>
<tr>
<tr>
<th scope="row">' . __( 'Optional Note', 'mp' ) . '</th>
<td><textarea name="' . $prefix . '[optional_note]">' . $order->get_meta( "mp_{$type}_info->optional_note", '' ) . '</textarea></td>
</tr>
</tr>
</table>';
}
return $html;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment