我正在尝试创建自定义产品类型,但是在更新以前创建的产品时,不会对其进行更新。例如:如果我要更新的产品是简单类型,当我将其更改为自定义产品类型并保存时,它仍保持为简单类型,而不是我的自定义产品类型。
您可以在此处查看问题的图像:https://media.giphy.com/media/5z83HMwdPy5twyUOGT/giphy.gif
目前,我有3.4.5版的Woocommerce和5.0-alpha-43406版的WordPress。
接下来,我留下用于生成个性化产品类型的代码:
WC_Product_canopytour.php
const req = require('tinyreq');
const start = '<mark>'; const end = '</mark>';
// const start = '<b>'; const end = '</b>';
// const start = '<strong>'; const end = '</strong>';
req('https://en.wikipedia.org/wiki/Language_code', (err, body) => {
if (err) { return console.log(err); }
body.split('<body')[1].split(start).map(textBold => {
if(textBold.includes(end)){
console.log(textBold.split(end)[0]);
console.log('────────────────────');
}
});
});
class-woocommerce-custom-product.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';
}
}
class-wcb.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;
}
使用此代码,将自定义产品的类型添加到产品信息选择中。选择创建的产品类型时,我还可以看到“定制”选项卡。但是,当保存选择的产品类型时,不会保存该产品,它会返回到初始值。
我在github中有我插件的源代码,如果可以的话 视图,也许我的代码有问题: https://github.com/jesus997/Woocommerce-Canopy-Booking
启动项目的步骤:
- 克隆存储库
- 在插件文件夹中运行
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' ); } }
- 运行纱线构建或纱线开发人员来编译资产
答案 0 :(得分:0)
我发现了为什么它对我不起作用。我添加了以下代码,以使ACF位于“产品数据”元框中:
$("#canopytour_options .options_group").append($("#acf-group_5b8804f5a1b49"));
显然,这不能正常工作,因为它不能保存所选产品的类型,请始终保持“简单”。
感谢您的时间。