我想为一些产品定制按钮,以取代商店页面上的“添加到购物车”按钮,并将动态重新链接设置到其他页面。因此,我在产品页面上做了一个复选框,如果该复选框已启用,则该产品将可以看到带有不同链接的自定义按钮。以下是复选框的代码:
add_action( \'add_meta_boxes\', \'wdm_add_meta_box\' );
add_action( \'add_meta_boxes\', \'wdm_add_customize_enable_metabox\' );
function wdm_add_customize_enable_metabox() {
add_meta_box(
\'Checkbox_metabox\',
\'Check if you want to enable customization button\',
\'wdm_enable_callback\',
\'product\',
\'normal\',
\'high\'
);
}
function wdm_enable_callback( $product ) {
$custom = get_post_custom( $product -> ID );
if( isset($custom[ "_wcm_custom_design_checkbox" ][0] ) ) {
$meta_box_check = $custom[ "_wcm_custom_design_checkbox" ][0];
}
else {
$meta_box_check = FALSE;
}
?>
<tr>
<td>Enable design panel at frontend?</td>
<td><input type="checkbox" name="wcm_enable_checkbox" id="wcm_enable_checkbox" <?php if ( $meta_box_check == true ) { ?> checked="checked"<?php } ?> /></td>
</tr>
<tr>
<?php
}
add_action( \'save_post\', \'wdm_save_meta_check_box_data\', 10,2 );
function wdm_save_meta_check_box_data( $post_id, $product ){
if ( $product -> post_type == \'product\' ) {
if ( isset($_POST["wcm_enable_checkbox"] ) && $_POST["wcm_enable_checkbox"] ) {
update_post_meta( $post_id, \'_wcm_custom_design_checkbox\', $_POST["wcm_enable_checkbox"] );
}
else {
update_post_meta( $post_id, \'_wcm_custom_design_checkbox\', \'\');
}
}
}
上述代码工作正常。我将此代码用于主题
add_to_cart
循环,这里我唯一的问题是如何定制
add_to_cart_url()
有关自定义链接以及如何更改
add_to_cart_text()
发短信给我想要的任何人。
以下是我对所做的更改add_to_cart.php
:
global $product, $post;
$hasCustomization = get_post_meta( $post -> ID, \'_wcm_custom_design_checkbox\', true );
if ( $hasCustomization == \'on\' ) {
echo apply_filters(
\'woocommerce_loop_add_to_cart_link\',
sprintf(
\'<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" class="add_to_cart %s product_type_%s">%s</a>\',
esc_url( $product -> add_to_cart_url() ),//Here i want my own urls
esc_attr( $product->id ),
esc_attr( $product->get_sku() ),
$product->is_purchasable() ? \'\' : \'\',//own css for custom text to show
esc_attr( $product->product_type ),
esc_html( $product->add_to_cart_text() )// Custom Text
),
$product
);
}
else{
echo apply_filters(
\'woocommerce_loop_add_to_cart_link\',
sprintf(
\'<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" class="add_to_cart %s product_type_%s">%s</a>\',
esc_url( $product -> add_to_cart_url() ),
esc_attr( $product -> id ),
esc_attr( $product -> get_sku() ),
$product -> is_purchasable() ? \'add_to_cart_button\' : \'\',
esc_attr( $product -> product_type ),
esc_html( $product -> add_to_cart_text() )
),
$product
);
}
?>
最合适的回答,由SO网友:Ethan O\'Sullivan 整理而成
每Ashish\'s 已发布的答案,下面是通过切换正确答案进行澄清的更新代码add_to_cart_url()
到get_permalink()
:
global $product, $post;
$hasCustomization = get_post_meta( $post -> ID, \'_wcm_custom_design_checkbox\', true );
if ( $hasCustomization == \'on\' ) {
echo apply_filters(
\'woocommerce_loop_add_to_cart_link\',
sprintf(
\'<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" class="add_to_cart %s product_type_%s">%s</a>\',
esc_url( $product -> get_permalink() ),
esc_attr( $product->id ),
esc_attr( $product->get_sku() ),
$product->is_purchasable() ? \'\' : \'\',to show
esc_attr( $product->product_type ),
esc_html( $product->add_to_cart_text() )// Custom Text
),
$product
);
}
else{
echo apply_filters(
\'woocommerce_loop_add_to_cart_link\',
sprintf(
\'<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" class="add_to_cart %s product_type_%s">%s</a>\',
esc_url( $product -> add_to_cart_url() ),
esc_attr( $product -> id ),
esc_attr( $product -> get_sku() ),
$product -> is_purchasable() ? \'add_to_cart_button\' : \'\',
esc_attr( $product -> product_type ),
esc_html( $product -> add_to_cart_text() )
),
$product
);
}
?>