Я пытаюсь создать пользовательский тип продукта, но при обновлении ранее созданного продукта он не обновляется. Например: если продукт, который я хочу обновить, является простым типом, когда я изменяю его на свой собственный тип продукта и сохраняю, он все равно поддерживается как простой тип вместо моего пользовательского типа продукта.
Вы можете увидеть gif проблемы здесь: https://media.giphy.com/media/5z83HMwdPy5twyUOGT/giphy.gif
В настоящее время у меня версия 3.4.5 Woocommerce и версия 5.0-alpha-43406 WordPress.
Далее я оставляю код, используемый для создания типа персонализированного продукта:
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
- Запуск сборки пряжи или разработчика пряжи для компиляции активов