【问题标题】:Make company name not required WooCommerce (3rd party plugin)不需要公司名称 WooCommerce(第三方插件)
【发布时间】:2017-10-11 11:53:41
【问题描述】:

我想创建 WooCommerce 中不需要的计费公司和运输公司。出于某种原因,我使用的代码适用于除公司部分之外的所有内容。 原来是第三方插件要求公司名称,这是该插件的完整代码:

<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}


require_once('legacy_pakkelabels_shipping_main.php');

/**
 * @class 		Pakkelabels_Shipping_GLS_Business_Legacy
 * @version		0.1.0
 * @author 		Magnus Vejlø - Pakkelabels
 */
class Legacy_Pakkelabels_Shipping_GLS_Business extends Legacy_Pakkelabels_Shipping_Main
{

    public function __construct($instance_id = 0)
    {
        $this->id = 'legacy_pakkelabels_shipping_gls_business';
        $this->instance_id = absint($instance_id);
        $this->method_title = __('GLS Business ', 'woocommerce-pakkelabels');
        $this->method_description = __('Adds the option to ship with the GLS business to the checkout', 'woocommerce-pakkelabels');
        $this->init();
    }


    /* add the diffrent actions */
    function addActions()
    {
        //adds the shipping method to the WooCommerce
        add_filter('woocommerce_shipping_methods', array($this, 'register_shipping_method'));

        add_action('woocommerce_after_shipping_rate', array($this, 'pakkelabels_shipping_gls_business_show_below_shipping'));

        add_action('woocommerce_checkout_process', array($this, 'pakkelabels_shipping_gls_business_field_process'));
    }


    function addFilters()
    {

    }


    function pakkelabels_shipping_gls_business_field_process() {

        global $woocommerce;
        $choosen_shipping_method1 = preg_replace('/\d/', '', $woocommerce->session->chosen_shipping_methods[0] );
        $choosen_shipping_method2 = preg_replace('/\d/', '', $woocommerce->session->chosen_shipping_methods );
        if((isset($_POST['ship_to_different_address']) &&  ($_POST['shipping_company'] == '' || !isset($_POST['shipping_company']))) && ($choosen_shipping_method1 == "legacy_pakkelabels_shipping_gls_business" || $choosen_shipping_method2 == "legacy_pakkelabels_shipping_gls_business")){
            if ( version_compare( $woocommerce->version, '2.1', '<' ) ) {
                $woocommerce->add_error(__('Please fill out the Shipping company', 'woocommerce-pakkelabels'));
            } else {
                wc_add_notice( __('Please fill out the Shipping company', 'woocommerce-pakkelabels') , 'error');
            }
        }
        if((!isset($_POST['ship_to_different_address']) && ($_POST['billing_company'] == '' || !isset($_POST['billing_company']))) && ($choosen_shipping_method1 == "legacy_pakkelabels_shipping_gls_business" || $choosen_shipping_method2 == "legacy_pakkelabels_shipping_gls_business")){
            if ( version_compare( $woocommerce->version, '2.1', '<' ) ) {
                $woocommerce->add_error(__('Please fill out the billing company', 'woocommerce-pakkelabels'));
            } else {
                wc_add_notice( __('Please fill out the billing company', 'woocommerce-pakkelabels') , 'error');
            }
        }
    }




    function pakkelabels_shipping_gls_business_show_below_shipping($rate){
        global $woocommerce;

        global $woocommerce;
        $choosen_shipping_method1 = preg_replace('/\d/', '', $woocommerce->session->chosen_shipping_methods[0] );
        $choosen_shipping_method2 = preg_replace('/\d/', '', $woocommerce->session->chosen_shipping_methods );
        if($choosen_shipping_method1 == "legacy_pakkelabels_shipping_gls_business" || $choosen_shipping_method2 == "legacy_pakkelabels_shipping_gls_business"){
            if($rate->method_id == 'legacy_pakkelabels_shipping_gls_business'){
                echo '<div class="gls_shipping_method_text shipping_company_required">'  . __('The company name is required.', 'woocommerce-pakkelabels').'</div>';
            }
        }
    }


    /* Register the shipping method in WooCommerce*/
    function register_shipping_method($methods)
    {
        $methods['legacy_pakkelabels_shipping_gls_business'] = 'Legacy_Pakkelabels_Shipping_GLS_Business';
        return $methods;
    }
}


$pakkelabels_GLS_Business_Legacy = new Legacy_Pakkelabels_Shipping_GLS_Business();
$pakkelabels_GLS_Business_Legacy->mainAddActions();
$pakkelabels_GLS_Business_Legacy->addActions();
$pakkelabels_GLS_Business_Legacy->addFilters();

使用的插件是一个交付插件,由于我们向公司交付,它需要一个公司名称。但是,由于我们是一家 B2B 商店,我们已经注册了这些公司名称,我们不需要它们,也不需要在结账时使用它们。所以我们在 WooCommerce 中隐藏了公司名称字段,但是无论我们写什么代码,我们都不能让它不需要填写。

【问题讨论】:

    标签: php wordpress woocommerce checkout


    【解决方案1】:

    改用过滤器 woocommerce_default_address_fields。

    function modify_woocommerce_default_address_fields( $fields ) {
        $fields['company']['required'] = false;
    
        return $fields;
    }
    
    add_filter( 'woocommerce_default_address_fields', 'modify_woocommerce_default_address_fields', 100, 1 );
    

    它在documentation 中声明了必须通过此过滤器操作的特定字段。

    • 国家
    • 名字
    • 姓氏
    • 公司
    • address_1
    • address_2
    • 城市
    • 状态
    • 邮政编码

    我们可以使用过滤器在通知添加到特定于该错误消息的数组之前删除它。

    function modify_woocommerce_notices( $message ) {
        if( stripos( $message, 'Please fill out the billing company' ) !== false ) {
            return '';
        }
    }
    
    add_filter( 'woocommerce_add_error', 'modify_woocommerce_notices' );
    

    我尚未测试该代码,但这是您如何抑制错误并阻止帐单公司填写必填字段的一般思路。

    【讨论】:

    • 谢谢。试过了,虽然我在编辑器中没有错误,但我仍然收到“请填写您要发送包裹的公司名称”错误。
    • 听起来您安装了其他导致此问题的插件,因为默认情况下,公司名称不是 WooCommerce 中的必填字段。
    • 你好。是的,我已经编辑了原始帖子并找到了导致错误的代码。您知道如何在该代码中修复它吗?
    • 在 WooCommerce 中,该字段不是必需的。就像我之前说的,你有另一个插件使该字段成为强制性的。是的,我可以看到您现在已将其添加到您的问题中,因此,如果您发现添加该功能的钩子操作的优先级是什么,那么您可以使用我的功能覆盖它,将其设置为在插件功能之后运行。您能否粘贴对该函数 pakkelabels_shipping_gls_business_field_process 的操作调用。否则只需将我的钩子的优先级设置为以低优先级运行,我将更改上面的代码。
    • 很多安德鲁。是的,你是对的,它是第三方插件。我已经附上了导致错误的插件部分的完整代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 2020-11-01
    • 2021-09-26
    • 2017-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多