我正在尝试创建一种新的产品类型,问题是现在它是基于$ product的,这似乎使其无法销售或其他原因,导致在产品页面上我没有添加到购物车,然后管理员产品页面我没有包含价格和库存标签的常规标签。
我添加了我在theme / functions.php中拥有的代码
如何使这种自定义类型的产品可销售:/
谢谢
add_action( 'init', 'register_test_product_type' );
function register_test_product_type() {
class WC_Product_Test_Product extends WC_Product {
public function __construct( $product ) {
$this->product_type = 'test_product';
parent::__construct( $simple_product );
}
}
}
add_filter( 'product_type_selector', 'add_test_product_type' );
function add_test_product_type( $types ){
$types[ 'test' ] = __( 'Test product', 'test_product' );
return $types;
}
add_filter( 'woocommerce_product_data_tabs', 'test_product_tab' );
function test_product_tab( $tabs) {
$tabs['test'] = array(
'label' => __( 'Tests', 'test_product' ),
'target' => 'test_product_options',
'class' => 'show_if_test_product',
);
return $tabs;
}
add_action( 'woocommerce_process_product_meta', 'save_test_product_settings' );
function save_test_product_settings( $post_id ){
$test_product_info = $_POST['test_product_info'];
if( !empty( $test_product_info ) ) {
update_post_meta( $post_id, 'test_product_info', esc_attr( $test_product_info ) );
}
}
add_action( 'woocommerce_single_product_summary', 'test_product_front' );
function test_product_front () {
global $product;
if ( 'test' == $product->get_type() ) {
echo( get_post_meta( $product->get_id(), 'test_product_info' )[0] );
}
}
function remove_linked_products($tabs){
unset($tabs['shipping']);
return($tabs);
}
add_filter('woocommerce_product_data_tabs', 'remove_linked_products', 10, 1);
如果给我这个错误,新代码可以节省罚款,而且除woocommerce以外的其他价格都可以节省
Fatal error: Uncaught Error: Call to a member function get_price() on null in C:\xampp\htdocs\fineland\wp-content\themes\crane\functions.php:1488 Stack trace: #0 C:\xampp\htdocs\fineland\wp-includes\class-wp-hook.php(286): demo_product_tab_product_tab_content('') #1 C:\xampp\htdocs\fineland\wp-includes\class-wp-hook.php(310): WP_Hook->apply_filters('', Array) #2 C:\xampp\htdocs\fineland\wp-includes\plugin.php(465): WP_Hook->do_action(Array) #3 C:\xampp\htdocs\fineland\wp-content\plugins\woocommerce\includes\admin\meta-boxes\views\html-product-data-panel.php(53): do_action('woocommerce_pro...') #4 C:\xampp\htdocs\fineland\wp-content\plugins\woocommerce\includes\admin\meta-boxes\class-wc-meta-box-product-data.php(35): include('C:\\xampp\\htdocs...') #5 C:\xampp\htdocs\fineland\wp-admin\includes\template.php(1306): WC_Meta_Box_Product_Data::output(Object(WP_Post), Array) #6 C:\xampp\htdocs\fineland\wp-admin\edit-form-advanced.php(638): do_meta_boxes(Object(WP_Screen), 'normal', Object(WP_Post)) #7 C:\xampp\htdocs\fineland\wp in C:\xampp\htdocs\fineland\wp-content\themes\crane\functions.php on line 1488
新代码
add_action( 'init', 'register_demo_product_type' );
function register_demo_product_type() {
class WC_Product_Demo extends WC_Product {
public function __construct( $product ) {
$this->product_type = 'demo';
parent::__construct( $product );
}
}
}
add_filter( 'product_type_selector', 'add_demo_product_type' );
function add_demo_product_type( $types ){
$types[ 'demo' ] = __( 'Demo product', 'dm_product' );
return $types;
}
add_filter( 'woocommerce_product_data_tabs', 'demo_product_tab' );
function demo_product_tab( $tabs) {
$tabs['demo'] = array(
'label' => __( 'Test', 'dm_product' ),
'target' => 'demo_product_options',
'class' => 'show_if_demo_product',
);
unset($tabs['shipping']);
return $tabs;
}
global $product;
add_action( 'woocommerce_product_data_panels', 'demo_product_tab_product_tab_content' );
function demo_product_tab_product_tab_content() {
?><div id='demo_product_options' class='panel woocommerce_options_panel'><?php
?><div class='options_group'><?php
$products;
woocommerce_wp_text_input(
array(
'id' => '_regular_price',
'value' => $product->get_price( 'edit' ),
'label' => __( 'Regular price', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')',
'data_type' => 'price',
)
);
?></div>
</div><?php
}
add_action( 'woocommerce_process_product_meta', 'save_demo_product_settings' );
function save_demo_product_settings( $post_id ){
$_regular_price = $_POST['_regular_price'];
if( !empty( $_regular_price ) ) {
update_post_meta( $post_id, '_regular_price', esc_attr( $_regular_price ) );
}
}
add_action( 'woocommerce_single_product_summary', 'demo_product_front' );
function demo_product_front () {
global $product;
if ( 'demo' == $product->get_type() ) {
echo( get_post_meta( $product->get_id(), '_regular_price' )[0] );
}
}
答案 0 :(得分:0)
似乎您需要添加一些变量。确保声明价格,tax_status,manage_stock,stock_quantity,stock_status。这应该可以帮助您选择简单的产品,但是请确保是否更改产品类型以确保声明所有其他合适的设置。
有关完整的产品数据架构,请在此处查看,并确保您具有本地安装设置的所有必需条目。 https://github.com/woocommerce/woocommerce/wiki/Product-Data-Schema