Skip to main content

Useful PHP Snippets

Below are some useful PHP snippets for custom ShopWP overrides.

Conditionally render different Elementor templates for the PDP

The below snippet does the following:

  1. Grabs all the collections associated with the product that you may have synced
  2. If no collections are found, stops rendering (might want to handle this differently)
  3. If the product is assigned to the collection Sale, then it renders the Elementor shortcode [elementor_shortcode_for_sale](this shortcode will be different. You can find it listed in the Elementor template page)
  4. Checks if the product belongs to any other collections, etc

use ShopWP\Utils;

defined('ABSPATH') ?: die();


global $post;

$DB_Products         = ShopWP\Factories\DB\Products_Factory::build();
$post_id             = $post->ID;
$product_data        = $DB_Products->get_product($post_id);
$product_collections = \maybe_unserialize($product_data->collections);

if (empty($product_collections)) {

function in_collection($collection_name, $product_collections) {
   return in_array($collection_name, array_column($product_collections, 'name'));

if (in_collection('Sale', $product_collections)) {
   echo do_shortcode('[elementor_shortcode_for_sale]');

} else if (in_collection('Another', $product_collections)) {
   echo do_shortcode('[elementor_shortcode_for_another]');

} else if (in_collection('And another', $product_collections)) {
   echo do_shortcode('[elementor_shortcode_for_and_another]');

} else if (in_collection('And yet another', $product_collections)) {
   echo do_shortcode('[elementor_shortcode_for_and_yet_another]');


Add custom attributes to the ShopWP script tags

The below snippet uses the native WordPress wp_script_attributes hook to customize the ShopWP script tag attributes.

function update_shopwp_script_attrs($attrs) {

	if ( empty( $attrs['id'] ) || empty( $attrs['src'] ) ) {
		return $attrs;

	if ( str_contains( $attrs['id'], 'shopwp' ) ) {
		$attrs['data-cfasync'] = 'true';

	return $attrs;

add_filter('wp_script_attributes', 'update_shopwp_script_attrs', 10, 1);