首页 > 解决方案 > 自定义产品类型不允许在 Woocommerce 中更新

问题描述

我正在尝试创建自定义产品类型,但是在更新以前创建的产品时,它没有更新。例如:如果我要更新的产品是简单类型,当我将其更改为我的自定义产品类型并保存时,它仍然保持为简单类型而不是我的自定义产品类型。

您可以在此处查看问题的 gif:https ://media.giphy.com/media/5z83HMwdPy5twyUOGT/giphy.gif

目前我有 Woocommerce 3.4.5 版和 WordPress 5.0-alpha-43406 版。

接下来我留下用于生成个性化产品类型的代码:

WC_Product_canopytour.php

class WC_Product_CanopyTour extends WC_Product {
    public function __construct( $product ) {
        $this->product_type = 'canopytour';
        $this->virtual = 'yes';
        parent::__construct( $product );
    }

    public function get_type() {
        return 'canopytour';
    }
}

类-woocommerce-custom-product.php

    public function register_canopytour_product_type() {
        include_once(plugin_dir_path( dirname( __FILE__ ) ) . 'woocommerce/WC_Product_canopytour.php');
    }

    public function add_canopytour_product( $types ) {
        $types[ 'canopytour' ] = __( 'Canopy Tour', $this->wcb );
        return $types;
    }

    public function get_tour_product_class($classname, $product_type) {
        if ( $product_type === "canopytour" ) {
            $classname = 'WC_Product_CanopyTour';
        }
        return $classname;
    }

    public function wcb_admin_footer() {
        if ( 'product' != get_post_type() ) :
            return;
        endif;

        ?><script type='text/javascript'>
            jQuery( document ).ready( function() {
                jQuery( '.options_group.pricing' ).addClass( 'show_if_canopytour show_if_variable_canopytour show_if_simple show_if_external' ).show();
                jQuery( 'li.general_options.general_tab' ).addClass( 'show_if_canopytour show_if_variable_canopytour show_if_simple show_if_external' ).show();
            });
        </script><?php
    }

    public function add_canopytour_tab($tabs) {
        $tabs['canopytour'] = array(
            'label'     => __( 'Canopy Tour', 'woocommerce' ),
            'target'    => 'canopytour_options',
            'class'     => array( 'show_if_canopytour', 'show_if_variable_canopytour'  ),
        );
        return $tabs;
    }

    public function canopytour_options_product_tab_content() {
        global $post; ?>
        <div id='canopytour_options' class='panel woocommerce_options_panel'>
            <div class='options_group'>
            </div>
        </div><?php
    }

    function hide_wcb_data_panel( $tabs) {
        // Other default values for 'attribute' are; general, inventory, shipping, linked_product, variations, advanced
        $tabs['shipping']['class'][] = 'hide_if_canopytour hide_if_variable_canopytour';
        return $tabs;
    }

类-wcb.php

require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/woocommerce/class-woocommerce-custom-product.php';

private function define_admin_hooks() {

        $plugin_admin = new WCB_Admin( $this->get_wcb(), $this->get_version() );

        $this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' );
        $this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' );

        if ($this->is_woocommerce_active()) {
            $woo_ct = new WCB_Woocommerce_CanopyTour_Product_Type( $this->get_wcb(), $this->get_version() );
            $this->loader->add_action( 'init', $woo_ct, 'register_canopytour_product_type' );
            $this->loader->add_filter( 'product_type_selector', $woo_ct, 'add_canopytour_product' );
            $this->loader->add_filter( 'woocommerce_product_class', $woo_ct, 'get_tour_product_class', 10, 2 ); 
            $this->loader->add_action( 'admin_head', $woo_ct, 'wcb_admin_head' );
            $this->loader->add_action( 'admin_footer', $woo_ct, 'wcb_admin_footer' );
            $this->loader->add_filter( 'woocommerce_product_data_tabs', $woo_ct, 'add_canopytour_tab' );
            $this->loader->add_action( 'woocommerce_product_data_panels', $woo_ct, 'canopytour_options_product_tab_content' );
            $this->loader->add_action( 'woocommerce_process_product_meta_simple_rental', $woo_ct, 'save_canopytour_option_field'  );
            $this->loader->add_action( 'woocommerce_process_product_meta_variable_rental', $woo_ct, 'save_canopytour_option_field'  );
            $this->loader->add_filter( 'woocommerce_product_data_tabs', $woo_ct, 'hide_wcb_data_panel' );

            $this->loader->add_action( 'woocommerce_product_options_pricing', $woo_ct, 'wcb_children_product_field' );
            $this->loader->add_action( 'save_post', $woo_ct, 'wcb_children_price_save_product' );
        }
    }

使用此代码,将定制产品的类型添加到产品信息选择中。当我选择创建的产品类型时,我还可以看到自定义选项卡。但保存时选择的产品类型不保存,返回初始值。

我在 github 中有我的插件的源代码,如果你能给它一个视图,也许我的代码有问题: https ://github.com/jesus997/Woocommerce-Canopy-Booking

启动项目的步骤:

  • 克隆存储库
  • 在插件文件夹内运行npm install
  • 运行 yarn build 或 yarn dev 来编译资产

标签: wordpresswoocommerce

解决方案


我发现为什么它对我不起作用。我添加了以下代码以使 ACF 位于 Product Data 元框中:

$("#canopytour_options .options_group").append($("#acf-group_5b8804f5a1b49"));

显然这不能正常工作,因为它不会保存所选产品的类型,请始终保持“简单”。

谢谢你的时间。


推荐阅读