英文:
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->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!
答案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(
[
'orderCount' => 0,
'ordersPerCustomerCount' => null,
],
true
);
$this->promotionRepository->clone($originalPromotionId, $context, null, $behavior);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论