在 WooCommerce 单一产品自定义选项卡中显示交叉销售

Yot*_*han 5 php wordpress tabs product woocommerce

我正在尝试在单个产品页面的自定义选项卡内显示 WooCommerce 交叉销售(类似于“评论”和“其他信息”的选项卡)。为此,我尝试使用 WooCommerce 函数:

\n
woocommerce_cross_sell_display();\n
Run Code Online (Sandbox Code Playgroud)\n

但它不起作用(我没有收到错误,也没有视觉结果)。

\n

这是我到目前为止所尝试的:

\n
//Add custom tabs filter\nadd_filter(\'woocommerce_product_tabs\', \'add_new_default_product_tab\' );\nfunction add_new_default_product_tab( $tabs ) {\n\n    global $product;\n\n    // set the new priority to the "reviews" tab\n    $tabs[\'reviews\'][\'priority\'] = 20;\n\n    // gets the value to use as the title and slug of the new tab\n    $custom_tab_title = "\xd7\x90\xd7\x91\xd7\x99\xd7\x96\xd7\xa8\xd7\x99\xd7\x9d";\n    $custom_tab_title2 = "\xd7\x90\xd7\x91\xd7\x99\xd7\x96\xd7\xa8\xd7\x99\xd7\x9d \xd7\x9e\xd7\xa9\xd7\x9c\xd7\x99\xd7\x9e\xd7\x99\xd7\x9d";\n\n    // if the custom field is set, it adds the new tab\n    if ( ! empty($custom_tab_title) ) {\n        $tabs[\'awp-\' . sanitize_title(\'props\')] = array(\n            \'title\' => \'\xd7\x90\xd7\x91\xd7\x99\xd7\x96\xd7\xa8\xd7\x99\xd7\x9d\',\n            \'callback\' => \'awp_custom_woocommerce_tabs\',\n            \'priority\' => 5\n        );\n    }\n    if ( ! empty($custom_tab_title) ) {\n        $tabs[\'awp-\' . sanitize_title(\'additional-props\')] = array(\n            \'title\' => \'\xd7\x90\xd7\x91\xd7\x99\xd7\x96\xd7\xa8\xd7\x99\xd7\x9d \xd7\x9e\xd7\xa9\xd7\x9c\xd7\x99\xd7\x9e\xd7\x99\xd7\x9d\',\n            \'callback\' => \'awp_custom_woocommerce_tabs2\',\n            \'priority\' => 10\n        );\n    }\n    return $tabs;\n}\n \n    //Callback to display upsells (WORKS)\n    \n    function awp_custom_woocommerce_tabs($key, $tab) {\n         woocommerce_upsell_display( 3,3 );\n    }\n    \n    \n    //Callback to display cross sells (Doesn\'t work)\n    \n    function awp_custom_woocommerce_tabs2($key, $tab) {\n        woocommerce_cross_sell_display();\n    }\n
Run Code Online (Sandbox Code Playgroud)\n

它是有线的,因为追加销售工作正常,但交叉销售(基本上是相同的)不起作用。

\n

如何在 WooCommerce 单一产品页面自定义选项卡中显示交叉销售?

\n

链接到我的网站

\n

Loi*_*tec 6

产品交叉销售适用于购物车,需要对woocommerce_cross_sell_display()功能进行一些更改,才能使其在产品单页面中发挥作用。

\n

这可以通过在自定义函数中克隆该函数的代码并更改此行来完成:

\n
$cross_sells = array_filter( array_map( \'wc_get_product\', WC()->cart->get_cross_sells() ), \'wc_products_array_filter_visible\' );\n
Run Code Online (Sandbox Code Playgroud)\n

\n
$cross_sells = array_filter( array_map( \'wc_get_product\', $product->get_cross_sell_ids() ), \'wc_products_array_filter_visible\' );\n
Run Code Online (Sandbox Code Playgroud)\n

所以你的最终代码将是:

\n
// Add custom tabs filter\nadd_filter(\'woocommerce_product_tabs\', \'add_new_default_product_tab\' );\nfunction add_new_default_product_tab( $tabs ) {\n\n    global $product;\n\n    // set the new priority to the "reviews" tab\n    $tabs[\'reviews\'][\'priority\'] = 20;\n\n    // gets the value to use as the title and slug of the new tab\n    $custom_tab_title = "\xd7\x90\xd7\x91\xd7\x99\xd7\x96\xd7\xa8\xd7\x99\xd7\x9d";\n    $custom_tab_title2 = "\xd7\x90\xd7\x91\xd7\x99\xd7\x96\xd7\xa8\xd7\x99\xd7\x9d \xd7\x9e\xd7\xa9\xd7\x9c\xd7\x99\xd7\x9e\xd7\x99\xd7\x9d";\n\n    // if the custom field is set, it adds the new tab\n    if ( ! empty($custom_tab_title) ) {\n        $tabs[\'awp-\' . sanitize_title(\'props\')] = array(\n            \'title\' => \'\xd7\x90\xd7\x91\xd7\x99\xd7\x96\xd7\xa8\xd7\x99\xd7\x9d\',\n            \'callback\' => \'woocommerce_upsell_display_in_tab\',\n            \'priority\' => 5\n        );\n    }\n    if ( ! empty($custom_tab_title) ) {\n        $tabs[\'awp-\' . sanitize_title(\'additional-props\')] = array(\n            \'title\' => \'\xd7\x90\xd7\x91\xd7\x99\xd7\x96\xd7\xa8\xd7\x99\xd7\x9d \xd7\x9e\xd7\xa9\xd7\x9c\xd7\x99\xd7\x9e\xd7\x99\xd7\x9d\',\n            \'callback\' => \'woocommerce_cross_sell_display_in_tab\',\n            \'priority\' => 10\n        );\n    }\n    return $tabs;\n}\n\n// Callback to display upsells\nfunction woocommerce_upsell_display_in_tab() {\n     woocommerce_upsell_display( 3, 3 );\n}\n\n\n// Callback to display cross sells\nfunction woocommerce_cross_sell_display_in_tab( $limit = 3, $columns = 3, $orderby = \'rand\', $order = \'desc\' ) {\n    global $product;\n\n    $cross_sells = array_filter( array_map( \'wc_get_product\', $product->get_cross_sell_ids() ), \'wc_products_array_filter_visible\' );\n\n    wc_set_loop_prop( \'name\', \'cross-sells\' );\n    wc_set_loop_prop( \'columns\', apply_filters( \'woocommerce_cross_sells_columns\', $columns ) );\n\n    // Handle orderby and limit results.\n    $orderby     = apply_filters( \'woocommerce_cross_sells_orderby\', $orderby );\n    $order       = apply_filters( \'woocommerce_cross_sells_order\', $order );\n    $cross_sells = wc_products_array_orderby( $cross_sells, $orderby, $order );\n    $limit       = apply_filters( \'woocommerce_cross_sells_total\', $limit );\n    $cross_sells = $limit > 0 ? array_slice( $cross_sells, 0, $limit ) : $cross_sells;\n\n    wc_get_template(\n        \'cart/cross-sells.php\',\n        array(\n            \'cross_sells\'    => $cross_sells,\n\n            // Not used now, but used in previous version of up-sells.php.\n            \'posts_per_page\' => $limit,\n            \'orderby\'        => $orderby,\n            \'columns\'        => $columns,\n        )\n    );\n}\n
Run Code Online (Sandbox Code Playgroud)\n

代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并有效。

\n