Php 显示交叉销售单产品自定义选项卡
我试图在单个产品页面的自定义选项卡中显示WooCommerce交叉销售(类似于“评论”和“附加信息”选项卡)。为此,我尝试使用WooCommerce功能:Php 显示交叉销售单产品自定义选项卡,php,wordpress,woocommerce,tabs,product,Php,Wordpress,Woocommerce,Tabs,Product,我试图在单个产品页面的自定义选项卡中显示WooCommerce交叉销售(类似于“评论”和“附加信息”选项卡)。为此,我尝试使用WooCommerce功能: woocommerce_cross_sell_display(); 但它不起作用(我没有收到任何错误和视觉效果) 以下是我迄今为止所做的尝试: //Add custom tabs filter add_filter('woocommerce_product_tabs', 'add_new_default_product_tab' ); fu
woocommerce_cross_sell_display();
但它不起作用(我没有收到任何错误和视觉效果)
以下是我迄今为止所做的尝试:
//Add custom tabs filter
add_filter('woocommerce_product_tabs', 'add_new_default_product_tab' );
function add_new_default_product_tab( $tabs ) {
global $product;
// set the new priority to the "reviews" tab
$tabs['reviews']['priority'] = 20;
// gets the value to use as the title and slug of the new tab
$custom_tab_title = "אביזרים";
$custom_tab_title2 = "אביזרים משלימים";
// if the custom field is set, it adds the new tab
if ( ! empty($custom_tab_title) ) {
$tabs['awp-' . sanitize_title('props')] = array(
'title' => 'אביזרים',
'callback' => 'awp_custom_woocommerce_tabs',
'priority' => 5
);
}
if ( ! empty($custom_tab_title) ) {
$tabs['awp-' . sanitize_title('additional-props')] = array(
'title' => 'אביזרים משלימים',
'callback' => 'awp_custom_woocommerce_tabs2',
'priority' => 10
);
}
return $tabs;
}
//Callback to display upsells (WORKS)
function awp_custom_woocommerce_tabs($key, $tab) {
woocommerce_upsell_display( 3,3 );
}
//Callback to display cross sells (Doesn't work)
function awp_custom_woocommerce_tabs2($key, $tab) {
woocommerce_cross_sell_display();
}
它是有线的,因为追加销售工作正常,但交叉销售(基本上是一样的)不起作用
如何在单个产品页面自定义选项卡中显示交叉销售
检查以下代码
//Add custom tabs filter
add_filter('woocommerce_product_tabs', 'add_new_default_product_tab' );
function add_new_default_product_tab( $tabs ) {
global $product;
// set the new priority to the "reviews" tab
$tabs['reviews']['priority'] = 20;
// gets the value to use as the title and slug of the new tab
$custom_tab_title = "אביזרים";
$custom_tab_title2 = "אביזרים משלימים";
// if the custom field is set, it adds the new tab
if ( ! empty($custom_tab_title) ) {
$tabs['awp-' . sanitize_title('props')] = array(
'title' => 'אביזרים',
'callback' => 'awp_custom_woocommerce_tabs',
'priority' => 5
);
}
if ( ! empty($custom_tab_title) ) {
$tabs['awp-' . sanitize_title('additional-props')] = array(
'title' => 'אביזרים משלימים',
'callback' => 'awp_custom_woocommerce_tabs2',
'priority' => 10
);
}
return $tabs;
}
//Callback to display upsells (WORKS)
function awp_custom_woocommerce_tabs($key, $tab) {
?>
<div class="related">
<?php
// Customised: Show cross-sells on single product pages, under the attributes and short description
global $post;
$crosssells = get_post_meta( $post->ID, '_upsell_ids',true);
if($crosssells) {
echo '<h2>Up Sell products</h2>';
echo '<ul>';
foreach ($crosssells as $item) {
// WP_Query arguments
$args = array (
'p' => $item,
'post_type' => array( 'product' ),
'post_status' => array( 'publish' ),
);
// The Query
$crosssells_products = new WP_Query( $args );
// The Loop
if ( $crosssells_products->have_posts() ) {
while ( $crosssells_products->have_posts() ) {
$crosssells_products->the_post();
?>
<li><a href="<?php the_permalink();?>"><?php the_title();?></a></li>
<?php
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
}
echo '</ul>';
}
?>
</div>
<?php
}
//Callback to display cross sells (Doesn't work)
function awp_custom_woocommerce_tabs2($key, $tab) {
?>
<div class="related">
<?php
// Customised: Show cross-sells on single product pages, under the attributes and short description
global $post;
$upsell = get_post_meta( $post->ID, '_crosssell_ids',true);
if($upsell) {
echo '<h2>Cross Sell products</h2>';
echo '<ul>';
foreach ($upsell as $item) {
// WP_Query arguments
$args = array(
'p' => $item,
'post_type' => array( 'product' ),
'post_status' => array( 'publish' ),
);
// The Query
$upsell_products = new WP_Query( $args );
// The Loop
if ( $upsell_products->have_posts() ) {
while ( $upsell_products->have_posts() ) {
$upsell_products->the_post();
?>
<li><a href="<?php the_permalink();?>"><?php the_title();?></a></li>
<?php
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
}
echo '</ul>';
}
?>
</div>
<?php
}
//添加自定义选项卡过滤器
添加过滤器('woocommerce\u product\u tab'、'add\u new\u default\u product\u tab');
功能添加\新\默认\产品\选项卡($tabs){
全球$产品;
//将新优先级设置为“审阅”选项卡
$tabs['reviews']['priority']=20;
//获取用作新选项卡的标题和slug的值
$custom_tab_title=“אביזרים”;
$custom_tab_title2=“אבירםש㪡ם”;
//如果设置了自定义字段,则会添加新选项卡
如果(!空($custom_tab_title)){
$tabs['awp-'.清理标题('props')]=数组(
“标题”=>“标题”,
“回调”=>“awp\u自定义\u商业\u选项卡”,
“优先级”=>5
);
}
如果(!空($custom_tab_title)){
$tabs['awp-'.清理标题('additional-props')]=array(
“标题”=>“标题”,
“回调”=>“awp\u自定义\u商业\u选项卡2”,
“优先级”=>10
);
}
返回$tabs;
}
//回调以显示向上销售(工作)
功能awp_自定义_商业_选项卡($key,$tab){
?>
产品交叉销售适用于购物车,需要在中进行一些更改,以使其在产品单页中工作
可以在自定义函数中从该函数克隆代码并更改:
与
因此,您的最终代码将是:
// Add custom tabs filter
add_filter('woocommerce_product_tabs', 'add_new_default_product_tab' );
function add_new_default_product_tab( $tabs ) {
global $product;
// set the new priority to the "reviews" tab
$tabs['reviews']['priority'] = 20;
// gets the value to use as the title and slug of the new tab
$custom_tab_title = "אביזרים";
$custom_tab_title2 = "אביזרים משלימים";
// if the custom field is set, it adds the new tab
if ( ! empty($custom_tab_title) ) {
$tabs['awp-' . sanitize_title('props')] = array(
'title' => 'אביזרים',
'callback' => 'woocommerce_upsell_display_in_tab',
'priority' => 5
);
}
if ( ! empty($custom_tab_title) ) {
$tabs['awp-' . sanitize_title('additional-props')] = array(
'title' => 'אביזרים משלימים',
'callback' => 'woocommerce_cross_sell_display_in_tab',
'priority' => 10
);
}
return $tabs;
}
// Callback to display upsells
function woocommerce_upsell_display_in_tab() {
woocommerce_upsell_display( 3, 3 );
}
// Callback to display cross sells
function woocommerce_cross_sell_display_in_tab( $limit = 3, $columns = 3, $orderby = 'rand', $order = 'desc' ) {
global $product;
$cross_sells = array_filter( array_map( 'wc_get_product', $product->get_cross_sell_ids() ), 'wc_products_array_filter_visible' );
wc_set_loop_prop( 'name', 'cross-sells' );
wc_set_loop_prop( 'columns', apply_filters( 'woocommerce_cross_sells_columns', $columns ) );
// Handle orderby and limit results.
$orderby = apply_filters( 'woocommerce_cross_sells_orderby', $orderby );
$order = apply_filters( 'woocommerce_cross_sells_order', $order );
$cross_sells = wc_products_array_orderby( $cross_sells, $orderby, $order );
$limit = apply_filters( 'woocommerce_cross_sells_total', $limit );
$cross_sells = $limit > 0 ? array_slice( $cross_sells, 0, $limit ) : $cross_sells;
wc_get_template(
'cart/cross-sells.php',
array(
'cross_sells' => $cross_sells,
// Not used now, but used in previous version of up-sells.php.
'posts_per_page' => $limit,
'orderby' => $orderby,
'columns' => $columns,
)
);
}
代码放在活动子主题(或活动主题)的functions.php文件中。经过测试,可以正常工作
$cross_sells = array_filter( array_map( 'wc_get_product', $product->get_cross_sell_ids() ), 'wc_products_array_filter_visible' );
// Add custom tabs filter
add_filter('woocommerce_product_tabs', 'add_new_default_product_tab' );
function add_new_default_product_tab( $tabs ) {
global $product;
// set the new priority to the "reviews" tab
$tabs['reviews']['priority'] = 20;
// gets the value to use as the title and slug of the new tab
$custom_tab_title = "אביזרים";
$custom_tab_title2 = "אביזרים משלימים";
// if the custom field is set, it adds the new tab
if ( ! empty($custom_tab_title) ) {
$tabs['awp-' . sanitize_title('props')] = array(
'title' => 'אביזרים',
'callback' => 'woocommerce_upsell_display_in_tab',
'priority' => 5
);
}
if ( ! empty($custom_tab_title) ) {
$tabs['awp-' . sanitize_title('additional-props')] = array(
'title' => 'אביזרים משלימים',
'callback' => 'woocommerce_cross_sell_display_in_tab',
'priority' => 10
);
}
return $tabs;
}
// Callback to display upsells
function woocommerce_upsell_display_in_tab() {
woocommerce_upsell_display( 3, 3 );
}
// Callback to display cross sells
function woocommerce_cross_sell_display_in_tab( $limit = 3, $columns = 3, $orderby = 'rand', $order = 'desc' ) {
global $product;
$cross_sells = array_filter( array_map( 'wc_get_product', $product->get_cross_sell_ids() ), 'wc_products_array_filter_visible' );
wc_set_loop_prop( 'name', 'cross-sells' );
wc_set_loop_prop( 'columns', apply_filters( 'woocommerce_cross_sells_columns', $columns ) );
// Handle orderby and limit results.
$orderby = apply_filters( 'woocommerce_cross_sells_orderby', $orderby );
$order = apply_filters( 'woocommerce_cross_sells_order', $order );
$cross_sells = wc_products_array_orderby( $cross_sells, $orderby, $order );
$limit = apply_filters( 'woocommerce_cross_sells_total', $limit );
$cross_sells = $limit > 0 ? array_slice( $cross_sells, 0, $limit ) : $cross_sells;
wc_get_template(
'cart/cross-sells.php',
array(
'cross_sells' => $cross_sells,
// Not used now, but used in previous version of up-sells.php.
'posts_per_page' => $limit,
'orderby' => $orderby,
'columns' => $columns,
)
);
}