问题与复制和修改促销有关:代码兑换问题

huangapple go评论81阅读模式
英文:

Issues with copying and modifying promotions: Code redemption problem

问题

I am currently working on copying an existing promotion and making some minor modifications to it. Although I have made progress, I am encountering a few problems that I am unable to resolve.

Here are the changes I make when copying the promotion:

  • Set "active" to true
  • Generate a new "code"
  • Set "createdAt" to the current date
  • Set "maxRedemptionsGlobal" to 1
  • Set "validFrom" to the current date
  • Enable "use_codes" and "useIndividualCodes"
  • Set "validUntill" to a date 5 minutes from now

In addition, I delete the "id", "orderCountorderCount", and "ordersPerCustomerCountordersPerCustomerCount" fields.

Next, I pass this data to a backend route. Initially, I attempted to simply insert the new promotion into the database, which partially worked. The promotion appears in the admin site, but the code cannot be redeemed. Strangely, the code option is set to "No Promotion code required" by default. When I change it to "Fixed Promotion Code," my code is correctly set as the promotion code. However, I still cannot use the code, and I consistently receive an error stating that the promotion code could not be found.

This is how I insert the Data in the Database:

class getDiscountsController extends StorefrontController
{
    private EntityRepository $discountRepository;
    private EntityRepository $discountCartRepository;
    private EntityRepository $promotionRepository;

    public function __construct(
        EntityRepository $discountRepository,
        EntityRepository $discountCartRepository,
        EntityRepository $promotionRepository
    ) {
        $this->discountRepository = $discountRepository;
        $this->discountCartRepository = $discountCartRepository;
        $this->promotionRepository = $promotionRepository;
    }

    //other unrelevant code

    public function copyPromotion(string $code, string $promotionData, Context $context): JsonResponse
    {
        $promotionDataArray = json_decode($parsedPromotionData, true);

        unset($promotionDataArray['orderCount']);
        unset($promotionDataArray['ordersPerCustomerCount']);

        $this->promotionRepository->create([$promotionDataArray], $context);
    }
}

Below is the relevant section of my Service.xml file:

<service id="M28\LuckyDiscounts\Storefront\Controller\getDiscountsController" public="true">
  <argument type="service" id="m28_lucky_discounts.repository" />
  <argument type="service" id="m28_lucky_discounts_per_cart.repository" />
  <argument type="service" id="promotion.repository" />
  <call method="setContainer">
    <argument type="service" id="service_container" />
  </call>
</service>

Thank you for your assistance!

英文:

I am currently working on copying an existing promotion and making some minor modifications to it. Although I have made progress, I am encountering a few problems that I am unable to resolve.

Here are the changes I make when copying the promotion:

  • Set "active" to true
  • Generate a new "code"
  • Set "createdAt" to the current date
  • Set "maxRedemptionsGlobal" to 1
  • Set "validFrom" to the current date
  • Enable "use_codes" and "useIndividualCodes"
  • Set "validUntill" to a date 5 minutes from now

In addition, I delete the "id", "orderCountorderCount", and "ordersPerCustomerCountordersPerCustomerCount" fields.

Next, I pass this data to a backend route. Initially, I attempted to simply insert the new promotion into the database, which partially worked. The promotion appears in the admin site, but the code cannot be redeemed. Strangely, the code option is set to "No Promotion code required" by default. When I change it to "Fixed Promotion Code," my code is correctly set as the promotion code. However, I still cannot use the code, and I consistently receive an error stating that the promotion code could not be found.

This is how I insert the Data in the Database:

class getDiscountsController extends StorefrontController
{
    private EntityRepository $discountRepository;
    private EntityRepository $discountCartRepository;
    private EntityRepository $promotionRepository;

    public function __construct(
        EntityRepository $discountRepository,
        EntityRepository $discountCartRepository,
        EntityRepository $promotionRepository
    ) {
        $this-&gt;discountRepository = $discountRepository;
        $this-&gt;discountCartRepository = $discountCartRepository;
        $this-&gt;promotionRepository = $promotionRepository;
    }

    //other unrelevant code

    public function copyPromotion(string $code, string $promotionData, Context $context): JsonResponse
    {
        $promotionDataArray = json_decode($parsedPromotionData, true);

        unset($promotionDataArray[&#39;orderCount&#39;]);
        unset($promotionDataArray[&#39;ordersPerCustomerCount&#39;]);


        $this-&gt;promotionRepository-&gt;create([$promotionDataArray], $context);
    }
}

Below is the relevant section of my Service.xml file:

&lt;service id=&quot;M28\LuckyDiscounts\Storefront\Controller\getDiscountsController&quot; public=&quot;true&quot;&gt;
  &lt;argument type=&quot;service&quot; id=&quot;m28_lucky_discounts.repository&quot; /&gt;
  &lt;argument type=&quot;service&quot; id=&quot;m28_lucky_discounts_per_cart.repository&quot; /&gt;
  &lt;argument type=&quot;service&quot; id=&quot;promotion.repository&quot; /&gt;
  &lt;call method=&quot;setContainer&quot;&gt;
    &lt;argument type=&quot;service&quot; id=&quot;service_container&quot; /&gt;
  &lt;/call&gt;
&lt;/service&gt;

Thank you for your assistance!

答案1

得分: 1

你可以使用存储库的 clone 方法,并使用覆盖来应用更改到副本中。

在管理部分:

const behavior = {
    cloneChildren: true,
    overwrites: {
        orderCount: 0,
        ordersPerCustomerCount: null,
    },
};

await this.promotionRepository.clone(originalPromotionId, Shopware.Context.api, behavior);

服务器端:

$behavior = new CloneBehavior(
    [
        'orderCount' => 0,
        'ordersPerCustomerCount' => null,
    ],
    true
);

$this->promotionRepository->clone($originalPromotionId, $context, null, $behavior);
英文:

You can use the clone method of the repository and use overwrites to apply changes to the duplicate.

In the administration:

const behavior = {
    cloneChildren: true,
    overwrites: {
        orderCount: 0,
        ordersPerCustomerCount: null,
    },
};

await this.promotionRepository.clone(originalPromotionId, Shopware.Context.api, behavior);

Server-side:

$behavior = new CloneBehavior(
    [
        &#39;orderCount&#39; =&gt; 0,
        &#39;ordersPerCustomerCount&#39; =&gt; null,
    ],
    true
);

$this-&gt;promotionRepository-&gt;clone($originalPromotionId, $context, null, $behavior);

huangapple
  • 本文由 发表于 2023年6月29日 19:25:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76580582.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定