I am using Feeds to import products and product displays. Because I am working on multiple sites with one CSV file, I need to create a rule to make sure that one cannot view a product display if it has not referenced products.

I am thinking about creating a rule on Node creation or update with a condition for the product reference field. If empty, it would unpublish the product display. However, I'm not familiar with rules to know how to do this.

Is this the correct way? Any better ideas? Suggestions?

Comments

liupascal’s picture

Something like this :
Rule event :
- Node creation
- Node update

Rule condition :
1- entity has field
--> product reference field
2- field is empty on the product reference field

Rule action :
- unpublish node

rtdean93’s picture

Thanks for the suggestion...

Since the Referenced Product is required, just checking to see if it was empty was not enough. In fact, the rule could not fire since I could not create or update a blank field. (the only way a field could be empty is if we use feeds to import product displays without the related products).

So instead, I created a view to show all of the related products attached to a node. Then I used a rule to count the results of the view. If the view was blank, then the rule would unpublish the product display.

Here is the view...

$view = new view;
$view->name = 'product_count';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'node';
$view->human_name = 'Product Count';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'Product Count';
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['query']['options']['query_comment'] = FALSE;
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['pager']['options']['items_per_page'] = '10';
$handler->display->display_options['style_plugin'] = 'table';
/* Relationship: Content: Referenced product */
$handler->display->display_options['relationships']['field_product_product_id']['id'] = 'field_product_product_id';
$handler->display->display_options['relationships']['field_product_product_id']['table'] = 'field_data_field_product';
$handler->display->display_options['relationships']['field_product_product_id']['field'] = 'field_product_product_id';
$handler->display->display_options['relationships']['field_product_product_id']['required'] = 1;
/* Field: Content: Title */
$handler->display->display_options['fields']['title']['id'] = 'title';
$handler->display->display_options['fields']['title']['table'] = 'node';
$handler->display->display_options['fields']['title']['field'] = 'title';
$handler->display->display_options['fields']['title']['label'] = '';
$handler->display->display_options['fields']['title']['alter']['alter_text'] = 0;
$handler->display->display_options['fields']['title']['alter']['make_link'] = 0;
$handler->display->display_options['fields']['title']['alter']['absolute'] = 0;
$handler->display->display_options['fields']['title']['alter']['word_boundary'] = 0;
$handler->display->display_options['fields']['title']['alter']['ellipsis'] = 0;
$handler->display->display_options['fields']['title']['alter']['strip_tags'] = 0;
$handler->display->display_options['fields']['title']['alter']['trim'] = 0;
$handler->display->display_options['fields']['title']['alter']['html'] = 0;
$handler->display->display_options['fields']['title']['hide_empty'] = 0;
$handler->display->display_options['fields']['title']['empty_zero'] = 0;
$handler->display->display_options['fields']['title']['link_to_node'] = 1;
/* Field: Commerce Product: Product ID */
$handler->display->display_options['fields']['product_id']['id'] = 'product_id';
$handler->display->display_options['fields']['product_id']['table'] = 'commerce_product';
$handler->display->display_options['fields']['product_id']['field'] = 'product_id';
$handler->display->display_options['fields']['product_id']['relationship'] = 'field_product_product_id';
$handler->display->display_options['fields']['product_id']['alter']['alter_text'] = 0;
$handler->display->display_options['fields']['product_id']['alter']['make_link'] = 0;
$handler->display->display_options['fields']['product_id']['alter']['absolute'] = 0;
$handler->display->display_options['fields']['product_id']['alter']['external'] = 0;
$handler->display->display_options['fields']['product_id']['alter']['replace_spaces'] = 0;
$handler->display->display_options['fields']['product_id']['alter']['trim_whitespace'] = 0;
$handler->display->display_options['fields']['product_id']['alter']['nl2br'] = 0;
$handler->display->display_options['fields']['product_id']['alter']['word_boundary'] = 1;
$handler->display->display_options['fields']['product_id']['alter']['ellipsis'] = 1;
$handler->display->display_options['fields']['product_id']['alter']['strip_tags'] = 0;
$handler->display->display_options['fields']['product_id']['alter']['trim'] = 0;
$handler->display->display_options['fields']['product_id']['alter']['html'] = 0;
$handler->display->display_options['fields']['product_id']['element_label_colon'] = 1;
$handler->display->display_options['fields']['product_id']['element_default_classes'] = 1;
$handler->display->display_options['fields']['product_id']['hide_empty'] = 0;
$handler->display->display_options['fields']['product_id']['empty_zero'] = 0;
$handler->display->display_options['fields']['product_id']['hide_alter_empty'] = 1;
$handler->display->display_options['fields']['product_id']['link_to_product'] = 0;
/* Sort criterion: Content: Post date */
$handler->display->display_options['sorts']['created']['id'] = 'created';
$handler->display->display_options['sorts']['created']['table'] = 'node';
$handler->display->display_options['sorts']['created']['field'] = 'created';
$handler->display->display_options['sorts']['created']['order'] = 'DESC';
/* Contextual filter: Content: Nid */
$handler->display->display_options['arguments']['nid']['id'] = 'nid';
$handler->display->display_options['arguments']['nid']['table'] = 'node';
$handler->display->display_options['arguments']['nid']['field'] = 'nid';
$handler->display->display_options['arguments']['nid']['default_action'] = 'default';
$handler->display->display_options['arguments']['nid']['default_argument_type'] = 'node';
$handler->display->display_options['arguments']['nid']['default_argument_skip_url'] = 0;
$handler->display->display_options['arguments']['nid']['summary']['number_of_records'] = '0';
$handler->display->display_options['arguments']['nid']['summary']['format'] = 'default_summary';
$handler->display->display_options['arguments']['nid']['summary_options']['items_per_page'] = '25';
$handler->display->display_options['arguments']['nid']['break_phrase'] = 0;
$handler->display->display_options['arguments']['nid']['not'] = 0;
/* Filter criterion: Content: Published */
$handler->display->display_options['filters']['status']['id'] = 'status';
$handler->display->display_options['filters']['status']['table'] = 'node';
$handler->display->display_options['filters']['status']['field'] = 'status';
$handler->display->display_options['filters']['status']['value'] = 1;
$handler->display->display_options['filters']['status']['group'] = 1;
$handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;
/* Filter criterion: Commerce Product: Status */
$handler->display->display_options['filters']['status_1']['id'] = 'status_1';
$handler->display->display_options['filters']['status_1']['table'] = 'commerce_product';
$handler->display->display_options['filters']['status_1']['field'] = 'status';
$handler->display->display_options['filters']['status_1']['relationship'] = 'field_product_product_id';
$handler->display->display_options['filters']['status_1']['value'] = '1';

/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page');
$handler->display->display_options['path'] = 'product-count';

Here is the rule...

{ "rules_hide_product_displays_with_no_products" : {
    "LABEL" : "Hide product displays with no products",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "rb_misc" ],
    "ON" : [ "node_insert", "node_update" ],
    "IF" : [
      { "NOT rb_misc_action_views_result_count" : { "view" : "product_count|page", "args" : "[node:nid]", "minimum" : "1" } },
      { "node_is_of_type" : {
          "node" : [ "node" ],
          "type" : { "value" : { "product_display" : "product_display" } }
        }
      }
    ],
    "DO" : [ { "node_unpublish" : { "node" : [ "node" ] } } ]
  }
}
rszrama’s picture

Status: Active » Fixed

Marking this fixed. Glad to see you got a solution.

liupascal’s picture

You could also have looped over the list of products and checked each status with rule component.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

summit’s picture

Hi,
How to handle this when you have multiple views which show product-displays?
Greetings, Martijn