Add clone handler for Commerce products and variations

Created on 30 November 2017, almost 7 years ago
Updated 28 February 2024, 9 months ago

Cloning Product doesn't work correct right now, cloned product have broken variations, because product id in variations from original product, not from cloned.

✨ Feature request
Status

Closed: works as designed

Version

1.0

Component

Code

Created by

πŸ‡·πŸ‡ΊRussia r.nabiullin

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States bobburns

    Number 35 custom module worked for me . . . select save product as cloned at bottom of Product Clone page. Get both the variations and Product, you'll need to edit the variation but it works

    Changed core version to => core_version_requirement: ">=8" in info.yml

  • πŸ‡ΊπŸ‡ΈUnited States bobburns

    I spoke to soon. The custom module commerce entity clone throws the following error when a node is attempted to be cloned. Unistall of it solves it

    The website encountered an unexpected error. Please try again later.
    Error: Call to undefined method Drupal\node\Entity\Node::setVariations() in Drupal\commerce_entity_clone\EventSubscriber\ProductEntityCloneSubscriber->ProductEntityPreClone() (line 22 of modules/commerce_entity_clone/src/EventSubscriber/ProductEntityCloneSubscriber.php).
    Drupal\commerce_entity_clone\EventSubscriber\ProductEntityCloneSubscriber->ProductEntityPreClone(Object, 'entity_clone.pre_clone', Object)
    call_user_func(Array, Object, 'entity_clone.pre_clone', Object) (Line: 142)
    Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object, 'entity_clone.pre_clone') (Line: 342)
    Drupal\entity_clone\Form\EntityCloneForm->submitForm(Array, Object)
    call_user_func_array(Array, Array) (Line: 114)
    Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object) (Line: 52)
    Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object) (Line: 597)
    Drupal\Core\Form\FormBuilder->processForm('entity_clone_form', Array, Object) (Line: 325)
    Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 73)
    Drupal\Core\Controller\FormController->getContentResult(Object, Object)
    call_user_func_array(Array, Array) (Line: 123)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 580)
    Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 169)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 81)
    Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
    Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
    Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
    Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 52)
    Drupal\crawler_rate_limit\CrawlerRateLimitMiddleware->handle(Object, 1, 1) (Line: 57)
    Drupal\advban\AdvbanMiddleware->handle(Object, 1, 1) (Line: 50)
    Drupal\ban\BanMiddleware->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
    Stack\StackedHttpKernel->handle(Object, 1, 1) (Line

  • πŸ‡ΊπŸ‡¦Ukraine rsych

    Call to undefined method Drupal\node\Entity\Node::setVariations() in Drupal\commerce_entity_clone\EventSubscriber\ProductEntityCloneSubscriber->ProductEntityPreClone() (line 22 of modules/commerce_entity_clone/src/EventSubscriber/ProductEntityCloneSubscriber.php)

    This issue can be fixed by wrapping the code inside the ProductEntityPreClone function:

    public function ProductEntityPreClone(EntityCloneEvent $event) {
      $original = $event->getEntity();
      if ($original instanceof Product) {
        // The rest of the code.
      }
    }

    And also use the Product class at the top of the file:
    use Drupal\commerce_product\Entity\Product;

  • πŸ‡ΊπŸ‡¦Ukraine rsych

    Also, I found another issue: when the user wants to clone old Product, there is a chance that a newly generated SKU will be the same as the existing one (from one of the Products created later). Here's the fix for it:

    $sku_query = $this->database->select('commerce_product_variation_field_data', 'v')
      ->fields('v', ['sku']);
      $sku_query_order = $sku_query->addExpression('CAST(sku as UNSIGNED)');
      $sku_query->orderBy($sku_query_order, 'DESC');
      $latest_sku_record = $sku_query->range(0, 1)->execute()->fetch();
      $new_sku = $latest_sku_record->sku + 1;

    But the Connection should be added by the dependency injection in order to be used as $this->database.

Production build 0.71.5 2024