In functions.php

// Velden opslaan
add_action( 'woocommerce_process_product_meta', 'wwbmz_custom_restrict_fields_save' );
function wwbmz_custom_restrict_fields_save( $post_id ){
		// Select
			$woocommerce_select = $_POST['_restriction-type'];
			if( !empty( $woocommerce_select ) )
				update_post_meta( $post_id, '_restriction-type', esc_attr( $woocommerce_select ) );
		// Textarea
			$woocommerce_textarea = $_POST['_regions'];
			if( !empty( $woocommerce_textarea ) )
				update_post_meta( $post_id, '_regions', esc_html( $woocommerce_textarea ) );

 }
 
 function wbmz_get_country() {
	  
	  if( get_option('wpcbr_debug_mode') && is_admin()){
		  $cookie_country = isset($_COOKIE["country"]) ? sanitize_text_field($_COOKIE["country"]) : '';
		  //echo $cookie_country;
		  //if( !empty( $cookie_country ) )return $this->user_country = $_COOKIE["country"];
	  }
	  
	  $force_geoloaction = get_field('forceer_geolocatie','options');
	  if ( !$force_geoloaction ) {			
		  global $woocommerce;
		  if( isset($woocommerce->customer) ){
			  $shipping_country = $woocommerce->customer->get_shipping_country();
			  $cookie_country = !empty($_COOKIE["country"]) ? sanitize_text_field($_COOKIE["country"]) : $shipping_country;
			  if( isset($cookie_country) ){
				  return $cookie_country;
			  }
		  }
	  }	
	  
	  if( empty( $cookie_country )  ) {
		  $geoloc = WC_Geolocation::geolocate_ip();
		  $cookie_country = !empty($_COOKIE["country"]) ? sanitize_text_field($_COOKIE["country"]) : $geoloc['country'];
		  //$this->user_country = $cookie_country;
		  return $cookie_country;
	  }
	  
	  return $cookie_country;
  }

Je moet velden maken om de mogelijkheid te geven om geolocatie te forceren en het bericht dat verschijnt. (ACF veldgroep – JSON)

[
    {
        "key": "group_63f887753cce3",
        "title": "Country restrictions",
        "fields": [
            {
                "key": "field_63f887769b78b",
                "label": "Forceer geolocatie?",
                "name": "forceer_geolocatie",
                "aria-label": "",
                "type": "true_false",
                "instructions": "",
                "required": 0,
                "conditional_logic": 0,
                "wrapper": {
                    "width": "",
                    "class": "",
                    "id": ""
                },
                "message": "",
                "default_value": 0,
                "ui": 0,
                "acfe_field_group_condition": 0,
                "ui_on_text": "",
                "ui_off_text": "",
                "style": ""
            },
            {
                "key": "field_63f8878a9b78c",
                "label": "Restrictie tekst",
                "name": "restrictie_tekst",
                "aria-label": "",
                "type": "textarea",
                "instructions": "",
                "required": 0,
                "conditional_logic": 0,
                "wrapper": {
                    "width": "",
                    "class": "",
                    "id": ""
                },
                "default_value": "",
                "acfe_textarea_code": 0,
                "maxlength": "",
                "rows": "",
                "placeholder": "",
                "new_lines": "",
                "acfe_field_group_condition": 0
            }
        ],
        "location": [
            [
                {
                    "param": "options_page",
                    "operator": "==",
                    "value": "acf-options-thema-opties"
                }
            ]
        ],
        "menu_order": 0,
        "position": "normal",
        "style": "default",
        "label_placement": "left",
        "instruction_placement": "label",
        "hide_on_screen": "",
        "active": true,
        "description": "",
        "show_in_rest": 0,
        "acfe_autosync": [
            "json"
        ],
        "acfe_form": 0,
        "acfe_display_title": "",
        "acfe_meta": "",
        "acfe_note": ""
    }
]

Je kan de onderstaande code altijd gebruiken waar je wilt

$restriction_type = get_post_meta( get_the_ID(), '_restriction-type', true );
 $regions = get_post_meta( get_the_ID(), '_regions', true );
 $regions_array = (explode(',', str_replace('/\s+/','', $regions)));
 $base_country = wbmz_get_country();
 $restrictie_tekst = get_field('restrictie_tekst','options');
if( $restriction_type == 'allow' && in_array($base_country , $regions_array) || $restriction_type == 'deny' && !in_array($base_country , $regions_array) || $restriction_type == '' || $regions == '' ) {
echo "Toegang";
else {
echo "verboden";
}

loop/add.to-cart.php

<?php
/**
 * Loop Add to Cart
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/loop/add-to-cart.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see         https://docs.woocommerce.com/document/template-structure/
 * @package     WooCommerce\Templates
 * @version     7.0.1
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

global $product;
$restriction_type = get_post_meta( get_the_ID(), '_restriction-type', true );
 $regions = get_post_meta( get_the_ID(), '_regions', true );
 $regions_array = (explode(',', str_replace('/\s+/','', $regions)));
 $base_country = wbmz_get_country();
 $restrictie_tekst = get_field('restrictie_tekst','options');
if( $restriction_type == 'allow' && in_array($base_country , $regions_array) || $restriction_type == 'deny' && !in_array($base_country , $regions_array) || $restriction_type == '' || $regions == '' ) {
 if( $product->get_price() == 0 ) { ?>
    <div class="d-block" style="margin-right: 30px">
    <button type="button" class="btn btn-primary m-1 w-100 vrginfo">
      Vraag prijs
     </button>
    </div>
    <div class="modal fade" id="product<?php echo $product->get_ID(); ?>" tabindex="-1" aria-labelledby="product<?php echo $product->get_ID(); ?>" aria-hidden="true">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <h1 class="modal-title fs-5" id="product<?php echo $product->get_ID(); ?>">Vraag prijs</h1>
            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
          </div>
          <div class="modal-body">
            <?php echo do_shortcode('[gravityform id="3" title="false" field_values="product='. $product->get_title().'"]');?>
          </div>
        </div>
      </div>
    </div>
    <?php
   //echo '<a href="/contact/" class="btn btn-primary m-1 d-block">Vraag prijs</a>';
   echo '<style> a[data-product_id="'.$product->get_ID().'"], .modal-backdrop { display: none!important;} .modal.show { background: #000;}</style>';
   } else {
echo apply_filters(
	'woocommerce_loop_add_to_cart_link', // WPCS: XSS ok.
	sprintf(
		'<a href="%s" data-quantity="%s" class="%s" %s>%s</a>',
		esc_url( $product->add_to_cart_url() ),
		esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ),
		esc_attr( isset( $args['class'] ) ? $args['class'] : 'btn btn-secondary' ),
		isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '',
		esc_html( $product->add_to_cart_text() )
	),
	$product,
	$args
);
};
} else {
   //echo $restrictie_tekst;
   if(is_product()):
      echo $restrictie_tekst;
   else:
      echo "<div class='px-1 pb-1 text-center'>{$restrictie_tekst}</div>";
   endif;
 };
Geef een reactie 0