Gam*_*ery 2 php wordpress woocommerce price product-variations
我正在使用此代码来获取可变产品选项
$terms = wc_get_product_terms( $bundle_product_id, $name, array( 'fields' => 'all' ) );
foreach ( $terms as $term ) {
if ( !in_array( $term->slug, $options ) ) {
continue;
}
echo '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $selected_value ), sanitize_title( $term->slug ), false ) . '>' . apply_filters( 'woocommerce_variation_option_name', $term->name ) . '</option>';
}
Run Code Online (Sandbox Code Playgroud)
\n\n\n仅当您在变量产品中设置的变体只有一种产品属性时(因此只有一个下拉列表),它才有效。
\n
如果您的可变产品中有多个下拉菜单,由于变体是不同产品属性值的组合,因此它不会按逻辑工作。
\n\n因此,以下代码将在唯一的产品属性下拉列表中显示产品变体价格:
\n\n// Utility function to get the price of a variation from it\'s attribute value\nfunction get_the_variation_price_html( $product, $name, $term_slug ){\n foreach ( $product->get_available_variations() as $variation ){\n if($variation[\'attributes\'][$name] == $term_slug ){\n return strip_tags( $variation[\'price_html\'] );\n }\n }\n}\n\n// Add the price to the dropdown options items.\nadd_filter( \'woocommerce_dropdown_variation_attribute_options_html\', \'show_price_in_attribute_dropdown\', 10, 2);\nfunction show_price_in_attribute_dropdown( $html, $args ) {\n // Only if there is a unique variation attribute (one dropdown)\n if( sizeof($args[\'product\']->get_variation_attributes()) == 1 ) :\n\n $options = $args[\'options\'];\n $product = $args[\'product\'];\n $attribute = $args[\'attribute\'];\n $name = $args[\'name\'] ? $args[\'name\'] : \'attribute_\' . sanitize_title( $attribute );\n $id = $args[\'id\'] ? $args[\'id\'] : sanitize_title( $attribute );\n $class = $args[\'class\'];\n $show_option_none = $args[\'show_option_none\'] ? true : false;\n $show_option_none_text = $args[\'show_option_none\'] ? $args[\'show_option_none\'] : __( \'Choose an option\', \'woocommerce\' );\n\n if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {\n $attributes = $product->get_variation_attributes();\n $options = $attributes[ $attribute ];\n }\n\n $html = \'<select id="\' . esc_attr( $id ) . \'" class="\' . esc_attr( $class ) . \'" name="\' . esc_attr( $name ) . \'" data-attribute_name="attribute_\' . esc_attr( sanitize_title( $attribute ) ) . \'" data-show_option_none="\' . ( $show_option_none ? \'yes\' : \'no\' ) . \'">\';\n $html .= \'<option value="">\' . esc_html( $show_option_none_text ) . \'</option>\';\n\n if ( ! empty( $options ) ) {\n if ( $product && taxonomy_exists( $attribute ) ) {\n $terms = wc_get_product_terms( $product->get_id(), $attribute, array( \'fields\' => \'all\' ) );\n\n foreach ( $terms as $term ) {\n if ( in_array( $term->slug, $options ) ) {\n // Get and inserting the price\n $price_html = get_the_variation_price_html( $product, $name, $term->slug );\n $html .= \'<option value="\' . esc_attr( $term->slug ) . \'" \' . selected( sanitize_title( $args[\'selected\'] ), $term->slug, false ) . \'>\' . esc_html( apply_filters( \'woocommerce_variation_option_name\', $term->name ) . \' :::\xc2\xa0\' . $price_html ) . \'</option>\';\n }\n }\n } else {\n foreach ( $options as $option ) {\n $selected = sanitize_title( $args[\'selected\'] ) === $args[\'selected\'] ? selected( $args[\'selected\'], sanitize_title( $option ), false ) : selected( $args[\'selected\'], $option, false );\n // Get and inserting the price\n $price_html = get_the_variation_price_html( $product, $name, $term->slug );\n $html .= \'<option value="\' . esc_attr( $option ) . \'" \' . $selected . \'>\' . esc_html( apply_filters( \'woocommerce_variation_option_name\', $option ) . \' :::\xc2\xa0\' . $price_html ) . \'</option>\';\n }\n }\n }\n $html .= \'</select>\';\n\n endif;\n\n return $html;\n}\nRun Code Online (Sandbox Code Playgroud)\n\n代码位于活动子主题(或活动主题)的 function.php 文件中。经过测试并有效。
\n\n\n| 归档时间: |
|
| 查看次数: |
7575 次 |
| 最近记录: |