我试图通过创建一个插件向WooCommerce添加一个新的自定义产品类型。我已经能够在管理界面中获得可用的新产品类型,并且能够自定义添加/编辑产品页面以进行一些自定义设置。
到目前为止,我所不能做的是,当您访问站点上的产品页面时,如何准确地挂钩/修改页面输出。
我找到了几页(here 和here) 这在主题上有一些指导,但我似乎无法根据我的产品类型(底部的代码)来实现挂钩。也许我的名称空间(函数或挂钩名称)有误,这就是为什么它没有被捕获?
插件名称:WooCommerce系列产品
插件文件夹:woocommerce familyproduct
产品类别:WC\\U Product\\U系列
产品类型:wcfp\\u family\\u Product
以下是我开始使用插件添加自定义产品类型时使用的参考资料:here, here 和here
到目前为止,我的插件代码如下所示(一些钩子在测试中被注释掉了):
<?php
/*
https://wisdmlabs.com/blog/create-product-type-custom-settings-woocommerce/
http://jeroensormani.com/adding-a-custom-woocommerce-product-type/
// Example showing a product select drop down menu on the Settings page of the custom product type:
https://stackoverflow.com/questions/30973651/add-product-search-field-in-woo-commerce-product-page
*/
// add a product type
add_filter( \'product_type_selector\', \'wcfp_add_family_product_type\' );
function wcfp_add_family_product_type( $types ){
$types[ \'wcfp_family_product\' ] = __( \'Family Product\' );
return $types;
}
add_action( \'plugins_loaded\', \'wcfp_create_family_product_type\' );
function wcfp_create_family_product_type(){
// declare the product class
class WC_Product_Family extends WC_Product{
public function __construct( $product ) {
$this->product_type = \'wcfp_family_product\';
parent::__construct( $product );
// add additional functions here
}
}
}
// add the settings under ‘General’ sub-menu
add_action( \'woocommerce_product_options_general_product_data\', \'wcfp_add_custom_settings\' );
function wcfp_add_custom_settings() {
global $woocommerce, $post;
echo \'<div class="options_group hidden show_if_wcfp_family_product">\';
?>
<p class="form-field product_field_type">
<label for="wcfp_family_product_ids"><?php _e( \'Component Products:\', \'woocommerce\' ); ?></label>
<input type="hidden" class="wc-product-search" style="width: 50%;" id="wcfp_family_product_ids" name="wcfp_family_product_ids" data-placeholder="<?php esc_attr_e( \'Search for a product…\', \'woocommerce\' ); ?>" data-action="woocommerce_json_search_products" data-multiple="true" data-exclude="<?php echo intval( $post->ID ); ?>" data-selected="<?php
$product_ids = array_filter( array_map( \'absint\', (array) get_post_meta( $post->ID, \'_wcfp_family_product_ids\', true ) ) );
$json_ids = array();
foreach ( $product_ids as $product_id ) {
$product = wc_get_product( $product_id );
if ( is_object( $product ) ) {
$json_ids[ $product_id ] = wp_kses_post( html_entity_decode( $product->get_formatted_name(), ENT_QUOTES, get_bloginfo( \'charset\' ) ) );
}
}
echo esc_attr( json_encode( $json_ids ) );
?>" value="<?php echo implode( \',\', array_keys( $json_ids ) ); ?>" /> <?php echo wc_help_tip( __( \'Select component parts to display on the product page.\', \'woocommerce\' ) ); ?>
</p>
<?php
echo \'</div>\';
}
add_action( \'woocommerce_process_product_meta\', \'wcfp_save_custom_settings\' );
function wcfp_save_custom_settings( $post_id ){
//Save the collection of products
$my_product_ids = isset( $_POST[\'wcfp_family_product_ids\'] ) ? array_filter( array_map( \'intval\', explode( \',\', $_POST[\'wcfp_family_product_ids\'] ) ) ) : array();
update_post_meta( $post_id, \'_wcfp_family_product_ids\', $my_product_ids );
}
/**
* Add a custom product tab.
*/
function wcfp_custom_product_tabs( $tabs) {
$tabs[\'rental\'] = array(
\'label\' => __( \'Family\', \'woocommerce\' ),
\'target\' => \'family_options\',
\'class\' => array( \'show_if_wcfp_family_product\'),
);
return $tabs;
}
//add_filter( \'woocommerce_product_data_tabs\', \'wcfp_custom_product_tabs\' );
/**
* Hide Attributes data panel.
*/
function wcfp_hide_attributes_data_panel( $tabs) {
// Other default values for \'attribute\' are; general, inventory, shipping, linked_product, variations, advanced
//$tabs[\'general\'][\'class\'][] = \'hide_if_wcfp_family_product\';
$tabs[\'inventory\'][\'class\'][] = \'hide_if_wcfp_family_product\';
$tabs[\'shipping\'][\'class\'][] = \'hide_if_wcfp_family_product\';
$tabs[\'linked_product\'][\'class\'][] = \'hide_if_wcfp_family_product\';
$tabs[\'attribute\'][\'class\'][] = \'hide_if_wcfp_family_product\';
$tabs[\'advanced\'][\'class\'][] = \'hide_if_wcfp_family_product\';
return $tabs;
}
//add_filter( \'woocommerce_product_data_tabs\', \'wcfp_hide_attributes_data_panel\' );
我已经创建/编辑了一个产品,您可以选择哪些产品构成“系列”产品。现在,我将继续创建一个自定义产品页面,该页面将更改客户查看产品时呈现的内容。根据我的搜索,我相信这将是“add\\u to\\u cart”挂钩。
现在,假设“add\\u to\\u cart”函数是我想要钩住的,我在插件中创建了一个模板文件夹,然后用PHP脚本和代码钩住add\\u to\\u cart函数,如下所示:
function woocommerce_wcfp_family_product_add_to_cart() {
wc_get_template( WP_PLUGIN_DIR.\'/woocommerce-familyproduct/templates/wcfp_family_product.php\' );
}
add_action( \'woocommerce_wcfp_family_product_add_to_cart\', \'woocommerce_wcfp_family_product_add_to_cart\' );
在模板文件中,我只有一些用于测试的原始HTML,比如H5标记,所以我知道钩子正在启动。
那么,我做错了什么?
最合适的回答,由SO网友:Nick Bork 整理而成
我的问题的症结在于类名/产品类型不匹配。我调用了WC\\u Product\\u Family类,但使用了“wcfp\\u Family\\u Product”的产品类型。
我重构了代码,为add\\u to\\u购物车添加了挂钩,并获得了一个粗略的工作示例:
<?php
/*
https://wisdmlabs.com/blog/create-product-type-custom-settings-woocommerce/
http://jeroensormani.com/adding-a-custom-woocommerce-product-type/
// Example showing a product select drop down menu on the Settings page of the custom product type:
http://stackoverflow.com/questions/30973651/add-product-search-field-in-woo-commerce-product-page
*/
// add a product type
add_filter( \'product_type_selector\', \'wcfp_add_family_product_type\' );
function wcfp_add_family_product_type( $types ){
$types[ \'familyproduct\' ] = __( \'Family Product\' );
return $types;
}
add_action( \'plugins_loaded\', \'wcfp_create_family_product_type\' );
function wcfp_create_family_product_type(){
// declare the product class
class WC_Product_FamilyProduct extends WC_Product{
public function __construct( $product ) {
$this->product_type = \'familyproduct\';
parent::__construct( $product );
// add additional functions here
}
}
}
// add the settings under ‘General’ sub-menu
add_action( \'woocommerce_product_options_general_product_data\', \'wcfp_add_custom_settings\' );
function wcfp_add_custom_settings() {
global $woocommerce, $post;
echo \'<div class="options_group hidden show_if_familyproduct">\';
?>
<p class="form-field product_field_type">
<label for="familyproduct_ids"><?php _e( \'Component Products:\', \'woocommerce\' ); ?></label>
<input type="hidden" class="wc-product-search" style="width: 50%;" id="familyproduct_ids" name="familyproduct_ids" data-placeholder="<?php esc_attr_e( \'Search for a product…\', \'woocommerce\' ); ?>" data-action="woocommerce_json_search_products" data-multiple="true" data-exclude="<?php echo intval( $post->ID ); ?>" data-selected="<?php
$product_ids = array_filter( array_map( \'absint\', (array) get_post_meta( $post->ID, \'_familyproduct_ids\', true ) ) );
$json_ids = array();
foreach ( $product_ids as $product_id ) {
$product = wc_get_product( $product_id );
if ( is_object( $product ) ) {
$json_ids[ $product_id ] = wp_kses_post( html_entity_decode( $product->get_formatted_name(), ENT_QUOTES, get_bloginfo( \'charset\' ) ) );
}
}
echo esc_attr( json_encode( $json_ids ) );
?>" value="<?php echo implode( \',\', array_keys( $json_ids ) ); ?>" /> <?php echo wc_help_tip( __( \'Select component parts to display on the product page.\', \'woocommerce\' ) ); ?>
</p>
<?php
echo \'</div>\';
}
add_action( \'woocommerce_process_product_meta\', \'wcfp_save_custom_settings\' );
function wcfp_save_custom_settings( $post_id ){
//Save the collection of products
$my_product_ids = isset( $_POST[\'familyproduct_ids\'] ) ? array_filter( array_map( \'intval\', explode( \',\', $_POST[\'familyproduct_ids\'] ) ) ) : array();
update_post_meta( $post_id, \'_familyproduct_ids\', $my_product_ids );
}
/**
* Add a custom product tab.
*/
function wcfp_custom_product_tabs( $tabs) {
$tabs[\'rental\'] = array(
\'label\' => __( \'Family\', \'woocommerce\' ),
\'target\' => \'family_options\',
\'class\' => array( \'show_if_familyproduct\'),
);
return $tabs;
}
//add_filter( \'woocommerce_product_data_tabs\', \'wcfp_custom_product_tabs\' );
/**
* Hide Attributes data panel.
*/
function wcfp_hide_attributes_data_panel( $tabs) {
// Other default values for \'attribute\' are; general, inventory, shipping, linked_product, variations, advanced
//$tabs[\'general\'][\'class\'][] = \'hide_if_familyproduct\';
$tabs[\'inventory\'][\'class\'][] = \'hide_if_familyproduct\';
$tabs[\'shipping\'][\'class\'][] = \'hide_if_familyproduct\';
$tabs[\'linked_product\'][\'class\'][] = \'hide_if_familyproduct\';
$tabs[\'attribute\'][\'class\'][] = \'hide_if_familyproduct\';
$tabs[\'advanced\'][\'class\'][] = \'hide_if_familyproduct\';
return $tabs;
}
//add_filter( \'woocommerce_product_data_tabs\', \'wcfp_hide_attributes_data_panel\' );
add_action( \'woocommerce_familyproduct_add_to_cart\', \'familyproduct_add_to_cart\' );
function familyproduct_add_to_cart() {
echo \'MY CUSTOM ADD TO CART HOOK\';
die();
}