Increasing WooCommerce Online Store Sales (Technique Number One)
Automatically adding a gift to the WooCommerce cart
In Technique number two We will tell you how to encourage the user to buy more by combining technique number two and the new method
However, the number one technique to increase online sales
In many online stores, offering various discounts to customers is one of the ways to attract and keep them. But one of the more attractive strategies for Increase in online store sales can Add automatic gift Instead of providing a discount code, you can change the settings of WooCommerce so that a product is automatically added to the customer's shopping cart as a gift, provided that the total purchase exceeds a certain amount (for example, 5 million Tomans).
Steps to add an automatic gift to the shopping cart in WooCommerce:
You have two solutions to increase the sales of your online store with this method 1- Using the code that is placed at the end of the article, which we have placed in the continuation of the training 2- The use of plugins, which I do not recommend due to the pressure on the site and database occupation, as well as the costs for purchasing plugins. Including the right plugin To increase the sales of the online store by offering a gift, the "WooCommerce Auto Add Gift" extension was named Plugin settings: After installation, go to the plugin settings section and set the necessary conditions for adding the gift. For example:
- If the customer's shopping cart exceeds 5 million tomans.
- The gift must be selected from a specific category.
- The price of the gift should not be more than 500 thousand tomans.
Selection of gift products: Select a specific category from which gift products will be randomly selected
Use the following function code
Proceed as follows to set the code In section A, select the category from which you want the gift to be selected (currently the selected accessory category) In section B, select the highest amount of the product that is selected as a gift In section C, select the maximum purchase amount You should put this code at the end of your function file and save it
function add_random_gift_product_to_cart() {
if ( is_admin() ) {
return;
}
// setting
$category_slug = 'accessory'; // A
$maximum_price = 100; // B
$minimum_amount = 1000; // C
$cart_total = WC()->cart->subtotal_ex_tax;
if ( $cart_total >= $minimum_amount ) {
$gift_already_in_cart = false;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product_id = $cart_item['product_id'];
$product = wc_get_product($product_id);
$product_categories = wp_get_post_terms($product_id, 'product_cat', array("fields" => "slugs"));
if ( in_array( $category_slug, $product_categories ) && $product->get_price() <= $maximum_price && isset( $cart_item['is_gift'] ) && $cart_item['is_gift'] ) {
$gift_already_in_cart = true;
break;
}
}
if ( !$gift_already_in_cart ) {
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $category_slug,
),
),
'meta_query' => array(
array(
'key' => '_price',
'value' => $maximum_price,
'compare' => '<=',
'type' => 'NUMERIC',
),
),
);
$products = get_posts( $args );
if ( ! empty( $products ) ) {
$random_product = $products[array_rand($products)];
$gift_product_id = $random_product->ID;
WC()->cart->add_to_cart( $gift_product_id, 1, '', array(), array( 'is_gift' => true ) );
}
}
} else {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( isset( $cart_item['is_gift'] ) && $cart_item['is_gift'] ) {
WC()->cart->remove_cart_item( $cart_item_key );
}
}
}
}
add_action( 'template_redirect', 'add_random_gift_product_to_cart' );
// Display the text "Gift" in the shopping cart and the shopping cart widget
function display_gift_text_in_cart( $product_name, $cart_item, $cart_item_key ) {
if ( isset( $cart_item['is_gift'] ) && $cart_item['is_gift'] ) {
return $product_name . ' - Gift';
}
return $product_name;
}
add_filter( 'woocommerce_cart_item_name', 'display_gift_text_in_cart', 10, 3 );
add_filter( 'woocommerce_widget_cart_item_name', 'display_gift_text_in_cart', 10, 3 );
function hide_gift_product_price_in_cart( $price, $cart_item, $cart_item_key ) {
if ( isset( $cart_item['is_gift'] ) && $cart_item['is_gift'] ) {
return 'Gift';
}
return $price;
}
add_filter( 'woocommerce_cart_item_price', 'hide_gift_product_price_in_cart', 10, 3 );
add_filter( 'woocommerce_cart_item_subtotal', 'hide_gift_product_price_in_cart', 10, 3 );
add_filter( 'woocommerce_widget_cart_item_quantity', 'hide_gift_product_price_in_cart', 10, 3 );
function adjust_cart_total_for_gift( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
return;
}
foreach ( $cart_object->get_cart() as $cart_item_key => $cart_item ) {
if ( isset( $cart_item['is_gift'] ) && $cart_item['is_gift'] ) {
$cart_item['data']->set_price( 0 );
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'adjust_cart_total_for_gift', 10, 1 );
function disable_quantity_input_for_gift( $product_quantity, $cart_item_key, $cart_item ) {
if ( isset( $cart_item['is_gift'] ) && $cart_item['is_gift'] ) {
return '1';
}
return $product_quantity;
}
add_filter( 'woocommerce_cart_item_quantity', 'disable_quantity_input_for_gift', 10, 3 );