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:
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!
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(
[
'orderCount' => 0,
'ordersPerCustomerCount' => null,
],
true
);
$this->promotionRepository->clone($originalPromotionId, $context, null, $behavior);