[英]Combining multiple cart item permalink changes in WooCommerce
I would like to change the permalinks of my cart item using the WooCommerce framework.我想使用 WooCommerce 框架更改我的购物车项目的永久链接。
I have the following:我有以下内容:
function addCustomFieldToUrl_1($permaLink, $cart_item, $cartItemId){
if ($cart_item['product_id'] == "1713" ) {
$newPermalink = "www.google.com";
}
return $newPermalink;
}
function addCustomFieldToUrl_2($permaLink, $cart_item, $cartItemId){
if ($cart_item['product_id'] == "2188" ) {
$newPermalink = "www.example.com";
}
return $newPermalink;
}
add_filter('woocommerce_cart_item_permalink', 'addCustomFieldToUrl_1', 10, 3);
add_filter('woocommerce_order_item_permalink', 'addCustomFieldToUrl_1', 10, 3);
add_filter('woocommerce_cart_item_permalink', 'addCustomFieldToUrl_2', 10, 3);
add_filter('woocommerce_order_item_permalink', 'addCustomFieldToUrl_2', 10, 3);
However only the second cart permalink is changed using this method.但是,使用此方法仅更改了第二个购物车永久链接。 If I delete it, then the first one gets changed.如果我删除它,那么第一个就会改变。
How can I combine the two functions into one and expand on that (as I have multiple products where the permalinks will need to be changed?)如何将这两个功能合二为一并对其进行扩展(因为我有多个产品需要更改永久链接?)
You could apply it this way你可以这样应用它
function addCustomFieldToUrl( $permalink, $cart_item, $cartItemId ) {
if ($cart_item['product_id'] == 1713 ) {
$permalink = "www.google.com";
} elseif ($cart_item['product_id'] == 2188 ) {
$permalink = "www.example.com";
}
return $permalink;
}
add_filter('woocommerce_cart_item_permalink', 'addCustomFieldToUrl', 10, 3 );
add_filter('woocommerce_order_item_permalink', 'addCustomFieldToUrl', 10, 3 );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.