ShippingOrderProcessor ลงทะเบียนไม่ดี?

Oct 21 2020

ฉันกำลังพยายามใช้โซลูชัน @jsacksick ซึ่งควรบังคับให้แสดงค่าจัดส่งในหน้าตะกร้าสินค้า (โดยปกติจะไม่มีในขั้นตอนนั้น) ฉันสร้างโมดูลใหม่สำหรับสิ่งนี้และโครงสร้างก็ง่าย:

shipping_fix
  src
    ShippingOrderProcessor.php
  shipping_fix.info.yml
  shipping_fix.module
  shipping_fix.services.yml

ShippingOrderProcessor.php เป็นสำเนาที่ถูกต้องของคลาส @jsacksick: https://gist.github.com/jsacksick/fde031197df92236ef5a916775e17a2e

ไฟล์ข้อมูลประกอบด้วย:

name: Shipping fix
description: Fixes missing shipping info on cart page
type: module
core: 8.x
dependencies:

ไฟล์โมดูลว่างเปล่าและไฟล์บริการประกอบด้วย:

services:
  shipping_fix.shipping_order_processor:
    class: Drupal\shipping_fix\ShippingOrderProcessor
    tags:
      - { name: commerce_shipping.shipping_order_processor, priority: -300 }

แต่มันไม่ทำงาน ฉันพยายามตั้งค่าจุดพักภายใน__construct()และprocess()วิธีการของชั้นเรียนนั้น แต่ไม่สามารถเข้าถึงได้

พยายามเปลี่ยนแถวสุดท้ายที่นี่เป็น:

 - { name: commerce_order.order_processor, priority: -300 }

ตามที่อธิบายไว้ที่นี่: https://docs.drupalcommerce.org/commerce2/developer-guide/orders/order-refresh-and-process

แต่แล้วไซต์ของฉันก็ขัดข้องเมื่อฉันพยายามล้างแคช?

สำหรับฉันแล้วดูเหมือนว่าฉันไม่ได้ลงทะเบียนอ็อบเจ็กต์โปรเซสเซอร์นี้ให้ดี ปัญหาที่เป็นไปได้มากที่สุดคือในไฟล์บริการ แต่ฉันคิดไม่ออกว่ามันคืออะไร (" commerce_shipping.shipping_order_processor " เป็นค่าที่ถูกต้องหรือไม่)

ลองใช้บริการทิ้งขยะเช่น:

drush devel:services
drush devel-container-services
drush devel-services

และแม้กระทั่งโหมด devel จะเปิดอยู่สำหรับแต่ละครั้งที่ฉันได้รับ: ไม่ได้กำหนดคำสั่ง "devel-container-services" หรือคล้ายกัน

แน่นอนว่าฉันเปิดใช้งานโมดูลของฉันพยายามล้างแคช แต่ไม่ได้ช่วย

อัปเดต:ข้อผิดพลาดที่ได้รับเมื่อเปลี่ยนไฟล์บริการแถวสุดท้ายเป็น:

 - { name: commerce_order.order_processor, priority: -300 }

คือ:

บริการ 'shipping_fix.shipping_order_processor' สำหรับผู้บริโภค 'commerce_order.order_refresh' ไม่ได้ใช้ Drupal \ commerce_order \ OrderProcessorInterface

และไฟล์ ShippingOrderProcess.php เริ่มต้นด้วย:

<?php declare(strict_types=1);

namespace Drupal\my_module\OrderProcessor;

use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\commerce_order\OrderProcessorInterface;
use Drupal\commerce_shipping\ShipmentManagerInterface;
use Drupal\commerce_shipping\ShippingOrderManagerInterface;
use Drupal\profile\Entity\Profile;

/**
 * Ensures there are shipments from the start.
 */
final class ShippingProcessor implements OrderProcessorInterface {

  /**
   * The shipping order manager.
   *
   * @var \Drupal\commerce_shipping\ShippingOrderManagerInterface
   */
  protected $shippingOrderManager; /** * The shipment manager. * * @var \Drupal\commerce_shipping\ShipmentManagerInterface */ protected $shipmentManager;


  public function __construct(ShippingOrderManagerInterface $shippingOrderManager, ShipmentManagerInterface $shipmentManager) {
    $this->shippingOrderManager = $shippingOrderManager;
    $this->shipmentManager = $shipmentManager;
  }

...

อัปเดต 2:

จัดการเพื่อล้างแคช: สำหรับพื้นที่ชื่อโครงสร้างไฟล์ของฉันต้องเป็น:

namespace Drupal\shipping_fix;

เปลี่ยนไฟล์ servies ด้วยและตอนนี้ดูเหมือนว่า:

services:
  shipping_fix.shipping_order_processor:
    class: Drupal\shipping_fix\ShippingProcessor
    arguments: ['@commerce_shipping.order_manager', '@commerce_shipping.shipment_manager']
    tags:
      - { name: commerce_order.order_processor, priority: -300 }

ตอนนี้การล้างแคชใช้งานได้ แต่ฉันยังไม่เห็นค่าขนส่งในหน้ารถเข็น ...

คำตอบ

NoSssweat Oct 22 2020 at 12:01

ในกรณีนี้คุณจะต้องเรียกใช้ก่อนหน้านี้commerce_shipping.early_order_processorกำหนดลำดับความสำคัญเป็น 300

services:
  shipping_fix.shipping_order_processor:
    class: Drupal\shipping_fix\ShippingProcessor
    arguments: ['@commerce_shipping.order_manager', '@commerce_shipping.shipment_manager']
    tags:
      - { name: commerce_order.order_processor, priority: 300 }