Last updated on October 18, 2023

Elementor WooCommerce issues

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Address common WooCommerce issues in Elementor.

Elementor is a popular page builder plugin for WordPress, and WooCommerce is a widely used e-commerce plugin. Sometimes, when using Elementor with WooCommerce, you may encounter certain issues or limitations. In this response, I will provide you with code snippets that can help you address some common Elementor WooCommerce issues.

1. Displaying WooCommerce product price in Elementor:

To display the price of a WooCommerce product in Elementor, you can use the wc_price() function provided by WooCommerce. This function formats the price with the appropriate currency symbol and decimal separators.

<?php
$product_id = get_the_ID(); // Get the current product ID
$product_price = get_post_meta( $product_id, '_regular_price', true ); // Get the regular price

if ( $product_price ) {
    echo wc_price( $product_price ); // Display the formatted price
}
?>

2. Adding WooCommerce product add to cart button in Elementor:

To add a WooCommerce product’s “Add to Cart” button in Elementor, you can use the woocommerce_template_loop_add_to_cart() function. This function generates the HTML markup for the button.

<?php
$product_id = get_the_ID(); // Get the current product ID

if ( $product_id ) {
    woocommerce_template_loop_add_to_cart( array( 'product_id' => $product_id ) ); // Display the "Add to Cart" button
}
?>

3. Customizing WooCommerce product loop in Elementor:

To customize the appearance of the WooCommerce product loop in Elementor, you can override the default WooCommerce template file. First, create a new folder called woocommerce in your theme directory. Then, copy the templates folder from the WooCommerce plugin directory to your theme directory. Finally, you can modify the template files in your theme’s woocommerce folder to customize the product loop.

For example, to customize the product loop item template, create a file called content-product.php in your theme’s woocommerce folder and modify it as needed.

<?php
// Custom content-product.php template for WooCommerce product loop in Elementor
// Modify this file to customize the product loop item

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

global $product;

// Output your custom HTML markup for the product loop item
?>
<div class="custom-product-loop-item">
    <h2><?php echo $product->get_title(); ?></h2>
    <div class="price"><?php echo $product->get_price_html(); ?></div>
    <?php woocommerce_template_loop_add_to_cart(); ?>
</div>

These code snippets can be useful when you encounter issues related to displaying WooCommerce product information or customizing the product loop in Elementor. By using these snippets, you can have more control over the appearance and functionality of your WooCommerce-powered online store.

Examples

Example 1: Fixing Elementor WooCommerce compatibility issues

This use case demonstrates how to fix compatibility issues between Elementor and WooCommerce by removing the default WooCommerce styles and scripts from the Elementor editor.

function wpsnippets_remove_woocommerce_styles() {
    if ( class_exists( 'WooCommerce' ) && ElementorPlugin::$instance->editor->is_edit_mode() ) {
        remove_action( 'wp_enqueue_scripts', [ 'WC_Frontend_Scripts', 'load_scripts' ] );
        remove_action( 'wp_print_scripts', [ 'WC_Frontend_Scripts', 'localize_printed_scripts' ] );
        remove_action( 'wp_print_footer_scripts', [ 'WC_Frontend_Scripts', 'localize_printed_scripts' ] );
        remove_action( 'wp_enqueue_scripts', [ 'WC_Emails', 'enqueue_styles' ] );
        remove_action( 'wp_enqueue_scripts', [ 'WC_Embed', 'enqueue_styles' ] );
    }
}
add_action( 'wp_enqueue_scripts', 'wpsnippets_remove_woocommerce_styles', 99 );

This code removes the default WooCommerce styles and scripts from the Elementor editor. It checks if WooCommerce is active and if the current page is being edited with Elementor. If both conditions are met, it removes the actions responsible for enqueuing the WooCommerce styles and scripts.

Example 2: Customizing the WooCommerce product grid in Elementor

This use case demonstrates how to customize the WooCommerce product grid in Elementor by modifying the query arguments.

function wpsnippets_custom_product_grid_query( $query ) {
    if ( ElementorPlugin::$instance->editor->is_edit_mode() && $query->is_main_query() && is_post_type_archive( 'product' ) ) {
        $query->set( 'posts_per_page', 12 );
        $query->set( 'orderby', 'date' );
        $query->set( 'order', 'DESC' );
    }
}
add_action( 'pre_get_posts', 'wpsnippets_custom_product_grid_query' );

This code modifies the query arguments for the WooCommerce product grid in Elementor. It checks if the current page is being edited with Elementor and if it’s the main query for the product archive page. If both conditions are met, it sets the number of products per page to 12, orders the products by date in descending order.

Example 3: Adding custom fields to WooCommerce product in Elementor

This use case demonstrates how to add custom fields to the WooCommerce product editor in Elementor using the woocommerce_product_data_panels action.

function wpsnippets_add_custom_product_fields() {
    global $product_object;

    echo '<div class="options_group">';
    woocommerce_wp_text_input( array(
        'id'          => '_custom_field',
        'label'       => __( 'Custom Field', 'text-domain' ),
        'placeholder' => __( 'Enter custom field value', 'text-domain' ),
        'desc_tip'    => true,
        'description' => __( 'This is a custom field for the product.', 'text-domain' ),
    ) );
    echo '</div>';
}
add_action( 'woocommerce_product_data_panels', 'wpsnippets_add_custom_product_fields' );

This code adds a custom field to the WooCommerce product editor in Elementor. It uses the woocommerce_wp_text_input function to generate a text input field with a label, placeholder, description, and tooltip. The custom field is displayed within a new options group in the product editor.

Last updated on October 18, 2023. Originally posted on November 14, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *