How can I set country on product page? - woocommerce

With the code below, I can select the country on the product page. However, this selection does not cause any change in the payment page. How can I provide the country selection on the product page on the checkout page? I would like to know your advice on this matter.
global $woocommerce;
woocommerce_form_field( 'billing_country', array( 'type' => 'country' ) );

Related

Hide "out of stock" products on home page only

My site has a few product categories, for example Tables, Chairs, Sofas and a Recently Sold category. I would like to:
hide all out-of-stock products from the homepage (shop page)
but still show them on the assigned category archives
Example:
a sold table has been assigned the categories "Tables" and "Recently Sold". The product should not be shown on the homepage, but will be shown on the category page "Tables" and the category page "Recently Sold".
a sold table has been assigned the category "Recently Sold". The product should only be shown on the category page "Recently Sold".
I had found a snippet somewhere years ago which worked perfectly. With the change of theme, it got lost. I have tried so many plugins and snippets but I just can't find a solution anymore.
Hope you guys can help :-) Thanks so much!
You can either use the plugin Hide Out of Stock Products. This will let you adjust the settings to hide them.
If you wanted to do it programmatically, use the code below.
add_filter( 'woocommerce_product_query_meta_query', 'hide_out_of_stock_products', 10, 2 );
function hide_out_of_stock_products( $meta_query, $query ) {
if ( ! is_admin() && is_front_page() ) {
$meta_query[] = array(
'key' => '_stock_status',
'value' => 'outofstock',
'compare' => 'NOT IN'
);
}
return $meta_query;
}
This code will hide the out of stock products from the home page, but they will be seen on other pages.

Showing/Hiding custom terms and conditions text for a specific product in the checkout page in woocommerce

1st problem: basically we have a trial product on our website that needs a specific terms and condition text to be visible during the checkout process + the default terms and condition text for other products and it should be hidden if the product is not included.
2nd problem: if the customer is buying the trial product only, the default terms and condition text should be hidden and the custom text should show up.
default text
default text + custom text
that's the code we're currently using. i need help with getting it to show and depends on the products that's being checked out, if Trial product only, only the custom text should show, if trial product + other products, both text should show, if there is no trial product, only default text should show. any help would be appreciated, thanks in advance.
add_action( 'woocommerce_review_order_before_submit', 'bbloomer_add_checkout_privacy_policy', 9 );
function bbloomer_add_checkout_privacy_policy() {
woocommerce_form_field( 'privacy_policy', array(
'type' => 'checkbox',
'class' => array('form-row privacy'),
'label_class' => array('woocommerce-form__label woocommerce-form__label-for-checkbox checkbox'),
'input_class' => array('woocommerce-form__input woocommerce-form__input-checkbox input-checkbox'),
'required' => true,
'label' => 'By placing an order with us, you agree to our full terms and conditions & privacy policy , which explain that you will be charged '.WC()->cart->get_total(). ' ( '.WC()->cart->get_cart_shipping_total().' Shipping and handling ). Your order will be shipped in 3-5 business days via USPS). To cancel your order, please call our Customer Service toll free number at (111) 11-1111 or email us at support#domain.com. You will see a charge from customdom on your bank statement. Our Customer Service department reserves the right to offer promo codes for varied price points.',
));
}
// Show notice if customer does not tick
add_action( 'woocommerce_checkout_process', 'bbloomer_not_approved_privacy' );
function bbloomer_not_approved_privacy() {
if ( ! (int) isset( $_POST['privacy_policy'] ) ) {
wc_add_notice( __( 'Please acknowledge the Privacy Policy' ), 'error' );
}
}

WooCommerce: Site-wide cookie filter

We need set site-wide cookie filter by brands.
WooComerce have build-in Brands taxonomy.
At the start landing page user will be select one from several brands. And after, user views products on site only by selected brand. If user will visit brand url (www.example.com/brands/brand-second), then need change filter to another brand, and shows products only by last selected brand. Others products not allowed to shows (another brand or without brand).
What any ideas for realize this?
I think you can try hold the visible brand in a cookie, and use woocommerce_product_query or pre_get_posts filters to filter according to that cookie value.
Each time the user visits that brand URL, change the cookie value.
I found solution for my question!
function filter_products_by_brand( $q ) {
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'brand',
'field' => 'slug',
'terms' => array( 'dobro' ), // Don't display products in the clothing category on the shop page.
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
add_action( 'pre_get_posts', 'filter_products_by_brand' );

show specific country in shipping and all country in billing field woocommerce checkout page

in woocommerce check out page i want to show all countries in billing details field and show only specific country in shipping details country fields how i can do that
woocommerce setting page
you can add from dashboard too.
ship to specific counties option to add specific countries.
no need to add code. use code when you need cause of already country code in woocomerce plugin.
i hope this help
thanks
Hook "woocommerce_checkout_fields" use to modify checkout fields.
function woo_override_checkout_fields( $fields ) {
$fields['shipping']['shipping_country'] = array(
'type' => 'select',
'label' => __('Selected Country List', 'woocommerce'),
'options' => array('IND' => 'India','US' => 'USA')
);
return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'woo_override_checkout_fields' );

Wordpress Posts per page if condition met

I have a custom post type called activities and a number of standard pages that are different countries. Each country has a number of activities that are available. The user needs to add a range of different activities, and then choose what country each activity is available in.
Using list pages, I have a dropdown listing all pages in the activities custom post type. The user is able add a new activity, add the content and select what country(page) this activity relates to. On that particular country page on the front-end the available activities are listed.
For each country I only want to show 3 activities. I do the standard loop getting all custom post types for activities. I have to check whether the postid of the page chosen in the drop down menu matches the current page, and if it does show the activity. Using the standard posts_per_page isn't working, as it only grabs three posts, then does the conditional statement on those to see if any matches the current id.
I guess what I want to happen is to have the posts_per_page only apply to activities that actually match the criteria of having the country page id matching the id of the country selected in the activity.
global $post;
$postid = get_the_ID();
$args = array( 'post_type' => 'activities', 'orderby' => 'date', 'order' =>'ASC','posts_per_page' => 3);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$country = rwmb_meta( 'rw_activities_pages' );
// this is the drop down list of pages. It gets the ID of the country page chosen
$currentID = get_post($country);
$currentTitle = $currentID->post_title;
if ($country == $postid){
// if the activity country id matches the current page show the info
echo get_the_title();
echo $currentTitle;
echo the_content();
}
endwhile;
wp_reset_query();
Any ideas would be fantastic, as I have a couple of custom post types that do a similar thing!
Thanks in advance,
Rich
What you want to do is filter the posts in the query itself using meta_query.
$args = array(
'post_type' => 'activities',
'meta_query' => array(
'key' => 'rw_activities_page',
'value'=> $postid,
'compare'=>'='
),
'orderby' => 'date',
'order' =>'ASC',
'posts_per_page' => 3);
You might need to adjust the meta_key if that's not exactly the value it's using.

Resources