Custom display before variation description on Woocommerce variable products
I need to add my own price table before the description of a variation product in woocommerce. I did that by customizing the get_available_variation function of class-wc-product-variable.php, which is in the includes directory of woocommerce.
I don't want to override this plugin, as the changes will get lost after an update.
How do I modify this function without hacking woocommerce?
public function get_available_variation( $variation ) {
if ( is_numeric( $variation ) ) {
$variation = wc_get_product( $variation );
}
if ( ! $variation instanceof WC_Product_Variation ) {
return false;
}
// See if prices should be shown for each variation after selection.
$show_variation_price = apply_filters( 'woocommerce_show_variation_price', $variation->get_price() === '' || $this->get_variation_sale_price( 'min' ) !== $this->get_variation_sale_price( 'max' ) || $this->get_variation_regular_price( 'min' ) !== $this->get_variation_regular_price( 'max' ), $this, $variation );
return apply_filters(
'woocommerce_available_variation', array(
'attributes' => $variation->get_variation_attributes(),
'availability_html' => wc_get_stock_html( $variation ),
'backorders_allowed' => $variation->backorders_allowed(),
'dimensions' => $variation->get_dimensions( false ),
'dimensions_html' => wc_format_dimensions( $variation->get_dimensions( false ) ),
'display_price' => wc_get_price_to_display( $variation ),
'display_regular_price' => wc_get_price_to_display( $variation, array( 'price' => $variation->get_regular_price() ) ),
'image' => wc_get_product_attachment_props( $variation->get_image_id() ),
'image_id' => $variation->get_image_id(),
'is_downloadable' => $variation->is_downloadable(),
'is_in_stock' => $variation->is_in_stock(),
'is_purchasable' => $variation->is_purchasable(),
'is_sold_individually' => $variation->is_sold_individually() ? 'yes' : 'no',
'is_virtual' => $variation->is_virtual(),
'max_qty' => 0 < $variation->get_max_purchase_quantity() ? $variation->get_max_purchase_quantity() : '',
'min_qty' => $variation->get_min_purchase_quantity(),
'price_html' => $show_variation_price ? '<span class="price">' . $variation->get_price_html() . '</span>' : '',
'sku' => $variation->get_sku(),
'variation_description' => CUSTOM_PRICE_TABLE($variation) . wc_format_content( $variation->get_description() ),
'variation_id' => $variation->get_id(),
'variation_is_active' => $variation->variation_is_active(),
'variation_is_visible' => $variation->variation_is_visible(),
'weight' => $variation->get_weight(),
'weight_html' => wc_format_weight( $variation->get_weight() ),
), $this, $variation
);
}
The only change I have to make is adding 'CUSTOM_PRICE_TABLE ($ Variation)' to the variation description.
php wordpress woocommerce product variations
add a comment |
I need to add my own price table before the description of a variation product in woocommerce. I did that by customizing the get_available_variation function of class-wc-product-variable.php, which is in the includes directory of woocommerce.
I don't want to override this plugin, as the changes will get lost after an update.
How do I modify this function without hacking woocommerce?
public function get_available_variation( $variation ) {
if ( is_numeric( $variation ) ) {
$variation = wc_get_product( $variation );
}
if ( ! $variation instanceof WC_Product_Variation ) {
return false;
}
// See if prices should be shown for each variation after selection.
$show_variation_price = apply_filters( 'woocommerce_show_variation_price', $variation->get_price() === '' || $this->get_variation_sale_price( 'min' ) !== $this->get_variation_sale_price( 'max' ) || $this->get_variation_regular_price( 'min' ) !== $this->get_variation_regular_price( 'max' ), $this, $variation );
return apply_filters(
'woocommerce_available_variation', array(
'attributes' => $variation->get_variation_attributes(),
'availability_html' => wc_get_stock_html( $variation ),
'backorders_allowed' => $variation->backorders_allowed(),
'dimensions' => $variation->get_dimensions( false ),
'dimensions_html' => wc_format_dimensions( $variation->get_dimensions( false ) ),
'display_price' => wc_get_price_to_display( $variation ),
'display_regular_price' => wc_get_price_to_display( $variation, array( 'price' => $variation->get_regular_price() ) ),
'image' => wc_get_product_attachment_props( $variation->get_image_id() ),
'image_id' => $variation->get_image_id(),
'is_downloadable' => $variation->is_downloadable(),
'is_in_stock' => $variation->is_in_stock(),
'is_purchasable' => $variation->is_purchasable(),
'is_sold_individually' => $variation->is_sold_individually() ? 'yes' : 'no',
'is_virtual' => $variation->is_virtual(),
'max_qty' => 0 < $variation->get_max_purchase_quantity() ? $variation->get_max_purchase_quantity() : '',
'min_qty' => $variation->get_min_purchase_quantity(),
'price_html' => $show_variation_price ? '<span class="price">' . $variation->get_price_html() . '</span>' : '',
'sku' => $variation->get_sku(),
'variation_description' => CUSTOM_PRICE_TABLE($variation) . wc_format_content( $variation->get_description() ),
'variation_id' => $variation->get_id(),
'variation_is_active' => $variation->variation_is_active(),
'variation_is_visible' => $variation->variation_is_visible(),
'weight' => $variation->get_weight(),
'weight_html' => wc_format_weight( $variation->get_weight() ),
), $this, $variation
);
}
The only change I have to make is adding 'CUSTOM_PRICE_TABLE ($ Variation)' to the variation description.
php wordpress woocommerce product variations
add a comment |
I need to add my own price table before the description of a variation product in woocommerce. I did that by customizing the get_available_variation function of class-wc-product-variable.php, which is in the includes directory of woocommerce.
I don't want to override this plugin, as the changes will get lost after an update.
How do I modify this function without hacking woocommerce?
public function get_available_variation( $variation ) {
if ( is_numeric( $variation ) ) {
$variation = wc_get_product( $variation );
}
if ( ! $variation instanceof WC_Product_Variation ) {
return false;
}
// See if prices should be shown for each variation after selection.
$show_variation_price = apply_filters( 'woocommerce_show_variation_price', $variation->get_price() === '' || $this->get_variation_sale_price( 'min' ) !== $this->get_variation_sale_price( 'max' ) || $this->get_variation_regular_price( 'min' ) !== $this->get_variation_regular_price( 'max' ), $this, $variation );
return apply_filters(
'woocommerce_available_variation', array(
'attributes' => $variation->get_variation_attributes(),
'availability_html' => wc_get_stock_html( $variation ),
'backorders_allowed' => $variation->backorders_allowed(),
'dimensions' => $variation->get_dimensions( false ),
'dimensions_html' => wc_format_dimensions( $variation->get_dimensions( false ) ),
'display_price' => wc_get_price_to_display( $variation ),
'display_regular_price' => wc_get_price_to_display( $variation, array( 'price' => $variation->get_regular_price() ) ),
'image' => wc_get_product_attachment_props( $variation->get_image_id() ),
'image_id' => $variation->get_image_id(),
'is_downloadable' => $variation->is_downloadable(),
'is_in_stock' => $variation->is_in_stock(),
'is_purchasable' => $variation->is_purchasable(),
'is_sold_individually' => $variation->is_sold_individually() ? 'yes' : 'no',
'is_virtual' => $variation->is_virtual(),
'max_qty' => 0 < $variation->get_max_purchase_quantity() ? $variation->get_max_purchase_quantity() : '',
'min_qty' => $variation->get_min_purchase_quantity(),
'price_html' => $show_variation_price ? '<span class="price">' . $variation->get_price_html() . '</span>' : '',
'sku' => $variation->get_sku(),
'variation_description' => CUSTOM_PRICE_TABLE($variation) . wc_format_content( $variation->get_description() ),
'variation_id' => $variation->get_id(),
'variation_is_active' => $variation->variation_is_active(),
'variation_is_visible' => $variation->variation_is_visible(),
'weight' => $variation->get_weight(),
'weight_html' => wc_format_weight( $variation->get_weight() ),
), $this, $variation
);
}
The only change I have to make is adding 'CUSTOM_PRICE_TABLE ($ Variation)' to the variation description.
php wordpress woocommerce product variations
I need to add my own price table before the description of a variation product in woocommerce. I did that by customizing the get_available_variation function of class-wc-product-variable.php, which is in the includes directory of woocommerce.
I don't want to override this plugin, as the changes will get lost after an update.
How do I modify this function without hacking woocommerce?
public function get_available_variation( $variation ) {
if ( is_numeric( $variation ) ) {
$variation = wc_get_product( $variation );
}
if ( ! $variation instanceof WC_Product_Variation ) {
return false;
}
// See if prices should be shown for each variation after selection.
$show_variation_price = apply_filters( 'woocommerce_show_variation_price', $variation->get_price() === '' || $this->get_variation_sale_price( 'min' ) !== $this->get_variation_sale_price( 'max' ) || $this->get_variation_regular_price( 'min' ) !== $this->get_variation_regular_price( 'max' ), $this, $variation );
return apply_filters(
'woocommerce_available_variation', array(
'attributes' => $variation->get_variation_attributes(),
'availability_html' => wc_get_stock_html( $variation ),
'backorders_allowed' => $variation->backorders_allowed(),
'dimensions' => $variation->get_dimensions( false ),
'dimensions_html' => wc_format_dimensions( $variation->get_dimensions( false ) ),
'display_price' => wc_get_price_to_display( $variation ),
'display_regular_price' => wc_get_price_to_display( $variation, array( 'price' => $variation->get_regular_price() ) ),
'image' => wc_get_product_attachment_props( $variation->get_image_id() ),
'image_id' => $variation->get_image_id(),
'is_downloadable' => $variation->is_downloadable(),
'is_in_stock' => $variation->is_in_stock(),
'is_purchasable' => $variation->is_purchasable(),
'is_sold_individually' => $variation->is_sold_individually() ? 'yes' : 'no',
'is_virtual' => $variation->is_virtual(),
'max_qty' => 0 < $variation->get_max_purchase_quantity() ? $variation->get_max_purchase_quantity() : '',
'min_qty' => $variation->get_min_purchase_quantity(),
'price_html' => $show_variation_price ? '<span class="price">' . $variation->get_price_html() . '</span>' : '',
'sku' => $variation->get_sku(),
'variation_description' => CUSTOM_PRICE_TABLE($variation) . wc_format_content( $variation->get_description() ),
'variation_id' => $variation->get_id(),
'variation_is_active' => $variation->variation_is_active(),
'variation_is_visible' => $variation->variation_is_visible(),
'weight' => $variation->get_weight(),
'weight_html' => wc_format_weight( $variation->get_weight() ),
), $this, $variation
);
}
The only change I have to make is adding 'CUSTOM_PRICE_TABLE ($ Variation)' to the variation description.
php wordpress woocommerce product variations
php wordpress woocommerce product variations
edited Nov 23 '18 at 18:58
LoicTheAztec
85.1k136095
85.1k136095
asked Nov 23 '18 at 15:37
Coden Lernen
183
183
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You just need to use woocommerce_available_variation filter hook this way:
add_filter( 'woocommerce_available_variation', 'form_to_out_of_stock_product_variations', 10, 3 );
function form_to_out_of_stock_product_variations( $data, $product, $variation ) {
if( CUSTOM_PRICE_TABLE($variation) )
$data['variation_description'] = CUSTOM_PRICE_TABLE($variation) . $data['variation_description'],
return $data;
}
Code goes in function.php file of your active child theme (or active theme). It should works.
This works! Thank you.
– Coden Lernen
Nov 23 '18 at 20:47
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53449440%2fcustom-display-before-variation-description-on-woocommerce-variable-products%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You just need to use woocommerce_available_variation filter hook this way:
add_filter( 'woocommerce_available_variation', 'form_to_out_of_stock_product_variations', 10, 3 );
function form_to_out_of_stock_product_variations( $data, $product, $variation ) {
if( CUSTOM_PRICE_TABLE($variation) )
$data['variation_description'] = CUSTOM_PRICE_TABLE($variation) . $data['variation_description'],
return $data;
}
Code goes in function.php file of your active child theme (or active theme). It should works.
This works! Thank you.
– Coden Lernen
Nov 23 '18 at 20:47
add a comment |
You just need to use woocommerce_available_variation filter hook this way:
add_filter( 'woocommerce_available_variation', 'form_to_out_of_stock_product_variations', 10, 3 );
function form_to_out_of_stock_product_variations( $data, $product, $variation ) {
if( CUSTOM_PRICE_TABLE($variation) )
$data['variation_description'] = CUSTOM_PRICE_TABLE($variation) . $data['variation_description'],
return $data;
}
Code goes in function.php file of your active child theme (or active theme). It should works.
This works! Thank you.
– Coden Lernen
Nov 23 '18 at 20:47
add a comment |
You just need to use woocommerce_available_variation filter hook this way:
add_filter( 'woocommerce_available_variation', 'form_to_out_of_stock_product_variations', 10, 3 );
function form_to_out_of_stock_product_variations( $data, $product, $variation ) {
if( CUSTOM_PRICE_TABLE($variation) )
$data['variation_description'] = CUSTOM_PRICE_TABLE($variation) . $data['variation_description'],
return $data;
}
Code goes in function.php file of your active child theme (or active theme). It should works.
You just need to use woocommerce_available_variation filter hook this way:
add_filter( 'woocommerce_available_variation', 'form_to_out_of_stock_product_variations', 10, 3 );
function form_to_out_of_stock_product_variations( $data, $product, $variation ) {
if( CUSTOM_PRICE_TABLE($variation) )
$data['variation_description'] = CUSTOM_PRICE_TABLE($variation) . $data['variation_description'],
return $data;
}
Code goes in function.php file of your active child theme (or active theme). It should works.
answered Nov 23 '18 at 18:57
LoicTheAztec
85.1k136095
85.1k136095
This works! Thank you.
– Coden Lernen
Nov 23 '18 at 20:47
add a comment |
This works! Thank you.
– Coden Lernen
Nov 23 '18 at 20:47
This works! Thank you.
– Coden Lernen
Nov 23 '18 at 20:47
This works! Thank you.
– Coden Lernen
Nov 23 '18 at 20:47
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53449440%2fcustom-display-before-variation-description-on-woocommerce-variable-products%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown