Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regression on issue #16 #81

Open
braders opened this issue Nov 21, 2023 · 0 comments
Open

Regression on issue #16 #81

braders opened this issue Nov 21, 2023 · 0 comments

Comments

@braders
Copy link

braders commented Nov 21, 2023

In #16, and associated PR #17, fixes were added to ensure that free gifts are not added to the cart during re-order.

This functionality is not working on Magento 2.4.4. I've not figured out quite when this broke, but I suspect the Magento core reorder code changed at some point.

Suggested solution
We have deployed the following code, which we suggest probably should be moved into this extension:

<type name="Magento\Quote\Model\Quote">
        <plugin name="gift_sales_rules_avoid_reorder_2" type="Vendor\SmileGiftSalesRule\Plugin\Quote\Model\Quote" />
</type>
namespace Vendor\SmileGiftSalesRule\Plugin\Quote\Model;

use Magento\Quote\Model\Quote as Subject;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Framework\DataObject;
use Magento\Framework\App\RequestInterface;

class Quote
{
    public function __construct(
        protected RequestInterface $request
    ) {
    }

    /**
     * Avoid to add a gift product when adding an order item.
     * Avoids the re-ordering of gift-items.
     *
     * sales_order_reorder: Front-end re-orders
     * sales_order_create_reorder: Admin re-orders
     *
     * Smile used to handle this out the box in `magento2-module-gift-sales-rule/Plugin/Checkout/Model/Cart.php`,
     * but that logic doesn't work with the latest versions of Magento.
     * This code is based on that logic.
     *
     * @param Subject  $subject Subject
     * @param \Closure $proceed Parent method
     * @param ProductInterface $orderItem Order item
     * @param DataObject $request   Quantity flag
     *
     * @return Subject
     */
    public function aroundAddProduct(
        Subject $subject,
        \Closure $proceed,
        $orderItem,
        $request = null
    ) {
        if ((isset($request['gift_rule']) && $request['gift_rule']) &&
            (in_array($this->request->getFullActionName(), ['sales_order_reorder', 'sales_order_create_reorder'], true))
        ) {
            return null;
        }

        return $proceed($orderItem, $request);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant