自定义产品类型不允许在Woocommerce中更新

问题描述 投票:0回答:1

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

你可以在这里看到问题的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定制,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来编译资产
wordpress woocommerce
1个回答
0
投票

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

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

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

谢谢你的时间。

© www.soinside.com 2019 - 2024. All rights reserved.