php - WooCommerce Checkout url hook - Change condition based on product category -
wondering if me customize code. change applied condition in code:
<?php /* plugin name: modify klarna checkout url plugin uri: http://krokedil.com description: change checkout url klarna checkout if user isn't specific country version: 1.0 author: krokedil author uri: http://krokedil.com */ add_filter( 'woocommerce_get_checkout_url', 'krokedil_change_checkout_url', 30 ); function krokedil_change_checkout_url( $url ) { $allowed_countries = array('no'); $customer_country = wc()->customer->get_default_country(); if( !in_array( $customer_country , $allowed_countries ) ) { $url = wc_get_page_permalink( 'checkout' ); } return $url; }
is possible instead, products belongs category in woocommerce, have custom checkout url?
thanks
yes it's possible, making changes:
add_filter( 'woocommerce_get_checkout_url', 'krokedil_change_checkout_url', 30 ); function krokedil_change_checkout_url( $checkout_url ) { // define special category here , custom url $my_cat = 'cat name'; // or multiple categories // $my_cat = array('cat name1', 'cat name2'); $my_url = 'http://my_custom_url.com/checkout/'; // custom url $bool_cat = false; if ( sizeof( wc()->cart->cart_contents) > 0 ) { foreach ( wc()->cart->get_cart() $cart_item ) { $item = $cart_item['data']; // => updated mistake in line if(!empty($item) && !$bool_cat && has_term( $my_cat, 'product_cat', $item->id ) ){ $bool_cat = true; } } if ( $bool_cat ) { $checkout_url = $my_url; } // optional else { $checkout_url = wc()->cart->get_checkout_url(); } } return $checkout_url; }
this code goes in plugin file or on function.php file of active child theme or theme
references:
Comments
Post a Comment