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

支付方式动态显示 #144

Closed
okami-chen opened this issue Feb 10, 2025 · 1 comment
Closed

支付方式动态显示 #144

okami-chen opened this issue Feb 10, 2025 · 1 comment

Comments

@okami-chen
Copy link

读取支付方式下面的Checkout动态显示?

            $className = '\\Plugin\\' . Str::studly($item->code) . '\\Checkout';
            if (class_exists($className)) {
                $object = app($className);
                if (method_exists($object, 'isEnable')) {
                    $plugin->setEnabled($object->isEnable());
                }
            }
@yushine
Copy link
Contributor

yushine commented Feb 22, 2025

不需要这样,写个插件就解决了。

后台配置:

$mappers = $options = [];

$currencies = currencies();

foreach (CountryRepo::getInstance()->getCountries() as $country) {
    $options[] = [
        'value' => $country->id,
        'label' => $country->name,
    ];
}

$billingMethods = PluginRepo::getInstance()->getBillingMethods();
foreach ($billingMethods as $method) {
    $plugin    = $method->plugin;
    $mappers[] = [
        'name'        => $plugin->code,
        'label'       => $plugin->name['zh_cn'] ?? $plugin->code,
        'type'        => 'select',
        'options'     => $options,
        'required'    => true,
        'rules'       => 'required',
        'emptyOption' => false,
    ];
}

return $mappers;

启动文件核心代码

class Boot
{
    /**
     * @return void
     * @throws Throwable
     */
    public function init(): void
    {
        listen_hook_filter('service.checkout.billing.methods', function ($data) {
            foreach ($data as $index => $item) {
                if (! PaymentFilterService::getInstance($item['code'])->checkValid()) {
                    unset($data[$index]);
                }
            }

            return array_values($data);
        });
    }
}

判断是否可用核心代码

    public function checkValid(): bool
    {
        $countryID = plugin_setting('pay_filter', $this->billingCode, false);
        $address   = CheckoutService::getInstance()->getShippingAddress();

        if (empty($countryID)) {
            return false;
        }

        if (empty($address)) {
            return false;
        }

        return $countryID == $address['country_id'] ?? 0;
    }

@yushine yushine closed this as completed Feb 26, 2025
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

2 participants