我正在与WooCommerce建立WordPress多站点网络,并尝试使其成为一体,以便在开始创建产品时自动选择所有产品属性并将其添加到产品中。
我测试了这里找到的解决方案: Auto add all product attributes when adding a new product in Woocommerce
当我在单个站点上对其进行测试时,此解决方案效果很好。
但是,当我将其放在多站点网络上时,它停止正常工作。
在多站点网络上,创建新产品时,它会添加属性并正确选择复选框,但是不会为每个属性添加“值”。
理想情况下,“值”部分中可用的所有选项也会自动选择。
同样,这在单个站点实例上完美运行,但是一旦我将其复制到多站点环境,它便停止工作。
注意:我进行了一些其他自定义,以帮助简化最终用户的用户体验。这些其他自定义设置似乎没有干扰或与我在单个站点上的测试冲突。我将所有相同的自定义内容复制到了多站点网络上。由于单站点实例和多站点网络实例之间的每种自定义都是相同的,因此我认为主要问题是因为这是一个多站点网络,因此需要进行更改。以下是我为您准备的其他自定义参考:
这是我插入到functions.php文件中的最终代码以供参考:
## --- HIDE PRODUCT TABS IN WOOCOMMERCE --- ##
function remove_linked_products($tabs){
unset($tabs['general']);
unset($tabs['inventory']);
unset($tabs['shipping']);
unset($tabs['linked_product']);
unset($tabs['advanced']);
return($tabs);
}
add_filter('woocommerce_product_data_tabs', 'remove_linked_products', 10, 1);
## --- HIDE PRODUCT TYPES IN WOOCOMMERCE --- ##
add_filter( 'product_type_selector', 'remove_product_types' );
function remove_product_types( $types ){
unset( $types['grouped'] );
unset( $types['external'] );
unset( $types['simple'] );
return $types;
}
## --- AUTOMATICALLY SELECT ALL ATTRIBUTES --- ##
add_action( 'save_post', 'auto_add_product_attributes', 50, 3 );
function auto_add_product_attributes( $post_id, $post, $update ) {
## --- Checking --- ##
if ( $post->post_type != 'product') return; // Only products
// Exit if it's an autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
// Exit if it's an update
if( $update )
return $post_id;
// Exit if user is not allowed
if ( ! current_user_can( 'edit_product', $post_id ) )
return $post_id;
## --- The Settings for your product attributes --- ##
$visible = ''; // can be: '' or '1'
$variation = '1'; // can be: '' or '1'
## --- The code --- ##
// Get all existing product attributes
global $wpdb;
$attributes = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}woocommerce_attribute_taxonomies" );
$position = 0; // Auto incremented position value starting at '0'
$data = array(); // initialising (empty array)
// Loop through each exiting product attribute
foreach( $attributes as $attribute ){
// Get the correct taxonomy for product attributes
$taxonomy = 'pa_'.$attribute->attribute_name;
$attribute_id = $attribute->attribute_id;
// Get all term Ids values for the current product attribute (array)
$term_ids = get_terms(array('taxonomy' => $taxonomy, 'fields' => 'ids'));
// Get an empty instance of the WC_Product_Attribute object
$product_attribute = new WC_Product_Attribute();
// Set the related data in the WC_Product_Attribute object
$product_attribute->set_id( $attribute_id );
$product_attribute->set_name( $taxonomy );
$product_attribute->set_options( $term_ids );
$product_attribute->set_position( $position );
$product_attribute->set_visible( $visible );
$product_attribute->set_variation( $variation );
// Add the product WC_Product_Attribute object in the data array
$data[$taxonomy] = $product_attribute;
$position++; // Incrementing position
}
// Get an instance of the WC_Product object
$product = wc_get_product( $post_id );
// Set the array of WC_Product_Attribute objects in the product
$product->set_attributes( $data );
$product->save(); // Save the product
}
我不是程序员,并且通过复制其他人的自定义说明来做到这一点。任何帮助,将不胜感激。谢谢!