So, I found this snippet for automatically adding a product when a user visits a product page:
/*
* Add item to cart on visit
*/
function add_product_to_cart() {
if ( ! is_admin() ) {
global $woocommerce;
$product_id = 64;
$found = false;
//check if product already in cart
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $value
) {
$_product = $values['data'];
if ( $_product->id == $product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
$woocommerce->cart->add_to_cart( $product_id );
} else {
// if no products in cart, add it
$woocommerce->cart->add_to_cart( $product_id );
}
}
}
add_action( 'init', 'add_product_to_cart' );
Now, it is for only product "id=64." How would I change it so that any products will be added.
Thank you
Related
I am creating a landing page for the customers and with a specific products that has a category of landing-page.
I want the other products that is currently on the cart page to be removed when the category landing-page is present on the cart.
Here's the snippet. Right now, it removes all the products in it because of the $woocommerce->cart->empty_cart().
add_action('woocommerce_checkout_before_customer_details', 'check_if_landing_page_category_is_on_cart');
function check_if_landing_page_category_is_on_cart() {
global $woocommerce;
$categories = array('landing-page');
$has_category = false;
foreach ( WC()->cart->get_cart() as $cart_item ) {
// Check for product categories
if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
$woocommerce->cart->empty_cart();
$has_category = true;
break;
}
}
if ( $has_category ) {
?>
<style>
.coupon-form {
display: none;
}
</style>
<?php
}
}
Any advice?
You can use WC_Cart::remove_cart_item() opposite WC_Cart::empty_cart()
So you get:
function action_woocommerce_checkout_before_customer_details() {
// Add categories. Multiple can be added, separated by a comma
$categories = array( 'landing-page' );
// Initialize
$cart_item_keys = array();
$has_category = false;
// WC Cart NOT null
if ( ! is_null( WC()->cart ) ) {
// Loop through cart items
foreach ( WC()->cart->get_cart_contents() as $cart_item_key => $cart_item ) {
// Get product id
$product_id = $cart_item['product_id'];
// NOT certain category
if ( ! has_term( $categories, 'product_cat', $product_id ) ) {
// Push to array
$cart_item_keys[] = $cart_item_key;
} else {
$has_category = true;
}
}
// NOT empty & has category is true
if ( ! empty ( $cart_item_keys ) && $has_category ) {
// Loop through all cart item keys that do not contain the category
foreach ( $cart_item_keys as $cart_item_key ) {
// Remove product from cart
WC()->cart->remove_cart_item( $cart_item_key );
}
}
}
}
add_action( 'woocommerce_checkout_before_customer_details', 'action_woocommerce_checkout_before_customer_details', 10, 0 );
I'm trying to disable certain shipping method if the sum of the products from category "Pillows" is below $99 (it's some sort of a premium shipping active only if customer orders more than $99).
I have this piece of code
add_filter( 'woocommerce_package_rates', 'unset_shipping_below_free', 10, 2 );
function unset_shipping_below_free( $rates, $package ) {
$categories = array('PILLOWS'); // Defined targeted product categories
$threshold = 99; // Defined threshold amount
$cart = WC()->cart;
$cart_items = $cart->get_cart();
//How to sum the value of Pillows in the cart?
if ( $subtotal_pillows < $threshold ) {
if ( isset( $rates['free_shipping:4'] ) ) {
unset( $rates['free_shipping:4'] );
}
if ( isset( $rates['flexible_shipping_single:5'] ) ) {
unset( $rates['flexible_shipping_single:5'] );
}
}
return $rates;
}
But I can't figure out how to sum the value of the Pillows. Any help greatly appeciated
You should try this:
add_filter( 'woocommerce_package_rates', 'unset_shipping_below_free', 10, 2 );
function unset_shipping_below_free( $rates, $package ) {
$categories = array('pillows'); // Defined targeted product categories slug
$threshold = 99; // Defined threshold amount
$subtotal_pillows = 0;
// Loop through all products in the Cart
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
$quantity = $cart_item['quantity'];
$line_subtotal = $cart_item['line_subtotal'];
$item_price = $line_subtotal / $quantity;
$subtotal_pillows = $subtotal_pillows+$item_price;
}
}
//echo $subtotal_pillows; exit;
//How to sum the value of Pillows in the cart?
if ( $subtotal_pillows < $threshold ) {
if ( isset( $rates['free_shipping:4'] ) ) {
unset( $rates['free_shipping:4'] );
}
if ( isset( $rates['flexible_shipping_single:5'] ) ) {
unset( $rates['flexible_shipping_single:5'] );
}
}
return $rates;
}
Note: $categories = array('pillows'); you should use category slug instead of category name for that you will got $subtotal_pillows properly category wise
I am trying to, based on if one or more product is in the cart, to display a custom DIV-tag with a cart count before the product.
It works, but the count does not work. Why?
This is my code:
add_action('woocommerce_before_single_product', 'product_count', 1, 1 );
function product_count( $cart ) {
if ( is_admin() && !defined( 'DOING_AJAX' ) ) return;
global $product;
$product_count = $cart->cart_contents_count;
$product_ids = array( 1708 );
$in_cart = false;
foreach( WC()->cart->get_cart() as $cart_item ) {
$product_in_cart = $cart_item['product_id'];
if ( in_array( $product_in_cart, $product_ids ) ) {
$in_cart = true;
break;
}
}
if ( ! $in_cart ) {
return;
} else {
echo '<div class="product-count">You have added a total of '.$product_count.' products.</div>';
}
}
I want to unset the payment method 'bacs' for the product ID 6197.
I have tried the code below, but this does not have the desired result.
add_filter( 'woocommerce_available_payment_gateways', 'wp_unset_gateway_by_id', 10, 2 );`
function wp_unset_gateway_by_id( $available_gateways, $products) {
global $woocommerce;
$unset = false;
$product_ids = array('6197');
if( in_array( $product->get_id(), $product_ids ) || ( $product->is_type('variation') && in_array( $product->get_parent_id(), $product_ids ) ) ){
unset( $available_gateways['bacs'] );
return $available_gateways;
}
}
I believe I am doing something wrong, but I am relatively new to this. Any advice would be appreciated.
$products is not passed as argument to the woocommerce_available_payment_gateways filter hook
Apply it in the following way:
function filter_woocommerce_available_payment_gateways( $payment_gateways ) {
// Not on admin
if ( is_admin() ) return $payment_gateways;
// The targeted product ids (several can be added, separated by a comma)
$targeted_ids = array( 6197 );
// Flag
$found = false;
// WC Cart
if ( WC()->cart ) {
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( array_intersect( $targeted_ids, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) {
$found = true;
break;
}
}
}
// True
if ( $found ) {
// Bacs
if ( isset( $payment_gateways['bacs'] ) ) {
unset( $payment_gateways['bacs'] );
}
}
return $payment_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'filter_woocommerce_available_payment_gateways', 10, 1 );
I want to check if, at least, one of item has specific attribute term in WooCommerce order to display something in the mail order.
Attribute tax is 'pa_labels' and the term is 'tree' but something is missing... Any Idea ?
add_action( 'woocommerce_thankyou', 'webroom_check_product_attr_in_order', 5 );
function webroom_check_product_attr_in_order( $order_id ) {
if ( ! $order_id ) {
return;
}
$order = wc_get_order( $order_id );
$attr_in_order = false;
$items = $order->get_items();
foreach ( $items as $item ) {
$product_id = $item['product_id'];
if ( has_term( 'tree', 'pa_labels', $product_id ) ) {
$attr_in_order = true;
break;
}
}
// Echo content only if $attr_in_order == true
if ( $attr_in_order ) {
add_action( 'woocommerce_email_customer_details', 'custom_woocommerce_email_customer_details', 25);
}
}
You can try something like this:
// check if in the order there is at least one product with the attribute "pa_labels" => "tree"
add_action( 'woocommerce_thankyou', 'webroom_check_product_attr_in_order', 5 );
function webroom_check_product_attr_in_order( $order_id ) {
if ( ! $order_id ) {
return;
}
$order = wc_get_order( $order_id );
$attr_in_order = false;
foreach ( $order->get_items() as $item ) {
$product = $item->get_product();
if ( has_term( 'tree', 'pa_labels', $product->get_id() ) ) {
$attr_in_order = true;
break;
}
}
// Echo content only if $attr_in_order == true
if ( $attr_in_order ) {
custom_woocommerce_email_customer_details();
}
}
// add content to the customer details in the email
add_action( 'woocommerce_email_customer_details', 'custom_woocommerce_email_customer_details', 25);
function custom_woocommerce_email_customer_details() {
// do stuff
}
The code must be added to the functions.php of the active theme.