php - 使用相同全局属性的不同产品需要为该全局属性显示不同的标签
问题描述
所以典型的例子是家具网店。
您有一个产品,它是一张桌子,腿和顶部有 2 种饰面。
该表有 2 个全局属性。
顶部饰面(木材 1、金属 1、金属 2、混凝土)
腿饰面(木材 1、金属 1、金属 2、混凝土)
家具制造商的材料(饰面)清单有限。所以我通常为那个特定的制造商创建一个单一的全局材料属性。属性的名称类似于“ManufacturerName Finishes”
如您所知,在同一产品上,您不能将 2 倍相同的全局属性应用于该家具上的不同组件以产生变化。所以我不得不手动复制该全局属性及其所有内容。也许您也可以通过编程方式帮助我。
在重复部分之后,我将拥有:
制造商名称饰面
制造商名称饰面2
比我开始创建和展示产品:
标题> 表格
attr 1 用于顶部饰面> 制造商名称饰面(木材 1、金属 1、金属 2、混凝土)
腿饰面的属性 2> 制造商名称饰面 2(木材 1、金属 1、金属 2、混凝土)
但不能看到“制造商名称完成”名称:)
我需要看看
标题> 表格
attr 1 用于顶部饰面> 顶部饰面(木材 1、金属 1、金属 2、混凝土)
腿部饰面的 attr 2 > 腿部饰面(木材 1、金属 1、金属 2、混凝土)
所以我开始在互联网上搜索不同的代码,我成功地制作了一个可以满足我需求的代码。它在属性部分的每个产品的库存页面中创建一些自定义字段,我在其中为用于变体的属性标签放置自定义文本。
我需要您的帮助来查看我的代码,因为我有一个错误,我将在这里描述:在我在购物车中添加产品后,页面不会重新加载启用此代码的产品页面。所以首先展示产品。我看到了属性的自定义标签(耶!!)。我选择一个变体。我将变体添加到购物车。页面开始自动刷新,但只出现菜单。产品在购物车中。当我禁用我的片段时,一切正常。所以我有一个错误。只要该产品在购物车中,即使我从主菜单再次访问它,我也无法再次看到产品页面(只有带有菜单的页面)。如果我从购物车中删除产品,我可以再次访问产品页面。
// Add custom fields in admin product edit pages - inventory tab - attributes
//https://stackoverflow.com/questions/63588126/setting-a-default-value-for-a-woocommerce-product-field-to-all-products
add_action( 'woocommerce_product_options_attributes', 'add_product_custom_attribute_titles_fields', 20 );
function add_product_custom_attribute_titles_fields() {
$nr_max_atribute = 2;//AICI PUTEM ALOCA CATE VREM. ATENTIE SA CORELAM CU $nr_max_atribute din urmatoarele 2 functii
for($i=0;$i<$nr_max_atribute;$i++){
$ii=$i+1;
woocommerce_wp_text_input( array(
'id' => '_custom_attribute'.$ii.'_title_text',
'type' => 'text',
'label' => __( 'Attribute '.$ii.' Custom Title', 'woocommerce' ),
'description' => __( 'Attribute '.$ii.' Custom Title text. Add a custom Atribute '.$ii.' Title text to be displayed as attribute name in product page.The custom title will go ONLY FOR ATTRIBUTES THAT HAVE "Used for variations" checkbox ENABLED', 'woocommerce' ),
'desc_tip' => true,
'value' => $value
) );
}
add_action( 'woocommerce_process_product_meta', 'save_product_custom_attribute_titles_fields', 20, 1 );
}
//_______________________________________________________________________________________________________
// Save the custom field value from admin product edit pages - inventory tab
add_action( 'woocommerce_process_product_meta', 'save_product_custom_attribute_titles_fields', 20, 1 );
function save_product_custom_attribute_titles_fields( $product_id ) {
if (array_key_exists('_custom_attribute1_title_text', $_POST)) {
$nr_max_atribute = 2;//AICI PUTEM ALOCA CATE VREM. ATENTIE SA CORELAM CU $nr_max_atribute din urmatoarea functie
for($i=0;$i<$nr_max_atribute;$i++){
$ii=$i+1;
update_post_meta($product_id, '_custom_attribute'.$ii.'_title_text', sanitize_text_field( $_POST['_custom_attribute'.$ii.'_title_text']));
}
}
}
//_______________________________________________________________________________________________________
//Put saved custom labels on product page
//https://gist.github.com/WPprodigy/5bfc0f8523ad3fd9dd5e59f174fb666e
//https://stackoverflow.com/questions/47542209/get-product-attributes-in-woocommerce-3
add_action( 'woocommerce_attribute_label', 'wc_change_attribute_label', 20, 3 );
function wc_change_attribute_label( $label, $name, $product ) {
if ( is_product()){
global $product;
$attributes = $product->get_attributes();
$variation_attributes = array();
foreach ( $attributes as $attribute ):
$attribute_data = $attribute->get_data();
if (($attribute_data['is_variation']===1) && $attribute_data['is_visible']===1){
$variation_attributes[]=$attribute_data;
}
endforeach;
$nr_max_atribute = 2;//AICI PUTEM ALOCA CATE VREM. ATENTIE SA CORELAM CU $nr_max_atribute din cele doua functii precedente
if (count($variation_attributes)<=$nr_max_atribute){
$nr_alocari_titluri_custom = count($variation_attributes);
} else {
$nr_alocari_titluri_custom = $nr_max_atribute;
}
for($i=0;$i<$nr_alocari_titluri_custom;$i++){
if (($variation_attributes[$i]["name"] === $name ) ) {
$ii=$i+1;
$custom_label = get_post_meta( $product->get_id(), '_custom_attribute'.$ii.'_title_text', true );
$label = $custom_label;
}
}
}
// Return the label, untouched if none of the above conditions were true
return $label;
}
解决方案
推荐阅读
- opencv - 在 OpenCV C++ 中使用 SimpleBlobDetector 时如何处理紧密重叠的关键点?
- ruby - 区分大小写的替换
- javascript - 为什么我不能在 JavaScript 中做 array[-1]?
- python - 除了计算之外,使用现有数据框选择性地构建新数据框
- javascript - Vue/AXIOS 没有完全下载
- java - 数据库 [默认] 未找到驱动程序 - Playframework 1.4.x 中的 MySQL
- listview - 如何在 Flutter 的同一屏幕上沿 ListView 显示小部件?
- python - 如果在 MainProcess 中执行 `glfw.create_window`,为什么多进程 glfw 应用程序会停止?
- azure-active-directory - Azure:服务主体 ID 与应用程序 ID
- android - 以编程方式取消(和隐藏)Android 通知