Woocommerce Produkt nur in spezifischem Land kaufbar
Bei Änderung des Landes / bzw. initial bei Aufruf der Checkout page wird geprüft ob das richtige Land ausgewählt, ansonsten wird das Produkt entfernt und eine Meldung ausgegeben.
localised script für AJAX
wp_localize_script( 'theme', 'ajaxvar', array('ajaxurl' => admin_url( 'admin-ajax.php' ), 'security' => wp_create_nonce('ajax_request')));
Checkbox zum aktivieren der Funktion:
function removeItemCountry() {
$message = '';
foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item )
{
$_product = $cart_item['data'];
$meta = $_product->get_meta( 'shipping_country', true );
if($meta && !($_POST['chosen_country'] == 'AT')) {
WC()->cart->remove_cart_item( $cart_item_key);
$message .= '';
// wc_add_notice($message, 'notice');
}
}
$cart = sprintf( _n( '%s item', '%s items', WC()->cart->cart_contents_count, 'woocommerce' ), WC()->cart->cart_contents_count );
wp_send_json_success(array('message'=>$message, 'cart'=>$cart));
}
add_action( "wp_ajax_removeItemCountry", "removeItemCountry" );
add_action( "wp_ajax_nopriv_removeItemCountry", "removeItemCountry" );
// define the woocommerce_before_checkout_form callback
function action_woocommerce_before_checkout_form( ) {
echo '';
};
// add the action
add_action( 'woocommerce_before_checkout_form', 'action_woocommerce_before_checkout_form', 10, 1 );
add_action( 'woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3 );
add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 );
add_filter( 'woocommerce_available_variation', 'load_variation_settings_fields' );
function variation_settings_fields( $loop, $variation_data, $variation ) {
woocommerce_wp_checkbox(
array(
'id' => "shipping_country{$loop}",
'name' => "shipping_country[{$loop}]",
'value' => get_post_meta( $variation->ID, 'shipping_country', true ),
'label' => __( ' Verkauf nur in Österreich', 'woocommerce' ),
'desc_tip' => true,
'description' => __( 'Wenn aktiv, ist Artikel nur in Österreich zu kaufen, und wird automatisch an der Kassa-Übersicht entfernt, wenn ein anderes Land ausgewählt wird.', 'woocommerce' ),
'wrapper_class' => 'form-row form-row-full',
)
);
}
function save_variation_settings_fields( $variation_id, $loop ) {
$text_field = $_POST['shipping_country'][ $loop ];
if ( ! empty( $text_field ) ) {
update_post_meta( $variation_id, 'shipping_country', esc_attr( $text_field ));
}
}
function load_variation_settings_fields( $variation ) {
$variation['shipping_country'] = get_post_meta( $variation[ 'variation_id' ], 'shipping_country', true );
return $variation;
}
theme.js
// if($('body').hasClass('woocommerce-checkout')) {
// var chosen_country = $('#billing_country').val();
// ajax_country(chosen_country);
// }
// function ajax_country(chosen_country) {
// $.ajax({
// type: "post",
// dataType: "json",
// url: ajaxvar.ajaxurl,
// data: {action: 'removeItemCountry', chosen_country: chosen_country},
// }).success(function (response) {
// $('.sidebar-box:first-child .sidebar-box-content:first > strong').html(response.data.cart)
// $('.c-notices-wrapper').html(response.data.message);
// $('body').trigger('update_checkout');
// })
// }
// $(document).on('change', '#billing_country', '', function () {
// var chosen_country = $(this).val();
// ajax_country(chosen_country);
// });